/* Advent of Code 2024 in Picat. Problem 13 https://adventofcode.com/2024/day/13 This version uses Picat's nondeterministic between/3 (instead of a for loop). However, it's only working for part 1 (with the domain 1..100. The domain for Part 2 is way to large for this, about 1..10**12. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. import cp. import aoc_utils. main => go. /* Part 1 Benchmark 1: picat 13_nondet.pi Time (mean ± σ): 132.6 ms ± 12.4 ms [User: 117.9 ms, System: 13.9 ms] Range (min … max): 108.4 ms … 160.5 ms 17 runs */ go ?=> nolog, File = "13.txt", Split = split2(read_file_chars(File)), member(Part,1..1), T = cond(Part == 1,0,10000000000000), Sum = 0, foreach(Line in Split) S = Line.split("=+\n, "), [AX,AY,BX,BY,PX1,PY1] = [S[I].to_int : I in [4,6,10,12,15,17]], PX = PX1 + T, PY = PY1 + T, if [X,Y,Z] = s(Part,AX,AY,BX,BY,PX,PY) then Sum := Sum + Z end end, println(Sum), Part == 2. go => true. s(Part,AX,AY,BX,BY,PX,PY) = [X,Y,Z] => if Part == 1 then between(1,100,X), between(1,100,Y) else between(1,10**12,X), between(1,10**12,Y) end, PX == AX*X + BX*Y, PY == AY*X + BY*Y, Z = 3*X+Y. /* % CP Model for comparison s_cp(Part,AX,AY,BX,BY,PX,PY) = [X,Y,Z] => X #>= 0, Y #>= 0, PX #= AX*X + BX*Y, PY #= AY*X + BY*Y, Z #=3*X+Y, solve($[min(Z)],[X,Y]). */