/* Advent of Code 2024 in Picat. Problem 6 https://adventofcode.com/2024/day/6 Only Part 1. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. import aoc_utils. main => go. /* Hyperfine Benchmark 1: picat 6_part1.pi Time (mean ± σ): 189.0 ms ± 6.9 ms [User: 147.7 ms, System: 40.9 ms] Range (min … max): 176.9 ms … 201.8 ms 15 runs */ go => File = "6.txt", M = [Line: Line in read_file_lines(File)], Rows = M.len, Cols = M[1].len, T = copy_term(M), [[X,Y]] = [[I,J] : I in 1..Rows, J in 1..Cols, M[I,J] = '^'], Dir = north, Path = [ [X,Y] ], T[X,Y] := 'x', OK = true, while (OK == true) [NewX,NewY] = fd(Dir,X,Y), if not check(Rows,Cols,NewX,NewY) then OK := false else if M[NewX,NewY] == '#' then NewDir = rot(Dir), Dir := NewDir else X := NewX, Y := NewY, if M[X,Y] != '#' then Path := Path ++ [[X,Y]], T[X,Y] := 'x' end end end end, % print_matrix(T), println([ 1 : I in 1..Rows, J in 1..Cols, T[I,J] == 'x'].len). check(Rows,Cols,X,Y) => X >= 1, X <= Rows, Y >= 1, Y <= Cols. print_matrix(M) => foreach(Row in M) println(Row) end, nl. % Forward one step fd(north,X,Y) = [X-1,Y]. fd(east,X,Y) = [X,Y+1]. fd(south,X,Y) = [X+1,Y]. fd(west,X,Y) = [X,Y-1]. % Rotate 90degrees rot(north) = east. rot(east) = south. rot(south) = west. rot(west) = north.