···1212 (x + 1, y + 1)
1313 ]
14141515+at grid x y = case ((grid !? y) >>= (!? x)) of
1616+ Just ch -> ch
1717+ Nothing -> '.'
1818+1919+isBlocked grid (x, y) = at grid x y == '@'
2020+1521answer input =
1622 length
1723 [ 1 | y <- [0 .. length grid - 1],
1824 x <- [0 .. (length $ grid !! y) - 1],
1919- grid !! y !! x == '@',
2020- length [1 | (x2, y2) <- surrounding x y,
2121- ((grid !? y2) >>= (!? x2)) == (Just '@')] < 4
2525+ at grid x y == '@',
2626+ (length $ filter (isBlocked grid) $ surrounding x y) < 4
2227 ]
2328 where
2429 Right grid = parse ((many $ oneOf "@.") `sepEndBy` char '\n') "" input
+3-7
2025/4/p1.tri
···11-import "trilogy:debug" use dbg, tap
11+import "trilogy:debug" use dbg
22import "trilogy:core" use length
33import "trilogy:io" use readall
44import "trilogy:grid" as grid
55-import "trilogy:number" use im, re, swap
65import "trilogy:array" use map, filter
76import "trilogy:parsec" use parse, end_by, char, many, char_of
8799-func get g p = with g.(swap <| im p).(re p) when 'mia cancel '.' else yield
88+func get g p = with g.p when 'mia cancel '.' else yield
1091110proc main!() {
1211 let rows = parse (end_by (char '\n') <| many <| char_of "@.") readall!()
···1514 let mut count = 0
1615 for pos:'@' in g {
1716 let around = grid::surrounding pos
1818- # TODO: something is wrong when passing the large record around, the runtime freezes
1919- # up. Accessing from the array here instead works a little better, but it's still way
2020- # too slow, and not linearly
2121- |> map (get rows)
1717+ |> map (get g)
2218 |> filter ((==) '@')
2319 |> length
2420 if around < 4 { count += 1 }