/* Advent of Code 2024 in Picat. Problem 13 https://adventofcode.com/2024/day/13 This variant parses the entries using regex 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. import aoc_utils_regex. main => go. /* Part 1 and 2 Benchmark 1: picat -g go 13_regex.pi Time (mean ± σ): 60.8 ms ± 7.6 ms [User: 42.5 ms, System: 18.3 ms] Range (min … max): 39.5 ms … 76.3 ms 43 runs Part 1 Time (mean ± σ): 52.6 ms ± 5.4 ms [User: 34.5 ms, System: 18.1 ms] Range (min … max): 41.0 ms … 62.1 ms 45 runs Part 2 Time (mean ± σ): 50.2 ms ± 7.8 ms [User: 32.1 ms, System: 18.0 ms] Range (min … max): 23.8 ms … 66.5 ms 39 runs */ % Find all numbers in a string (an entry) % Now moved to aoc_utils.pi % find_all_numbers(S) = regex_find_all("(\\d+)",S).map(to_int). go ?=> Ls = read_file_chars("13.txt").split2().map(find_all_numbers), member(Part,1..2), T = cond(Part == 1,0,10000000000000), [Z : [AX,AY,BX,BY,PX,PY] in Ls, Z=s(AX,AY,BX,BY,PX+T,PY+T)].sum.println, Part == 2. go => true. s(AX,AY,BX,BY,PX,PY) = Z => X #>= 0, Y #>= 0, PX #= AX*X + BX*Y, PY #= AY*X + BY*Y, Z #=3*X+Y, solve($[min(Z)],[X,Y]).