Solve 2024-06-01

This commit is contained in:
Alexander Kobjolke 2024-12-07 23:03:39 +01:00
parent 645279cac7
commit fa8d8225f9
3 changed files with 186 additions and 7 deletions

View file

@ -0,0 +1,130 @@
.......................#....#....................#....#..##.##.........................................#...........#...........#..
.............#........................................#....................#............#.......................#............#....
.........#.............#..#....#..........................#.......#.#.......#...............................................##....
..........#....#....................................................#....................#...........#....#.......................
.............#...#....................#...........#.........#...........................#............#............................
..........................................................................................#.....#.......#...................##....
...............................................#.#......#..............#...#...................##...........#....#..#...#.........
..#.................#......#........#......................#.......#...............................#..............................
.........................#...............#.....................................#....#..#..#............................#..........
......................#.................................................................#..........#..............#...#......#....
............................#.....##..........#.....................#..........#..#.......#.......................................
......#...###...........#.................................................................#.......................................
......................#.#..#................#..........#................#...........#............#..............#...#.........#...
....#.........................#..#...............................................#....#.............#.............................
..#............#...#............#..........#...............#.....#....##.........................#........#.......................
..................#............#..............#..........................................................#..#..#........#.........
.......#...#............#..........................................................#..............................#.............#.
..#................#....................#.#...#.................................#.........#....................#.........#........
...................................#..#..........................#........#.....#.........................................#.......
...........##.................##............#................#...#..................................##...#........................
..............#..................#....................................................................#...........................
.#...#....#..........##..................................#.............................................................#......#...
......#.....#................................#...............................................................#......#.............
..........#.............................................#............................##....#.........................#............
..........#......#.................#...#........#...........#.#..........#........#....................................#..........
.#..................#.#............................#.......................#.........#.......#....................................
.....................#....#..................#..................................................##..#................#............
......#......................#..#..............##.......#..................................................#..............#.......
..........................................................................#........#...#..............#.................#.........
....................##....#.......................#........#...................#.............##.......#...........................
.........#............#..................................................................................#........................
........................................................................#................#........................#.............#.
............#.......#.................................#............#..............................................................
.#.............#...............................................#....#.........#....#..................................#.#.........
.......................................#.....................................................................................#....
........#........................................#...............#........................................................#.......
...............................#.........#........#...........#.......................................................#.........#.
.....................##...#....................................................#............#..................#..#..........##...
................#..........#...#............................................................#...##................................
...................#........#...............................#.................#.........#.........................................
.................................................................................................................#................
............................#......................................................#.....................................#......#.
......#................#............................#.............................................................................
..#...........#.....#.........#....#................#............................................#................................
..................................#.......#.......#...........#.........#................#.....................................#.#
...............................................#............................................#.#.....................#.......##....
..............................#...............#..............................#...#...#.............................#...........#..
.......#...........#..........................................................#........#..#.........#.....#.....#...#.............
......................................................#.#............#.........#.#...........#........##.....#....................
...........................................................................#..........#.........................#.........#.......
....#.........................#.....................................................................................#...#......#..
...#.......#............................................................#...............................#....................##.#.
.................................#.....#................................................................#.........................
.....................................#........................#....................................#..............................
..............#.......................................................................#.......#....................#..........#...
................................#..........................................#.................................#.....#..............
...........#.......................................................#...............#.....................#..............#......#..
..................................#..........................#.................................................................#..
.............##......#....................................................#......#...............................................#
#.......#...................#..........#...................#.................#..................................................#.
.......#..........#.....##...........#.......................#............#..............................#.............#..........
.............#...................##......................................................................................#........
#.............................#...................................................................#...............................
.#...................................#....##..................................................................................#...
.................................#.#..................#..#..#..........................#.......#..................................
..................#....#....#....................................................#....................................#........#..
..........................................#...#.....#...........#.........#............#............#..........................#..
...............................................................................#..............................................#...
.............................#......#....................#....#....................................................#.#.#..........
................##......#........#........#...........#..........................#......................#..............#..........
.....................#.....#..#.............................................................................................#.....
.......#..#...........#................................................................#..........................................
#.......................................................................................................#...#.....................
.#..........................................................#.....................................................#...............
#.....#...#...............#..#......#........#..#...................#...................#.................#.......................
..#.................................................................................#.#......................#....................
.......................#.............#..........#.......................#...................................#.....................
.................#......#......#...#.#.#.............................#.......................................................#....
..#...........................................................................................................#...................
.................................#..................#.....#........#.........................................................#....
.............#......................#...............................................#...#.............................#...#....#..
.#............................#..........................#.#...........................#..........................#.#.............
.............................#.........#.................##............................#...................#......................
.........#......#....#.........#..............................................................................................#..#
.....................#......#.................#..#..........................#..................#..............................#.#.
........................................................#......................#............#.......#......#......................
........................................#....##.#........#......#..............................#..........#..........#.##.........
.............#.......#.......#......................#..........................................#.........#............#...#...#.#.
..................#........................................................#...............................#.....#.....#..........
.........#.................#........#..............^..............#.................##......#...................................#.
....#...................#.#.................................#...#.................................................#...............
.................#.............................................#................#.....#......#.......#...................#........
...............#...............................................................................#..#...............#...............
........................................##..........................................................................##............
............................................#...................#........#...#...#....#.........#...................#.............
.....#...#...................#....................................#......#...........#...............#............................
........................#..................#.....................#.................................................#......##......
...#.................#....................#..................................#.#.........................#.......#................
....................#............#..................................#...#......#....................#...............#.............
.....#...............#.......###........#............#......................#.......#....#........................................
....................#........#...........#..................................#..#................#.............#...................
.....#..................#...................#.#..................................#.......#..#.....................................
..#..#.......#........................................#.................................................................#.........
.......#............#...#....#.......................#.#...................................#.........................#..#.#.......
..............#......................................................................................................#............
............#....................#........................#.........#...#.........................................................
...................................#..............................#.........................................................#.....
......##...........#.#........................#.........................................................................#.........
..........................#.....................................................#..............................#..............#...
.........................#...#.....................#..#................#................................................#....#....
...#.................................#...#.......#...............................................................#................
..................................#....................................#...........................##..#.#.....................#..
.#.............#.....#...............................................................#...............#...............#..#.........
.........#.............................#........................#.............#......................#........#............#......
...................................#..#...#.....................................#.................................................
........................................##..........#..............................................#.....................#........
......#...........................................#....#.....................#...................#..#.........#...................
.#....#.....#........##..................................#......#................................................#....#..##.......
..........#....................................................................##.........................#...........#...........
...........#.#......#.......#..................#............#...............................................#...#..............#..
.................................................................#.......#.........#.....#........#....##....#..........#.........
.............................#......................................................#.....#...#...#....#...........#..........#.#.
.....................#........#.........................#......................................#................#...............#.
.........................................................................#...................................................#....
..............#...#..#................#....................................#.......#....#.#...............#.........#.............
.......#......#.#............................................#.......#............................................................
##......................................................#..#.................................##...................#..#...........#
...........#.#...#........#............#..............................#........................#.........................#..#.....
................................................##.........................................#.................#....................
............................#....................................................#...#........................#.#......#........#.

View file

@ -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

View file

@ -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]