-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from ephemient/hs/day16
Day 16: Reindeer Maze
- Loading branch information
Showing
6 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{-# LANGUAGE ViewPatterns #-} | ||
|
||
-- | | ||
-- Module: Day16 | ||
-- Description: <https://adventofcode.com/2024/day/16 Day 16: Reindeer Maze> | ||
module Day16 (part1, part2) where | ||
|
||
import Control.Arrow (second) | ||
import Control.Exception (assert) | ||
import Data.Heap (FstMinPolicy) | ||
import Data.Heap qualified as Heap (insert, singleton, view) | ||
import Data.Map qualified as Map (empty, insert, (!?)) | ||
import Data.Set (Set) | ||
import Data.Set qualified as Set (empty, fromList, insert, member, notMember, size) | ||
import Data.Text (Text) | ||
import Data.Text qualified as T (foldl) | ||
|
||
parse :: Text -> Maybe (Set (Int, Int), (Int, Int), (Int, Int)) | ||
parse input = case T.foldl parse' (0, 0, Set.empty, [], []) input of | ||
(_, _, maze, [start], [end]) -> Just (maze, start, end) | ||
_ -> Nothing | ||
where | ||
parse' (y, _, maze, start, end) '\n' = (y + 1, 0, maze, start, end) | ||
parse' (y, x, maze, start, end) '#' = (y, x + 1, Set.insert (y, x) maze, start, end) | ||
parse' (y, x, maze, start, end) 'S' = (y, x + 1, maze, (y, x) : start, end) | ||
parse' (y, x, maze, start, end) 'E' = (y, x + 1, maze, start, (y, x) : end) | ||
parse' (y, x, maze, start, end) _ = (y, x + 1, maze, start, end) | ||
|
||
part1 :: Text -> Maybe Int | ||
part1 input = do | ||
(maze, start, end) <- parse input | ||
let go visited (Heap.view -> Just ((score, pv@(p@(y, x), v@(dy, dx))), queue')) | ||
| p == end = Just score | ||
| pv `Set.member` visited = go visited queue' | ||
| otherwise = | ||
go (Set.insert pv visited) . foldl' (flip Heap.insert) queue' $ | ||
[(score + 1, (p', v)) | let p' = (y + dy, x + dx), p' `Set.notMember` maze] | ||
++ [(score + 1000, (p, (-dx, dy))), (score + 1000, (p, (dx, -dy)))] | ||
go _ _ = Nothing | ||
go Set.empty $ Heap.singleton @FstMinPolicy (0 :: Int, (start, (0, 1))) | ||
|
||
part2 :: Text -> Maybe Int | ||
part2 input = do | ||
(maze, start, end) <- parse input | ||
best <- part1 input | ||
let go acc visited (Heap.view -> Just ((score, (pv@(p@(y, x), v@(dy, dx)), path)), queue)) | ||
| p == end = assert (score == best) $ go (acc <> Set.fromList (p : path)) visited' queue | ||
| Just score' <- visited Map.!? pv, score' < score = go acc visited queue | ||
| otherwise = | ||
go acc visited' . foldl' (flip Heap.insert) queue . map (second (,path')) . filter ok $ | ||
[(score + 1, (p', v)) | let p' = (y + dy, x + dx), p' `Set.notMember` maze] | ||
++ [(score + 1000, (p, (-dx, dy))), (score + 1000, (p, (dx, -dy)))] | ||
where | ||
visited' = Map.insert pv score visited | ||
path' = p : path | ||
ok (score', pv') = score' <= best && maybe True (>= score) (visited Map.!? pv') | ||
go acc _ _ = Set.size acc | ||
pure $ go Set.empty Map.empty $ Heap.singleton @FstMinPolicy (0 :: Int, ((start, (0, 1)), [])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day16Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day16 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example1, example2 :: Text | ||
example1 = | ||
T.unlines | ||
[ -- :r!pbpaste | sed 's/.*/ , "&"/;1s/,/ /' | ||
"###############", | ||
"#.......#....E#", | ||
"#.#.###.#.###.#", | ||
"#.....#.#...#.#", | ||
"#.###.#####.#.#", | ||
"#.#.#.......#.#", | ||
"#.#.#####.###.#", | ||
"#...........#.#", | ||
"###.#.#####.#.#", | ||
"#...#.....#.#.#", | ||
"#.#.#.###.#.#.#", | ||
"#.....#...#.#.#", | ||
"#.###.#.#.#.#.#", | ||
"#S..#.....#...#", | ||
"###############" | ||
] | ||
example2 = | ||
T.unlines | ||
[ "#################", | ||
"#...#...#...#..E#", | ||
"#.#.#.#.#.#.#.#.#", | ||
"#.#.#.#...#...#.#", | ||
"#.#.#.#.###.#.#.#", | ||
"#...#.#.#.....#.#", | ||
"#.#.#.#.#.#####.#", | ||
"#.#...#.#.#.....#", | ||
"#.#.#####.#.###.#", | ||
"#.#.#.......#...#", | ||
"#.#.###.#####.###", | ||
"#.#.#...#.....#.#", | ||
"#.#.#.#####.###.#", | ||
"#.#.#.........#.#", | ||
"#.#.#.#########.#", | ||
"#S#.............#", | ||
"#################" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example1 `shouldBe` Just 7036 | ||
part1 example2 `shouldBe` Just 11048 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example1 `shouldBe` Just 45 | ||
part2 example2 `shouldBe` Just 64 |