/* Advent of Code 2024 in Picat. Problem 8 https://adventofcode.com/2024/day/8 This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. /* Part 1 and Part 2 together Benchmark 1: picat -g go 8.pi Time (mean ± σ): 37.8 ms ± 6.5 ms [User: 21.2 ms, System: 16.5 ms] Range (min … max): 21.1 ms … 49.2 ms 55 runs */ go => File = "8.txt", M = [Line: Line in read_file_lines(File)], N = M.len, % The antennas As = [ [I,J,M[I,J]] : I in 1..N, J in 1..N, M[I,J] != '.'], Map = new_map(), foreach([I,J,C] in As) Map.put(C,Map.get(C,[])++[[I,J]]) end, % For part 1: the multiple T is 1..1 and a plain hash table, % For part 2: the multiple T is 1..N and we place the antennas in the map member(Part,1..2), Found = cond(Part == 1,new_map(),new_map([[I,J]=1 : [I,J,C] in As])), % Test each antenna type foreach(Char in Map.keys) K = Map.get(Char), % Test each pair foreach(I in 1..K.len, J in I+1..K.len) Diff = diff(K[I],K[J]), KI = K[I], KJ = K[J], foreach(T in 1..cond(Part==1,1,N)) A = KI[1]-T*Diff[1], B = KI[2]-T*Diff[2], if A >= 1, A <= N, B >=1, B <= N then Found.put([A,B],1) end, C = KJ[1]+T*Diff[1], D = KJ[2]+T*Diff[2], if C >= 1, C <= N, D >= 1, D <= N then Found.put([C,D],1) end end end end, println(Found.keys.len), Part == 2. % Ensure we do both parts diff([X1,Y1],[X2,Y2]) = [X2-X1,Y2-Y1].