summaryrefslogtreecommitdiff
path: root/10-2.hs
diff options
context:
space:
mode:
authorLaura Orvokki Kursula <lav@vampires.gay>2024-12-10 10:37:41 +0100
committerLaura Orvokki Kursula <lav@vampires.gay>2024-12-10 10:37:41 +0100
commitf8d90470145abf0147d80a94e01055584f39e3cf (patch)
tree17e92059986f2c61ba3fdeb5360a0181cc8de48b /10-2.hs
parent3bf3d795e565bf380a68f696b27bed6bb484156f (diff)
downloadaoc2024-f8d90470145abf0147d80a94e01055584f39e3cf.tar.gz
aoc2024-f8d90470145abf0147d80a94e01055584f39e3cf.zip
10-2
Diffstat (limited to '10-2.hs')
-rw-r--r--10-2.hs37
1 files changed, 37 insertions, 0 deletions
diff --git a/10-2.hs b/10-2.hs
new file mode 100644
index 0000000..cf2840c
--- /dev/null
+++ b/10-2.hs
@@ -0,0 +1,37 @@
+import Data.Array
+
+data Pos = Pos Int Int deriving (Eq,Ix,Ord,Show)
+
+type Map = Array Pos Int
+
+step :: Map -> Pos -> [Pos]
+step m p@(Pos x y) = filter ((== 1) . subtract (m ! p) . (m !))
+ . filter (`elem` indices m)
+ $ steps
+ where
+ steps = [ Pos (x+1) y
+ , Pos (x-1) y
+ , Pos x (y+1)
+ , Pos x (y-1)
+ ]
+
+width,height :: [[a]] -> Int
+width = length . head
+height = length
+
+score :: Map -> Pos -> Int
+score m p | m ! p == 9 = 1
+ | otherwise = sum . map (score m) $ step m p
+
+trailheads :: Map -> [Pos]
+trailheads = map fst . filter ((== 0) . snd) . assocs
+
+parse :: [[Char]] -> Map
+parse ls = listArray
+ (Pos 0 0, Pos (width ls - 1) (height ls - 1))
+ (map (read . pure) $ concat ls)
+
+main :: IO ()
+main = do
+ m <- parse . lines <$> getContents
+ print . sum . map (score m) . trailheads $ m