Advent of Code solutions

cleanup

+11 -10
+8 -3
2025/4/p1.hs
··· 12 12 (x + 1, y + 1) 13 13 ] 14 14 15 + at grid x y = case ((grid !? y) >>= (!? x)) of 16 + Just ch -> ch 17 + Nothing -> '.' 18 + 19 + isBlocked grid (x, y) = at grid x y == '@' 20 + 15 21 answer input = 16 22 length 17 23 [ 1 | y <- [0 .. length grid - 1], 18 24 x <- [0 .. (length $ grid !! y) - 1], 19 - grid !! y !! x == '@', 20 - length [1 | (x2, y2) <- surrounding x y, 21 - ((grid !? y2) >>= (!? x2)) == (Just '@')] < 4 25 + at grid x y == '@', 26 + (length $ filter (isBlocked grid) $ surrounding x y) < 4 22 27 ] 23 28 where 24 29 Right grid = parse ((many $ oneOf "@.") `sepEndBy` char '\n') "" input
+3 -7
2025/4/p1.tri
··· 1 - import "trilogy:debug" use dbg, tap 1 + import "trilogy:debug" use dbg 2 2 import "trilogy:core" use length 3 3 import "trilogy:io" use readall 4 4 import "trilogy:grid" as grid 5 - import "trilogy:number" use im, re, swap 6 5 import "trilogy:array" use map, filter 7 6 import "trilogy:parsec" use parse, end_by, char, many, char_of 8 7 9 - func get g p = with g.(swap <| im p).(re p) when 'mia cancel '.' else yield 8 + func get g p = with g.p when 'mia cancel '.' else yield 10 9 11 10 proc main!() { 12 11 let rows = parse (end_by (char '\n') <| many <| char_of "@.") readall!() ··· 15 14 let mut count = 0 16 15 for pos:'@' in g { 17 16 let around = grid::surrounding pos 18 - # TODO: something is wrong when passing the large record around, the runtime freezes 19 - # up. Accessing from the array here instead works a little better, but it's still way 20 - # too slow, and not linearly 21 - |> map (get rows) 17 + |> map (get g) 22 18 |> filter ((==) '@') 23 19 |> length 24 20 if around < 4 { count += 1 }