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 Grid = Map Point2 Char 21 22toGrid :: String -> Grid 23toGrid s = Map.fromList [((x,y),c) | (y,r) <- enumerate (lines s), (x,c) <- enumerate r] 24 25neighbors8 :: Point2 -> [Point2] 26neighbors8 (x,y) = 27 [(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)] 28 29neighbors4 :: Point2 -> [Point2] 30neighbors4 (x,y) = 31 [(x-1,y), (x+1,y), (x,y-1), (x,y+1)] 32 33trim :: String -> String 34trim = f . f 35 where f = reverse . dropWhile isSpace