day6 part one

This commit is contained in:
JasterV 2025-03-29 01:03:05 +01:00
parent 53c6d3f68f
commit 68a1641121
5 changed files with 234 additions and 7 deletions

View file

@ -25,12 +25,13 @@ source-repository head
library
exposed-modules:
Data.Matrix
Day1
Day2
Day3
Day4
Day4.Matrix
Day5
Day6
other-modules:
Paths_aoc2024
autogen-modules:

130
input/day6.txt Normal file
View file

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

View file

@ -1,10 +1,12 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Day4.Matrix
module Data.Matrix
( Matrix,
Position,
buildMatrix,
groupWith,
lookupValue,
lookup,
lookupMultiple,
filterWithKey,
@ -13,6 +15,7 @@ module Day4.Matrix
where
import qualified Data.IntMap.Lazy as IntMap
import Data.List (find)
import Data.Map.Lazy (Map)
import qualified Data.Map.Lazy as Map
import Data.Maybe
@ -20,6 +23,8 @@ import Prelude hiding (lookup)
newtype Matrix v = Matrix (Map (Int, Int) v)
type Position = (Int, Int)
buildMatrix :: [[a]] -> Matrix a
buildMatrix xs = Matrix (go xs 0 Map.empty)
where
@ -36,21 +41,31 @@ buildMatrix xs = Matrix (go xs 0 Map.empty)
size :: Matrix v -> Int
size (Matrix hmap) = Map.size hmap
filterWithKey :: ((Int, Int) -> v -> Bool) -> Matrix v -> Matrix v
filterWithKey :: (Position -> v -> Bool) -> Matrix v -> Matrix v
filterWithKey f (Matrix hmap) = Matrix (Map.filterWithKey f hmap)
lookup :: (Int, Int) -> Matrix v -> Maybe v
lookup :: Position -> Matrix v -> Maybe v
lookup position (Matrix hmap) = Map.lookup position hmap
lookupMultiple :: [(Int, Int)] -> Matrix v -> [v]
lookupMultiple :: [Position] -> Matrix v -> [v]
lookupMultiple positions matrix = mapMaybe (`lookup` matrix) positions
{--
Search for the given value on the matrix.
Return the position of the first match if found and nothing if it doens't exist.
--}
lookupValue :: (Eq v) => v -> Matrix v -> Maybe Position
lookupValue v (Matrix hmap) =
let entries = Map.toAscList hmap
mEntry = find ((== v) . snd) entries
in fst <$> mEntry
{--
Given a matrix of elements and a function mapping a position into an aggregation of its values,
group the elements by the aggregation result.
The values are grouped in order.
--}
groupWith :: forall v. ((Int, Int) -> Int) -> Matrix v -> [[v]]
groupWith :: forall v. (Position -> Int) -> Matrix v -> [[v]]
groupWith f (Matrix hmap) =
let sortedEntries = Map.toAscList hmap
intMap = foldr (\(position, value) -> insertValue (f position) value) IntMap.empty sortedEntries

View file

@ -1,7 +1,7 @@
module Day4 (partOne, partTwo) where
import Data.List (sort, transpose)
import qualified Day4.Matrix as M
import qualified Data.Matrix as M
partOne :: String -> Int
partOne input =

81
src/Day6.hs Normal file
View file

@ -0,0 +1,81 @@
module Day6 (partOne, partTwo) where
import Data.HashSet (HashSet)
import qualified Data.HashSet as HashSet
import Data.Matrix (Matrix, Position)
import qualified Data.Matrix as M
import Data.Maybe
import Prelude hiding (Left, Right)
data Error = StartingPositionNotFound
deriving (Show)
partOne :: String -> Maybe Int
partOne input = length <$> predictGuardsRoute (parseLabMap input)
partTwo :: String -> Int
partTwo _input = 0
-- Laboratory Map
--
type LabMap = Matrix Char
type Direction = Char
type Guard = (Position, Direction)
type Visited = HashSet (Position, Direction)
parseLabMap :: String -> LabMap
parseLabMap = M.buildMatrix . lines
findGuard :: LabMap -> Maybe Guard
findGuard matrix =
let mUp = M.lookupValue '^' matrix
mDown = M.lookupValue 'v' matrix
mRight = M.lookupValue '>' matrix
mLeft = M.lookupValue '<' matrix
in case [mUp, mDown, mRight, mLeft] of
[Just pos, _, _, _] -> Just (pos, '^')
[_, Just pos, _, _] -> Just (pos, 'v')
[_, _, Just pos, _] -> Just (pos, '>')
[_, _, _, Just pos] -> Just (pos, '<')
_ -> Nothing
predictGuardsRoute :: LabMap -> Maybe [Position]
predictGuardsRoute labMap = do
guard <- findGuard labMap
return (go guard HashSet.empty HashSet.empty)
where
go :: Guard -> Visited -> HashSet Position -> [Position]
go guard@(position, _) visited acc =
let guard' = moveGuard guard
acc' = HashSet.insert position acc
visited' = HashSet.insert guard visited
in -- If we have hit a loop or if the guard can't move anymore, finish prediction
if HashSet.member guard visited || guard == guard'
then HashSet.toList acc'
else go guard' visited' acc'
moveGuard :: Guard -> Guard
moveGuard guard =
let guard'@(position', _) = moveForward guard
mObstacle = M.lookup position' labMap
in case mObstacle of
Nothing -> guard
Just '#' -> turnRight guard
Just _ -> guard'
moveForward :: Guard -> Guard
moveForward ((row, col), '^') = ((row - 1, col), '^')
moveForward ((row, col), 'v') = ((row + 1, col), 'v')
moveForward ((row, col), '>') = ((row, col + 1), '>')
moveForward ((row, col), '<') = ((row, col - 1), '<')
moveForward guard = guard
turnRight :: Guard -> Guard
turnRight (pos, '^') = (pos, '>')
turnRight (pos, '>') = (pos, 'v')
turnRight (pos, 'v') = (pos, '<')
turnRight (pos, '<') = (pos, '^')
turnRight guard = guard