-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.hs
33 lines (24 loc) · 961 Bytes
/
day06.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Main where
import qualified Data.Map as M
import Data.Foldable (find)
import Data.List (unfoldr, nub)
type Vec = (Int, Int)
type Guard = Maybe (Vec, Vec)
main = interact (unlines . sequence [part1] . parse)
part1 = ("Part 1: " ++) . show . length . nub . uncurry walkAll
walkAll grid start = case start of
Just first -> fst first : unfoldr (fmap (\state -> (fst state, Just state)) . walk grid) start
walk :: M.Map Vec Char -> Guard -> Guard
walk grid guard = do
(pos, dir) <- guard
c <- grid M.!? move pos dir
case c of
'#' -> return (move pos (right dir), right dir)
_ -> return (move pos dir, dir)
right (x, y) = (-y, x)
move (x, y) (dx, dy) = (x + dx, y + dy)
parse :: String -> (M.Map Vec Char, Guard)
parse input = (M.fromList grid, start)
where
grid = [((x, y), c) | (l, y) <- zip (lines input) [0..], (c, x) <- zip l [0..]]
start = fmap (const (0, -1)) <$> find ((== '^') . snd) grid