/* Advent of Code 2024 in Picat. Problem 11 https://adventofcode.com/2024/day/11 This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. /* Hyperfine Part 1 and 2 Benchmark 1: picat -g go 11.pi Time (mean ± σ): 123.7 ms ± 9.5 ms [User: 106.1 ms, System: 17.5 ms] Range (min … max): 108.7 ms … 138.0 ms 22 runs Only part 1: Benchmark 1: picat -g go 11.pi Time (mean ± σ): 42.3 ms ± 6.7 ms [User: 25.8 ms, System: 16.4 ms] Range (min … max): 22.6 ms … 52.6 ms 62 runs Only part 2: Benchmark 1: picat -g go 11.pi Time (mean ± σ): 121.2 ms ± 8.0 ms [User: 101.6 ms, System: 19.4 ms] Range (min … max): 100.8 ms … 131.9 ms 21 runs */ go => File = "11.txt", L = read_file_lines(File).first.split().map(to_int), member(Part,1..2), Map = new_map(), foreach(N in L) Map.put(N,Map.get(N,0)+1) end, foreach(_ in 1..cond(Part==1,25,75)) Map2 = new_map(), foreach(N=C in Map,V in n(N)) Map2.put(V,Map2.get(V,0)+C) end, Map := Map2 end, println(Map.values.sum), Part == 2. table n(N) = Res => if N == 0 then Res = [1] elseif N.to_string.len mod 2 == 0 then Res = half(N) else Res = [N*2024] end. table half(N) = [S1,S2] => S = N.to_string, Len = S.len, Half = Len // 2, S1 = S[1..Half].to_int, S2 = S[Half+1..Len].to_int.