diff options
author | Nat Lasseter <user@4574.co.uk> | 2024-12-06 18:14:05 +0000 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2024-12-06 18:14:05 +0000 |
commit | 36c1d3c758815b5f3a62d4b527bddbce8805101d (patch) | |
tree | 37d5c1491c1ffc3ca8fcd580805e0d5bfe3b52d5 /day06/day06.hs | |
parent | 21851bc112ea69f41ac44350f5f0f3906317513c (diff) |
[Day 02] Part 2: brute force, naïve, slow. I'm sure it would get there eventually...
Diffstat (limited to 'day06/day06.hs')
-rw-r--r-- | day06/day06.hs | 34 |
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)) |