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