summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--day06/day06.hs34
1 files changed, 30 insertions, 4 deletions
diff --git a/day06/day06.hs b/day06/day06.hs
index fa022f7..3f11fed 100644
--- a/day06/day06.hs
+++ b/day06/day06.hs
@@ -67,16 +67,42 @@ el ?: arr
route :: Grid -> [Coo] -> Guard -> [Coo]
route grid seen guard@(coo, dir)
- | nguard == Nothing = coo ?: seen
- | otherwise = route grid (coo ?: seen) (fromJust nguard)
+ | nguard == Nothing = coo ?: seen
+ | otherwise = route grid (coo ?: seen) (fromJust nguard)
where
nguard = step grid guard
+route2 :: Grid -> [Guard] -> Bool
+route2 grid seen@(guard:_)
+ | nguard == Nothing = False
+ | (fromJust nguard) `elem` seen = True
+ | otherwise = route2 grid ((fromJust nguard):seen)
+ where
+ nguard = step grid guard
+
+putObs :: Grid -> Coo -> Grid
+putObs grid (r, c) =
+ let (br,tr:ar) = splitAt r grid
+ (bc,_:ac) = splitAt c tr in
+ br ++
+ [bc ++ [Obs] ++ ac] ++
+ ar
+
part1 :: Grid -> Guard -> Int
-part1 grid guard = length $ route grid [] guard
+part1 grid guard =
+ length $ route grid [] guard
+
+part2 :: Grid -> Guard -> Int
+part2 grid guard =
+ length $
+ filter (route2A . putObsA) $
+ route grid [] guard
+ where
+ route2A = (flip route2) [guard]
+ putObsA = putObs grid
main = do
file <- readFile "day06.input"
let (grid, guard) = consume file
putStrLn ("Part 1: " ++ (show $ part1 grid guard))
- --putStrLn ("Part 2: " ++ (show $ part2 lns))
+ putStrLn ("Part 2: " ++ (show $ part2 grid guard))