From fa8d8225f9425d4ae191d048e416ad8b28106fac Mon Sep 17 00:00:00 2001 From: Alexander Kobjolke Date: Sat, 7 Dec 2024 23:03:39 +0100 Subject: [PATCH] Solve 2024-06-01 --- data/Y2024/D06/riddle | 130 +++++++++++++++++++++++++++++++++ src/AoC/Y2024/D06.hs | 59 +++++++++++++-- test/spec/AoCSpec/Y2024Spec.hs | 4 + 3 files changed, 186 insertions(+), 7 deletions(-) diff --git a/data/Y2024/D06/riddle b/data/Y2024/D06/riddle index e69de29..fed9be4 100644 --- a/data/Y2024/D06/riddle +++ b/data/Y2024/D06/riddle @@ -0,0 +1,130 @@ +.......................#....#....................#....#..##.##.........................................#...........#...........#.. +.............#........................................#....................#............#.......................#............#.... +.........#.............#..#....#..........................#.......#.#.......#...............................................##.... +..........#....#....................................................#....................#...........#....#....................... +.............#...#....................#...........#.........#...........................#............#............................ +..........................................................................................#.....#.......#...................##.... +...............................................#.#......#..............#...#...................##...........#....#..#...#......... +..#.................#......#........#......................#.......#...............................#.............................. +.........................#...............#.....................................#....#..#..#............................#.......... +......................#.................................................................#..........#..............#...#......#.... +............................#.....##..........#.....................#..........#..#.......#....................................... +......#...###...........#.................................................................#....................................... +......................#.#..#................#..........#................#...........#............#..............#...#.........#... +....#.........................#..#...............................................#....#.............#............................. +..#............#...#............#..........#...............#.....#....##.........................#........#....................... +..................#............#..............#..........................................................#..#..#........#......... +.......#...#............#..........................................................#..............................#.............#. +..#................#....................#.#...#.................................#.........#....................#.........#........ +...................................#..#..........................#........#.....#.........................................#....... +...........##.................##............#................#...#..................................##...#........................ +..............#..................#....................................................................#........................... +.#...#....#..........##..................................#.............................................................#......#... +......#.....#................................#...............................................................#......#............. +..........#.............................................#............................##....#.........................#............ +..........#......#.................#...#........#...........#.#..........#........#....................................#.......... +.#..................#.#............................#.......................#.........#.......#.................................... +.....................#....#..................#..................................................##..#................#............ +......#......................#..#..............##.......#..................................................#..............#....... +..........................................................................#........#...#..............#.................#......... +....................##....#.......................#........#...................#.............##.......#........................... +.........#............#..................................................................................#........................ +........................................................................#................#........................#.............#. +............#.......#.................................#............#.............................................................. +.#.............#...............................................#....#.........#....#..................................#.#......... +.......................................#.....................................................................................#.... +........#........................................#...............#........................................................#....... +...............................#.........#........#...........#.......................................................#.........#. +.....................##...#....................................................#............#..................#..#..........##... +................#..........#...#............................................................#...##................................ +...................#........#...............................#.................#.........#......................................... +.................................................................................................................#................ +............................#......................................................#.....................................#......#. +......#................#............................#............................................................................. +..#...........#.....#.........#....#................#............................................#................................ +..................................#.......#.......#...........#.........#................#.....................................#.# +...............................................#............................................#.#.....................#.......##.... +..............................#...............#..............................#...#...#.............................#...........#.. +.......#...........#..........................................................#........#..#.........#.....#.....#...#............. +......................................................#.#............#.........#.#...........#........##.....#.................... +...........................................................................#..........#.........................#.........#....... +....#.........................#.....................................................................................#...#......#.. +...#.......#............................................................#...............................#....................##.#. +.................................#.....#................................................................#......................... +.....................................#........................#....................................#.............................. +..............#.......................................................................#.......#....................#..........#... +................................#..........................................#.................................#.....#.............. +...........#.......................................................#...............#.....................#..............#......#.. +..................................#..........................#.................................................................#.. +.............##......#....................................................#......#...............................................# +#.......#...................#..........#...................#.................#..................................................#. +.......#..........#.....##...........#.......................#............#..............................#.............#.......... +.............#...................##......................................................................................#........ +#.............................#...................................................................#............................... +.#...................................#....##..................................................................................#... +.................................#.#..................#..#..#..........................#.......#.................................. +..................#....#....#....................................................#....................................#........#.. +..........................................#...#.....#...........#.........#............#............#..........................#.. +...............................................................................#..............................................#... +.............................#......#....................#....#....................................................#.#.#.......... +................##......#........#........#...........#..........................#......................#..............#.......... +.....................#.....#..#.............................................................................................#..... +.......#..#...........#................................................................#.......................................... +#.......................................................................................................#...#..................... +.#..........................................................#.....................................................#............... +#.....#...#...............#..#......#........#..#...................#...................#.................#....................... +..#.................................................................................#.#......................#.................... +.......................#.............#..........#.......................#...................................#..................... +.................#......#......#...#.#.#.............................#.......................................................#.... +..#...........................................................................................................#................... +.................................#..................#.....#........#.........................................................#.... +.............#......................#...............................................#...#.............................#...#....#.. +.#............................#..........................#.#...........................#..........................#.#............. +.............................#.........#.................##............................#...................#...................... +.........#......#....#.........#..............................................................................................#..# +.....................#......#.................#..#..........................#..................#..............................#.#. +........................................................#......................#............#.......#......#...................... +........................................#....##.#........#......#..............................#..........#..........#.##......... +.............#.......#.......#......................#..........................................#.........#............#...#...#.#. +..................#........................................................#...............................#.....#.....#.......... +.........#.................#........#..............^..............#.................##......#...................................#. +....#...................#.#.................................#...#.................................................#............... +.................#.............................................#................#.....#......#.......#...................#........ +...............#...............................................................................#..#...............#............... +........................................##..........................................................................##............ +............................................#...................#........#...#...#....#.........#...................#............. +.....#...#...................#....................................#......#...........#...............#............................ +........................#..................#.....................#.................................................#......##...... +...#.................#....................#..................................#.#.........................#.......#................ +....................#............#..................................#...#......#....................#...............#............. +.....#...............#.......###........#............#......................#.......#....#........................................ +....................#........#...........#..................................#..#................#.............#................... +.....#..................#...................#.#..................................#.......#..#..................................... +..#..#.......#........................................#.................................................................#......... +.......#............#...#....#.......................#.#...................................#.........................#..#.#....... +..............#......................................................................................................#............ +............#....................#........................#.........#...#......................................................... +...................................#..............................#.........................................................#..... +......##...........#.#........................#.........................................................................#......... +..........................#.....................................................#..............................#..............#... +.........................#...#.....................#..#................#................................................#....#.... +...#.................................#...#.......#...............................................................#................ +..................................#....................................#...........................##..#.#.....................#.. +.#.............#.....#...............................................................#...............#...............#..#......... +.........#.............................#........................#.............#......................#........#............#...... +...................................#..#...#.....................................#................................................. +........................................##..........#..............................................#.....................#........ +......#...........................................#....#.....................#...................#..#.........#................... +.#....#.....#........##..................................#......#................................................#....#..##....... +..........#....................................................................##.........................#...........#........... +...........#.#......#.......#..................#............#...............................................#...#..............#.. +.................................................................#.......#.........#.....#........#....##....#..........#......... +.............................#......................................................#.....#...#...#....#...........#..........#.#. +.....................#........#.........................#......................................#................#...............#. +.........................................................................#...................................................#.... +..............#...#..#................#....................................#.......#....#.#...............#.........#............. +.......#......#.#............................................#.......#............................................................ +##......................................................#..#.................................##...................#..#...........# +...........#.#...#........#............#..............................#........................#.........................#..#..... +................................................##.........................................#.................#.................... +............................#....................................................#...#........................#.#......#........#. diff --git a/src/AoC/Y2024/D06.hs b/src/AoC/Y2024/D06.hs index a10982a..93dcfc7 100644 --- a/src/AoC/Y2024/D06.hs +++ b/src/AoC/Y2024/D06.hs @@ -7,10 +7,9 @@ module AoC.Y2024.D06 (solve) where import AoC.Riddle import AoC.Util +import Data.Array ((!), (//)) import Data.Array qualified as A --- import Data.Set qualified as S - import Data.String (lines) import Prelude hiding (lines) @@ -86,13 +85,59 @@ movePosition (x, y) d = let (dx, dy) = directionToVector d in (x + dx, y + dy) -moveGuard :: MapState -> Maybe (Position, MapState) -moveGuard MapState{..} = do - (pos, d) <- guardPosition - case d of {} +getTile :: Matrix Tile -> Position -> Maybe Tile +getTile m pos = + guard (A.inRange (A.bounds m) pos) >> Just (m ! pos) + +turnRight :: Direction -> Direction +turnRight = \case + U -> R + R -> D + D -> L + L -> U + +moveGuard :: MapState -> MapState +moveGuard s@MapState{..} = case guardPosition of + Nothing -> s + Just (pos, d) -> + let + newPos = movePosition pos d + in + case getTile labMap newPos of + Just Obstruction -> do + let + newDirection = turnRight d + MapState{labMap = labMap // [(pos, Guard newDirection)], guardPosition = Just (pos, newDirection)} + Just _ -> MapState{labMap = labMap // [(pos, Visited), (newPos, Guard d)], guardPosition = Just (newPos, d)} + Nothing -> MapState{labMap = labMap // [(pos, Visited)], guardPosition = Nothing} + +-- moveGuard :: MapState -> Maybe (Position, MapState) +-- moveGuard MapState{..} = do +-- (pos, d) <- guardPosition +-- let +-- newPos = movePosition pos d +-- case getTile labMap newPos of +-- Just Obstruction -> do +-- let +-- newDirection = turnRight d +-- Just (pos, MapState{labMap = labMap // [(pos, Guard newDirection)], guardPosition = Just (pos, newDirection)}) +-- Just _ -> Just (newPos, MapState{labMap = labMap // [(pos, Visited), (newPos, Guard d)], guardPosition = Just (newPos, d)}) +-- Nothing -> Just (pos, MapState{labMap = labMap // [(pos, Visited)], guardPosition = Nothing}) + +guardLeftTheMap :: MapState -> Bool +guardLeftTheMap MapState{..} = isNothing guardPosition + +guardRoute :: MapState -> [MapState] +guardRoute = takeWhile (not . guardLeftTheMap) . iterate moveGuard part1 :: Riddle -> Maybe Int -part1 riddle = Nothing +part1 riddle = do + start <- parse riddle + MapState{..} <- + viaNonEmpty + head + (dropWhile (not . guardLeftTheMap) (iterate moveGuard start)) + pure $ length $ filter (\(_, t) -> t == Visited) (A.assocs labMap) part2 :: Riddle -> Maybe Int part2 riddle = Nothing diff --git a/test/spec/AoCSpec/Y2024Spec.hs b/test/spec/AoCSpec/Y2024Spec.hs index 2f8014e..8265402 100644 --- a/test/spec/AoCSpec/Y2024Spec.hs +++ b/test/spec/AoCSpec/Y2024Spec.hs @@ -24,3 +24,7 @@ spec = do describe "Day 5" do it "calculates correctly" do runAoC Y2024 D05 `shouldReturn` Right [5452, 4598] + + describe "Day 6" do + it "calculates correctly" do + runAoC Y2024 D05 `shouldReturn` Right [5453, 0]