From 68a16411217f8f65b21b97955e262926c9f11c7e Mon Sep 17 00:00:00 2001 From: JasterV <49537445+JasterV@users.noreply.github.com> Date: Sat, 29 Mar 2025 01:03:05 +0100 Subject: [PATCH] day6 part one --- aoc2024.cabal | 3 +- input/day6.txt | 130 +++++++++++++++++++++++++++++++++++ src/{Day4 => Data}/Matrix.hs | 25 +++++-- src/Day4.hs | 2 +- src/Day6.hs | 81 ++++++++++++++++++++++ 5 files changed, 234 insertions(+), 7 deletions(-) create mode 100644 input/day6.txt rename src/{Day4 => Data}/Matrix.hs (71%) create mode 100644 src/Day6.hs diff --git a/aoc2024.cabal b/aoc2024.cabal index 9396eb3..96780f4 100644 --- a/aoc2024.cabal +++ b/aoc2024.cabal @@ -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: diff --git a/input/day6.txt b/input/day6.txt new file mode 100644 index 0000000..f4dd47a --- /dev/null +++ b/input/day6.txt @@ -0,0 +1,130 @@ +.....#..#................#...#.....#.......................................................#.............................##....... +......................#..............................................................#..............#..........................#.. +.........#........................#.....................................................#..............#...........#........##.... +..........#......................#.....#...#............#..........................#.....#........................................ +#....................................................................................................................#............ +.#....#......................#.......................#...............................#...#.....#...................#........#..... +..#..#.......................##........#...............................................................#........#.........#....... +..............................................................#...#.........#..#.................................................. +.............####..................................#................#..#.....................................#.....#.............. +..........#................#................................................#........................#.....#...................... +..............................................#.........#.....................#..................#.......................#........ +.........................................##..#.................................#.....#............................................ +............#..#...#...............................#.#.....#...............#...............................#..............#.#.#..# +.................#..........#..#....#.....................#......................................##............#.................. +.......#....#.....................#......##...................#..............#.................#........#..#......#............... +...............................................#.............#.................................................................... +..............#..............................................##..................#..........................#............#........ +................#.......................#..#............................................................#..........#..#.........#. +.#.......................###............#.........#.................................................#............................. +........#......................................................................................#.....#.......#...#................ +#.#.........................#..#.............................................#....#........................#...................... +.......#.....#...............................#.....#......................#.................................#.....#...#........... +................................................................................................#............#.................... +.......#......#...............#...#............................#................................#................................# +.#.....................................................................................................................#......#... +...........#..........................................#........#...................#.............................................. +....#......................................................................................#.....##.#...................#......... +...#..........................................................................##.......................................#.....#.... +#........#................#...............#.........#..#.#..............##..............#........#............#....#.....#........ +......#......................................#...............#....#..#...........................#................................ +#..............................................................................#............#...#..#....................#......#.. +.......#.........................................#.........#.......#....................................#......................... +......#......#.............................................#.....................#...........#.............#..#................... +#.......................#........#............................................##..............................#................... +..........................................................................#........................................#....#......#.. +..........#..#...............................................#............................#.#..............................#...... +....#...........#..#...................................#................................................................#......... +..............#..........#...............#....#.....................................#.......................#..................... +.............#........#....#.........#...............#.........#.................................................................. +.#......#...................#.......#...........................................................................#.............#... +.............##...........#.........................................#......................#.......................#.............. +.....#................#.....#...................##.......#......#...........#.......#............................................# +.#..#.............................................................#..............................................#................ +...#......#............................#.......................................................................................... +...................................................................................#.............................................. +#...................................#...................##..................#...............................................#..... +................#.....#...........................#............................................................................... +.................................#............#........................................................#.............#............ +..................#......#........................................................#..^.....#...................................... +..#.....#.................................................#........#.........................................................#..#. +................#..............................#....................................................#............................. +................#....#..........................................#............................#.................................... +...............#.......................................#.....................................#..............................#..... +..................................................#............#..............#........##.#.....................................#. +.#..................................#......#..#............#.................#.............#......#..#.......................#.... +..................#..............................#..........................#..........................##............#.......#.... +.........#..................................................................#...................#..........................#...... +................#....................................#............#............................................................#.. +..##..........#.#..#........................#....#.................................#.............................................# +............................................#.........................................................#..#...................#...# +.......................................#......................................................................#................... +....#.......................#...#.........................................................................#................#...... +...........#...........#............#.....#...#...................#........................................#.................##... +....#...........................................................................................#................................. +..............................................................................#.#.............................................#... +.........#..#..............................................#...................................................................... +#...................#........#............#.......#.....................#................#.#.#............#..................#.... +.......................#................#.....#...........#.....................#...........................#..........#.........# +........................................................................#.#.....................#................................. +...#.................................................#..........................#........#.......................#................ +............................#...........#.......#.................................#.##...........#............................#... +...##.....................................#.........................#...........#...................................#............. +#.#..#................................#.........#..#.#..........#......................#............#......................#...... +....#.............................................................#............................................................... +...............#.#......#........................................................................#.........................#...... +................#.....#...#...............#.........................................#....#...........................#.......#.... +...............#.........................................................................#........................................ +..........................#..##................................................................................................... +.##....................#..#.....................#..............................................#........................#......... +............................................................................................#............#........................ +#...................#...#......................#...............#............#....................................#................ +...........................#........#......................................................................#...................... +................................#...#...#.#.......#.................#.......#................##................................... +........................#.......................#................................................................................. +#...#...............#.........................................................................#.......................#........... +.........#...................#...............................................#...................##..............................# +.....#............................................................................................................................ +.#.......#...............................................................#.......................#................#..........#.... +....................................#..........#......#...........................................................#.#.#..#...#.... +......................#..............................................##..................#........................................ +....#......#...................#................#..............................................................................#.. +...........#.................##.#...................................................#..#................#......................... +......#.....#......#...#........#.....................................#....#...........#................#..........#.............. +#.......##.....#........#...........................................#...#..........#.............#........#.......#............... +......................#......#...................#.........................#........................#..................#...#...... +.....#...........#...#.........................##.......#.....................................#................................... +....................................................................#............................................................. +........#...............#......................................................................................................... +.......................#.........................#..........................................#.#....#..#....#...................... +........................##............................................................#.....#.........#....#......#..#............ +........................#........#..................#................#.#.##............#.......................................... +.......................................................#.........................#...............##...................##....#...#. +........#...#.............#...........#........#......................#...#.........#..#..................#.#............#........ +#.#....................#..........................................#...................................................#........#.. +...............................................#............#.....#.#...........#........#.#....................................#. +......#..........................................#............................................................#..........#...#...# +....................#......................................#....#..............................#....................#............. +......................#.#.....#......#....#..#.................................................................................... +.....#..........................................#.........#...................................#................#.......#.......... +........##....................................................................#...................................#.............#. +#.........................................#..#....#................#.#.....#....................#............................#.... +#....#..#..........#..................#......................#.................................................................... +...................#..#....#...................#...........................#.#..#............#......#................#.......#.#.. +...............#......................................................#......#..#..............#.....................#............ +............#.....................#.........#....#...............#.......#...#...............#.......................##.#......... +....#............#..................#....#.......................#..............................................#...#............. +..........................................#....#..........#..#....................#......#......................#...#............. +............#..........#...........#......#..#......................................................................#............. +...#.................#.............................................................#.............................................. +.....#.#.........#....................................................#..................#...............#........................ +..#......................##.....#.........#..............#..#............#..#............................................#........ +...................#.....#.........#........#....................#..................................#................#............ +....#.............................#.............................#.......................#.......................#.....#........... +...#......#....#...........#............................#.....#........#......#................................................... +........#..........#............................#..#..#.......................................#....#.##........................... +.......#......##........#.................#.............................................................#....#........#........... +.....................#............................................#.#......#.....##....#........#................................. +...........#.....................##.#....#..#..................................................................................... +....................#......#................................#.....................................................#.......#..#.... +.....................................................#.........#.......................................#.....##..#................ diff --git a/src/Day4/Matrix.hs b/src/Data/Matrix.hs similarity index 71% rename from src/Day4/Matrix.hs rename to src/Data/Matrix.hs index 1a6f826..3e56d4a 100644 --- a/src/Day4/Matrix.hs +++ b/src/Data/Matrix.hs @@ -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 diff --git a/src/Day4.hs b/src/Day4.hs index a9831ea..b5f939a 100644 --- a/src/Day4.hs +++ b/src/Day4.hs @@ -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 = diff --git a/src/Day6.hs b/src/Day6.hs new file mode 100644 index 0000000..d73c901 --- /dev/null +++ b/src/Day6.hs @@ -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