/* Advent of Code 2024 in Picat. Problem 13 https://adventofcode.com/2024/day/13 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 and 2 Benchmark 1: picat 13.pi Time (mean ± σ): 58.8 ms ± 7.8 ms [User: 42.7 ms, System: 16.0 ms] Range (min … max): 36.9 ms … 69.7 ms 39 runs Part 1 Benchmark 1: picat 13.pi Time (mean ± σ): 47.4 ms ± 8.2 ms [User: 33.2 ms, System: 14.2 ms] Range (min … max): 27.2 ms … 62.0 ms 54 runs Part 2 Benchmark 1: picat 13.pi Time (mean ± σ): 47.3 ms ± 9.3 ms [User: 29.8 ms, System: 17.5 ms] Range (min … max): 23.6 ms … 60.8 ms 43 runs */ go ?=> nolog, File = "13.txt", Split = split2(read_file_chars(File)), member(Part,1..2), 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(AX,AY,BX,BY,PX,PY) then Sum := Sum + Z end end, println(Sum), Part == 2. go => true. s(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,Z]).