Solve 2024-06-01
This commit is contained in:
parent
645279cac7
commit
fa8d8225f9
3 changed files with 186 additions and 7 deletions
|
|
@ -0,0 +1,130 @@
|
||||||
|
.......................#....#....................#....#..##.##.........................................#...........#...........#..
|
||||||
|
.............#........................................#....................#............#.......................#............#....
|
||||||
|
.........#.............#..#....#..........................#.......#.#.......#...............................................##....
|
||||||
|
..........#....#....................................................#....................#...........#....#.......................
|
||||||
|
.............#...#....................#...........#.........#...........................#............#............................
|
||||||
|
..........................................................................................#.....#.......#...................##....
|
||||||
|
...............................................#.#......#..............#...#...................##...........#....#..#...#.........
|
||||||
|
..#.................#......#........#......................#.......#...............................#..............................
|
||||||
|
.........................#...............#.....................................#....#..#..#............................#..........
|
||||||
|
......................#.................................................................#..........#..............#...#......#....
|
||||||
|
............................#.....##..........#.....................#..........#..#.......#.......................................
|
||||||
|
......#...###...........#.................................................................#.......................................
|
||||||
|
......................#.#..#................#..........#................#...........#............#..............#...#.........#...
|
||||||
|
....#.........................#..#...............................................#....#.............#.............................
|
||||||
|
..#............#...#............#..........#...............#.....#....##.........................#........#.......................
|
||||||
|
..................#............#..............#..........................................................#..#..#........#.........
|
||||||
|
.......#...#............#..........................................................#..............................#.............#.
|
||||||
|
..#................#....................#.#...#.................................#.........#....................#.........#........
|
||||||
|
...................................#..#..........................#........#.....#.........................................#.......
|
||||||
|
...........##.................##............#................#...#..................................##...#........................
|
||||||
|
..............#..................#....................................................................#...........................
|
||||||
|
.#...#....#..........##..................................#.............................................................#......#...
|
||||||
|
......#.....#................................#...............................................................#......#.............
|
||||||
|
..........#.............................................#............................##....#.........................#............
|
||||||
|
..........#......#.................#...#........#...........#.#..........#........#....................................#..........
|
||||||
|
.#..................#.#............................#.......................#.........#.......#....................................
|
||||||
|
.....................#....#..................#..................................................##..#................#............
|
||||||
|
......#......................#..#..............##.......#..................................................#..............#.......
|
||||||
|
..........................................................................#........#...#..............#.................#.........
|
||||||
|
....................##....#.......................#........#...................#.............##.......#...........................
|
||||||
|
.........#............#..................................................................................#........................
|
||||||
|
........................................................................#................#........................#.............#.
|
||||||
|
............#.......#.................................#............#..............................................................
|
||||||
|
.#.............#...............................................#....#.........#....#..................................#.#.........
|
||||||
|
.......................................#.....................................................................................#....
|
||||||
|
........#........................................#...............#........................................................#.......
|
||||||
|
...............................#.........#........#...........#.......................................................#.........#.
|
||||||
|
.....................##...#....................................................#............#..................#..#..........##...
|
||||||
|
................#..........#...#............................................................#...##................................
|
||||||
|
...................#........#...............................#.................#.........#.........................................
|
||||||
|
.................................................................................................................#................
|
||||||
|
............................#......................................................#.....................................#......#.
|
||||||
|
......#................#............................#.............................................................................
|
||||||
|
..#...........#.....#.........#....#................#............................................#................................
|
||||||
|
..................................#.......#.......#...........#.........#................#.....................................#.#
|
||||||
|
...............................................#............................................#.#.....................#.......##....
|
||||||
|
..............................#...............#..............................#...#...#.............................#...........#..
|
||||||
|
.......#...........#..........................................................#........#..#.........#.....#.....#...#.............
|
||||||
|
......................................................#.#............#.........#.#...........#........##.....#....................
|
||||||
|
...........................................................................#..........#.........................#.........#.......
|
||||||
|
....#.........................#.....................................................................................#...#......#..
|
||||||
|
...#.......#............................................................#...............................#....................##.#.
|
||||||
|
.................................#.....#................................................................#.........................
|
||||||
|
.....................................#........................#....................................#..............................
|
||||||
|
..............#.......................................................................#.......#....................#..........#...
|
||||||
|
................................#..........................................#.................................#.....#..............
|
||||||
|
...........#.......................................................#...............#.....................#..............#......#..
|
||||||
|
..................................#..........................#.................................................................#..
|
||||||
|
.............##......#....................................................#......#...............................................#
|
||||||
|
#.......#...................#..........#...................#.................#..................................................#.
|
||||||
|
.......#..........#.....##...........#.......................#............#..............................#.............#..........
|
||||||
|
.............#...................##......................................................................................#........
|
||||||
|
#.............................#...................................................................#...............................
|
||||||
|
.#...................................#....##..................................................................................#...
|
||||||
|
.................................#.#..................#..#..#..........................#.......#..................................
|
||||||
|
..................#....#....#....................................................#....................................#........#..
|
||||||
|
..........................................#...#.....#...........#.........#............#............#..........................#..
|
||||||
|
...............................................................................#..............................................#...
|
||||||
|
.............................#......#....................#....#....................................................#.#.#..........
|
||||||
|
................##......#........#........#...........#..........................#......................#..............#..........
|
||||||
|
.....................#.....#..#.............................................................................................#.....
|
||||||
|
.......#..#...........#................................................................#..........................................
|
||||||
|
#.......................................................................................................#...#.....................
|
||||||
|
.#..........................................................#.....................................................#...............
|
||||||
|
#.....#...#...............#..#......#........#..#...................#...................#.................#.......................
|
||||||
|
..#.................................................................................#.#......................#....................
|
||||||
|
.......................#.............#..........#.......................#...................................#.....................
|
||||||
|
.................#......#......#...#.#.#.............................#.......................................................#....
|
||||||
|
..#...........................................................................................................#...................
|
||||||
|
.................................#..................#.....#........#.........................................................#....
|
||||||
|
.............#......................#...............................................#...#.............................#...#....#..
|
||||||
|
.#............................#..........................#.#...........................#..........................#.#.............
|
||||||
|
.............................#.........#.................##............................#...................#......................
|
||||||
|
.........#......#....#.........#..............................................................................................#..#
|
||||||
|
.....................#......#.................#..#..........................#..................#..............................#.#.
|
||||||
|
........................................................#......................#............#.......#......#......................
|
||||||
|
........................................#....##.#........#......#..............................#..........#..........#.##.........
|
||||||
|
.............#.......#.......#......................#..........................................#.........#............#...#...#.#.
|
||||||
|
..................#........................................................#...............................#.....#.....#..........
|
||||||
|
.........#.................#........#..............^..............#.................##......#...................................#.
|
||||||
|
....#...................#.#.................................#...#.................................................#...............
|
||||||
|
.................#.............................................#................#.....#......#.......#...................#........
|
||||||
|
...............#...............................................................................#..#...............#...............
|
||||||
|
........................................##..........................................................................##............
|
||||||
|
............................................#...................#........#...#...#....#.........#...................#.............
|
||||||
|
.....#...#...................#....................................#......#...........#...............#............................
|
||||||
|
........................#..................#.....................#.................................................#......##......
|
||||||
|
...#.................#....................#..................................#.#.........................#.......#................
|
||||||
|
....................#............#..................................#...#......#....................#...............#.............
|
||||||
|
.....#...............#.......###........#............#......................#.......#....#........................................
|
||||||
|
....................#........#...........#..................................#..#................#.............#...................
|
||||||
|
.....#..................#...................#.#..................................#.......#..#.....................................
|
||||||
|
..#..#.......#........................................#.................................................................#.........
|
||||||
|
.......#............#...#....#.......................#.#...................................#.........................#..#.#.......
|
||||||
|
..............#......................................................................................................#............
|
||||||
|
............#....................#........................#.........#...#.........................................................
|
||||||
|
...................................#..............................#.........................................................#.....
|
||||||
|
......##...........#.#........................#.........................................................................#.........
|
||||||
|
..........................#.....................................................#..............................#..............#...
|
||||||
|
.........................#...#.....................#..#................#................................................#....#....
|
||||||
|
...#.................................#...#.......#...............................................................#................
|
||||||
|
..................................#....................................#...........................##..#.#.....................#..
|
||||||
|
.#.............#.....#...............................................................#...............#...............#..#.........
|
||||||
|
.........#.............................#........................#.............#......................#........#............#......
|
||||||
|
...................................#..#...#.....................................#.................................................
|
||||||
|
........................................##..........#..............................................#.....................#........
|
||||||
|
......#...........................................#....#.....................#...................#..#.........#...................
|
||||||
|
.#....#.....#........##..................................#......#................................................#....#..##.......
|
||||||
|
..........#....................................................................##.........................#...........#...........
|
||||||
|
...........#.#......#.......#..................#............#...............................................#...#..............#..
|
||||||
|
.................................................................#.......#.........#.....#........#....##....#..........#.........
|
||||||
|
.............................#......................................................#.....#...#...#....#...........#..........#.#.
|
||||||
|
.....................#........#.........................#......................................#................#...............#.
|
||||||
|
.........................................................................#...................................................#....
|
||||||
|
..............#...#..#................#....................................#.......#....#.#...............#.........#.............
|
||||||
|
.......#......#.#............................................#.......#............................................................
|
||||||
|
##......................................................#..#.................................##...................#..#...........#
|
||||||
|
...........#.#...#........#............#..............................#........................#.........................#..#.....
|
||||||
|
................................................##.........................................#.................#....................
|
||||||
|
............................#....................................................#...#........................#.#......#........#.
|
||||||
|
|
@ -7,10 +7,9 @@ module AoC.Y2024.D06 (solve) where
|
||||||
import AoC.Riddle
|
import AoC.Riddle
|
||||||
import AoC.Util
|
import AoC.Util
|
||||||
|
|
||||||
|
import Data.Array ((!), (//))
|
||||||
import Data.Array qualified as A
|
import Data.Array qualified as A
|
||||||
|
|
||||||
-- import Data.Set qualified as S
|
|
||||||
|
|
||||||
import Data.String (lines)
|
import Data.String (lines)
|
||||||
import Prelude hiding (lines)
|
import Prelude hiding (lines)
|
||||||
|
|
||||||
|
|
@ -86,13 +85,59 @@ movePosition (x, y) d =
|
||||||
let (dx, dy) = directionToVector d
|
let (dx, dy) = directionToVector d
|
||||||
in (x + dx, y + dy)
|
in (x + dx, y + dy)
|
||||||
|
|
||||||
moveGuard :: MapState -> Maybe (Position, MapState)
|
getTile :: Matrix Tile -> Position -> Maybe Tile
|
||||||
moveGuard MapState{..} = do
|
getTile m pos =
|
||||||
(pos, d) <- guardPosition
|
guard (A.inRange (A.bounds m) pos) >> Just (m ! pos)
|
||||||
case d of {}
|
|
||||||
|
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 -> 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 -> Maybe Int
|
||||||
part2 riddle = Nothing
|
part2 riddle = Nothing
|
||||||
|
|
|
||||||
|
|
@ -24,3 +24,7 @@ spec = do
|
||||||
describe "Day 5" do
|
describe "Day 5" do
|
||||||
it "calculates correctly" do
|
it "calculates correctly" do
|
||||||
runAoC Y2024 D05 `shouldReturn` Right [5452, 4598]
|
runAoC Y2024 D05 `shouldReturn` Right [5452, 4598]
|
||||||
|
|
||||||
|
describe "Day 6" do
|
||||||
|
it "calculates correctly" do
|
||||||
|
runAoC Y2024 D05 `shouldReturn` Right [5453, 0]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue