-
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.
- Loading branch information
Showing
6 changed files
with
162 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,79 @@ | ||
{-# LANGUAGE BlockArguments #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | | ||
-- Module: Day15 | ||
-- Description: <https://adventofcode.com/2024/day/15 Day 15: Warehouse Woes> | ||
module Day15 (part1, part2) where | ||
|
||
import Control.Arrow (Arrow (first, second)) | ||
import Control.Exception (assert) | ||
import Data.Array.Unboxed (IArray, UArray, assocs, listArray, (!), (//)) | ||
import Data.List (sort) | ||
import Data.Text (Text) | ||
import Data.Text qualified as T (breakOn, concat, concatMap, foldl', justifyLeft, length, lines, singleton, unpack) | ||
import Data.Text.Read (Reader) | ||
|
||
parse :: (IArray a Char) => Reader (a (Int, Int) Char) | ||
parse input | ||
| null grid = Left "empty warehouse" | ||
| otherwise = | ||
Right | ||
( listArray ((0, 0), (height - 1, width - 1)) . T.unpack . T.concat $ | ||
T.justifyLeft width '.' <$> grid, | ||
rest | ||
) | ||
where | ||
(grid, rest) = first T.lines $ T.breakOn "\n\n" input | ||
height = length grid | ||
width = maximum $ 0 : map T.length grid | ||
|
||
part1 :: Text -> Either String Int | ||
part1 input = do | ||
(grid0, moves) <- parse @UArray input | ||
pos0 <- case filter ((== '@') . snd) $ assocs grid0 of | ||
[(pos0, _)] -> Right pos0 | ||
_ -> Left "can't find @" | ||
pure . sum $ | ||
[ 100 * y + x | ||
| ((y, x), c) <- assocs . fst $ T.foldl' move (grid0, pos0) moves, | ||
c == 'O' || c == '[' | ||
] | ||
where | ||
move state@(grid, pos) c | ||
| '^' <- c = move' moveY (first pred pos) (-1) | ||
| 'v' <- c = move' moveY (first succ pos) 1 | ||
| '<' <- c = move' moveX (second pred pos) (-1) | ||
| '>' <- c = move' moveX (second succ pos) 1 | ||
where | ||
move' mover pos' delta = | ||
assert (grid ! pos == '@') . maybe state ((,pos') . (grid //) . sort) $ | ||
mover delta pos' [(pos', '@'), (pos, '.')] | ||
moveY, moveX :: Int -> (Int, Int) -> [((Int, Int), Char)] -> Maybe [((Int, Int), Char)] | ||
moveY dy pos'@(y, x) k = case grid ! pos' of | ||
'.' -> Just k | ||
'O' -> moveY dy (y + dy, x) $ ((y + dy, x), 'O') : (pos', '.') : k | ||
'[' -> | ||
assert (grid ! (y, x + 1) == ']') do | ||
k' <- | ||
moveY dy (y + dy, x) $ | ||
((y + dy, x), '[') : ((y + dy, x + 1), ']') : ((y, x), '.') : ((y, x + 1), '.') : k | ||
moveY dy (y + dy, x + 1) k' | ||
']' -> assert (grid ! (y, x - 1) == '[') do | ||
k' <- moveY dy (y + dy, x) $ ((y + dy, x - 1), '[') : ((y + dy, x), ']') : ((y, x - 1), '.') : ((y, x), '.') : k | ||
moveY dy (y + dy, x - 1) k' | ||
_ -> Nothing | ||
moveX dx pos'@(y, x) k = case grid ! pos' of | ||
'.' -> Just k | ||
d | d == 'O' || d == '[' || d == ']' -> moveX dx (y, x + dx) $ ((y, x + dx), d) : (pos', '.') : k | ||
_ -> Nothing | ||
move state _ = state | ||
|
||
part2 :: Text -> Either String Int | ||
part2 = | ||
part1 . T.concatMap \case | ||
'#' -> "##" | ||
'.' -> ".." | ||
'@' -> "@." | ||
'O' -> "[]" | ||
c -> T.singleton c |
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,70 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day15Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day15 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example1, example2, example3 :: Text | ||
example1 = | ||
T.unlines | ||
[ "##########", | ||
"#..O..O.O#", | ||
"#......O.#", | ||
"#.OO..O.O#", | ||
"#[email protected].#", | ||
"#O#..O...#", | ||
"#O..O..O.#", | ||
"#.OO.O.OO#", | ||
"#....O...#", | ||
"##########", | ||
"", | ||
"<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^", | ||
"vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v", | ||
"><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<", | ||
"<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^", | ||
"^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><", | ||
"^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^", | ||
">^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^", | ||
"<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>", | ||
"^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>", | ||
"v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^" | ||
] | ||
example2 = | ||
T.unlines | ||
[ "########", | ||
"#..O.O.#", | ||
"##@.O..#", | ||
"#...O..#", | ||
"#.#.O..#", | ||
"#...O..#", | ||
"#......#", | ||
"########", | ||
"", | ||
"<^^>>>vv<v>>v<<" | ||
] | ||
example3 = | ||
T.unlines | ||
[ "#######", | ||
"#...#.#", | ||
"#.....#", | ||
"#..OO@#", | ||
"#..O..#", | ||
"#.....#", | ||
"#######", | ||
"", | ||
"<vv<<^^<<^^" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example2 `shouldBe` Right 2028 | ||
part1 example1 `shouldBe` Right 10092 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example3 `shouldBe` Right 618 | ||
part2 example1 `shouldBe` Right 9021 |