Advent of Code 2025
1module Util where
2
3import qualified Data.Map as Map
4import Data.Map (Map)
5
6splitOn :: (Char -> Bool) -> String -> [String]
7splitOn p s = case dropWhile p s of
8 "" -> []
9 s' -> w : splitOn p s''
10 where (w, s'') = break p s'
11
12split :: Char -> String -> [String]
13split c = splitOn (== c)
14
15enumerate :: [a] -> [(Int,a)]
16enumerate l = zip [0..] l
17
18type Point2 = (Int,Int)
19type Grid = Map Point2 Char
20
21toGrid :: String -> Grid
22toGrid s = Map.fromList [((x,y),c) | (y,r) <- enumerate (lines s), (x,c) <- enumerate r]
23
24neighbors8 :: Point2 -> [Point2]
25neighbors8 (x,y) =
26 [(x-1,y-1),(x,y-1),(x+1,y-1),(x-1,y),(x+1,y),(x-1,y+1),(x,y+1),(x+1,y+1)]
27
28neighbors4 :: Point2 -> [Point2]
29neighbors4 (x,y) =
30 [(x-1,y), (x+1,y), (x,y-1), (x,y+1)]