/* Advent of Code 2024 in Picat. Problem 17 https://adventofcode.com/2024/day/17 Only part 1 This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. import aoc_utils. import aoc_utils_regex. main => go. /* Part 1 Hyperfine Benchmark 1: picat 17.pi Time (mean ± σ): 32.0 ms ± 8.3 ms [User: 18.1 ms, System: 13.5 ms] Range (min … max): 17.4 ms … 49.7 ms 57 runs */ go => Chars = read_file_chars("17.txt"), [A,B,C|Program] = Chars.split2().map(find_all_numbers).flatten, OK = true, Out = {}, IP = 0, while (OK==true) if (IP >= Program.len) OK := false end, % Halt if (OK) IP1 = IP+1, Cmd = Program[IP1], if (Cmd == 0) A := A div 2**combo(Program[IP1+1],[A,B,C]), IP := IP + 2 elseif (Cmd == 1) B := B ^ Program[IP1+1], IP := IP + 2 elseif (Cmd == 2) B := combo(Program[IP1+1],[A,B,C]) mod 8, IP := IP + 2 elseif (Cmd == 3) if (A != 0) IP := Program[IP1+1] else IP := IP + 2 end elseif (Cmd == 4) B := B ^ C, IP := IP + 2 elseif (Cmd == 5) Out := Out ++ {combo(Program[IP1+1],[A,B,C]) mod 8},IP := IP + 2 elseif (Cmd == 6) B := A div 2**combo(Program[IP1+1],[A,B,C]), IP := IP + 2 elseif (Cmd == 7) C := A div 2**combo(Program[IP1+1],[A,B,C]), IP := IP + 2 else printf("Invalid command: %w!",Cmd),halt end end end, println(out=Out.to_list.map(to_string).join(',')), nl. table combo(X,_Regs) = X, membchk(X,0..3) => true. combo(4,[A,B,C]) = A. combo(5,[A,B,C]) = B. combo(6,[A,B,C]) = C. combo(7,[A,B,C]) = 0 => println("Invalid"), halt.