From: Neil Smith Date: Thu, 22 Dec 2022 20:24:52 +0000 (+0000) Subject: Day 22, initial version X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=ea35652beea3aa2e32ae15c13388d893b0044e1a;p=advent-of-code-22.git Day 22, initial version --- diff --git a/advent-of-code22.cabal b/advent-of-code22.cabal index 5fa92ad..1b1a610 100644 --- a/advent-of-code22.cabal +++ b/advent-of-code22.cabal @@ -205,4 +205,8 @@ executable advent21 import: common-extensions, build-directives main-is: advent21/Main.hs build-depends: text, attoparsec, containers, lens - \ No newline at end of file + +executable advent22 + import: common-extensions, build-directives + main-is: advent22/Main.hs + build-depends: containers, linear, lens, mtl diff --git a/advent22/Main.hs b/advent22/Main.hs new file mode 100644 index 0000000..93af269 --- /dev/null +++ b/advent22/Main.hs @@ -0,0 +1,264 @@ +-- Writeup at https://work.njae.me.uk/2022/12/19/advent-of-code-2022-day-18/ + +import Debug.Trace + +import AoC +import Prelude hiding (Left, Right) +import qualified Data.Map.Strict as M +import Data.Map.Strict ((!)) +import Linear hiding (E) +import Control.Lens +import Data.Ix +import Data.Maybe +import Data.List +import Data.Char +import Control.Monad.Reader + + +type Position = V2 Int -- r, c +_r :: Lens' (V2 Int) Int +_r = _x +_c :: Lens' (V2 Int) Int +_c = _y + +data Cell = Tile | Wall + deriving (Show, Eq) + +type FieldMap = M.Map Position Cell + +data Direction = Right | Down | Left | Up + deriving (Show, Eq, Ord, Enum, Bounded) + +predW, succW :: (Eq a, Bounded a, Enum a) => a -> a +predW a + | a == minBound = maxBound + | otherwise = pred a +succW a + | a == maxBound = minBound + | otherwise = succ a + +data PathElement = Forward Int | Clockwise | Anticlockwise + deriving (Show, Eq) + +data Person = Person {_position :: Position, _facing :: Direction} + deriving (Show, Eq) +makeLenses ''Person + +data Field = Field { getMap :: FieldMap, whatsAheadFunc :: Person -> FieldContext Person, whatsAtFunc :: Position -> FieldContext Cell} +type FieldContext = Reader Field + +data Face = A | B | C | D | E | F + deriving (Show, Eq) + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- readFile dataFileName + let (field, instrs) = successfulParse text + print $ part1 field instrs + print $ part2 field instrs + -- print $ probeAllCorners field + +part1 fieldMap instrs = passwordOf endPerson + where field = mkFlatField fieldMap + startPos = V2 0 $ fromJust $ minimumOf (folded . filteredBy (_r . only 0) . _c) $ M.keysSet fieldMap + startPerson = Person startPos Right + endPerson = runReader (walk startPerson instrs) field + +part2 fieldMap instrs = passwordOf endPerson + where field = mkCubeField fieldMap + startPos = V2 0 $ fromJust $ minimumOf (folded . filteredBy (_r . only 0) . _c) $ M.keysSet fieldMap + startPerson = Person startPos Right + endPerson = runReader (walk startPerson instrs) field + +-- probeCube fieldMap startPos startDirection = endPerson +-- where field = mkCubeField fieldMap +-- startPerson = Person startPos startDirection +-- endPerson = runReader (whatsAheadCube startPerson) field + +-- probeAllCorners fieldMap = [(p, probeACorner p field) | p <- persons] +-- where persons = [ Person (V2 r c) f +-- | r <- [0, 49, 50, 99, 100, 149] +-- , c <- [0, 49, 50, 99, 100, 149, 150, 199] +-- , f <- [Right, Down, Left, Up] +-- , (V2 r c) `M.member` fieldMap +-- ] +-- field = mkCubeField fieldMap + +-- probeACorner person field +-- | Debug.Trace.trace (show person) False = undefined +-- | otherwise = runReader (whatsAheadCube person) field + + +passwordOf :: Person -> Int +passwordOf person = 1000 * (person ^. position . _r + 1) + + 4 * (person ^. position . _c + 1) + + (fromEnum $ person ^. facing) + + +mkFlatField :: FieldMap -> Field +mkFlatField fieldMap = + Field { getMap = fieldMap + , whatsAheadFunc = whatsAheadFlat + , whatsAtFunc = whatsAt} + + +mkCubeField :: FieldMap -> Field +mkCubeField fieldMap = + Field { getMap = fieldMap + , whatsAheadFunc = whatsAheadCube + , whatsAtFunc = whatsAt} + +whatsAt :: Position -> FieldContext Cell +whatsAt posiiton = + do fieldMap <- asks getMap + return $ fieldMap ! posiiton + +whatsAheadFlat :: Person -> FieldContext Person +whatsAheadFlat person = + do let easyNext = (person ^. position) + (deltaOf $ person ^. facing) + fieldMap <- asks getMap + if easyNext `M.member` fieldMap + then return $ person & position .~ easyNext + else do let currenFacing = person ^. facing + let currentRow = person ^. position . _r + let currentCol = person ^. position . _c + let rightMovingCol = fromJust $ minimumOf (folded . filteredBy (_r . only currentRow) . _c) $ M.keysSet fieldMap + let leftMovingCol = fromJust $ maximumOf (folded . filteredBy (_r . only currentRow) . _c) $ M.keysSet fieldMap + let upMovingRow = fromJust $ maximumOf (folded . filteredBy (_c . only currentCol) . _r) $ M.keysSet fieldMap + let downMovingRow = fromJust $ minimumOf (folded . filteredBy (_c . only currentCol) . _r) $ M.keysSet fieldMap + return $ case currenFacing of + Right -> person & position . _c .~ rightMovingCol + Left -> person & position . _c .~ leftMovingCol + Up -> person & position . _r .~ upMovingRow + Down -> person & position . _r .~ downMovingRow + + +-- A B +-- C +-- D E +-- F + +whatsAheadCube :: Person -> FieldContext Person +whatsAheadCube person = + do let easyNext = (person ^. position) + (deltaOf $ person ^. facing) + let currentFace = faceOf (person ^. position) + let nextFace = faceOf easyNext + fieldMap <- asks getMap + if (easyNext `M.member` fieldMap) && (currentFace == nextFace) + then return $ person & position .~ easyNext + else return $ crossEdge person currentFace + +faceOf :: Position -> Face +faceOf position + | (inRange (0, 49) r) && (inRange (50, 99) c) = A + | (inRange (0, 49) r) && (inRange (100, 149) c) = B + | (inRange (50, 99) r) && (inRange (50, 99) c) = C + | (inRange (100, 149) r) && (inRange (0, 49) c) = D + | (inRange (100, 149) r) && (inRange (50, 99) c) = E + | (inRange (150, 199) r) && (inRange (0, 49) c) = F + | otherwise = error "Not a face" + where r = position ^. _r + c = position ^. _c + +crossEdge :: Person -> Face -> Person +crossEdge person face = + case (d, face) of + (Up, A) -> person & position . _r .~ (interpol c 150 199) & position . _c .~ 0 & facing .~ Right + (Right, A) -> person & position . _c .~ 100 + (Down, A) -> person & position . _r .~ 50 + (Left, A) -> person & position . _r .~ (interpol r 149 100) & position . _c .~ 0 & facing .~ Right + + (Up, B) -> person & position . _r .~ 199 & position . _c .~ (interpol c 0 49) + (Right, B) -> person & position . _r .~ (interpol r 149 100) & position . _c .~ 99 & facing .~ Left + (Down, B) -> person & position . _r .~ (interpol c 50 99) & position . _c .~ 99 & facing .~ Left + (Left, B) -> person & position . _c .~ 99 + + (Up, C) -> person & position . _r .~ 49 + (Right, C) -> person & position . _r .~ 49 & position . _c .~ (interpol r 100 149) & facing .~ Up + (Down, C) -> person & position . _r .~ 100 + (Left, C) -> person & position . _r .~ 100 & position . _c .~ (interpol r 0 49) & facing .~ Down + + (Up, D) -> person & position . _r .~ (interpol c 50 99) & position . _c .~ 50 & facing .~ Right + (Right, D) -> person & position . _c .~ 50 + (Down, D) -> person & position . _r .~ 150 + (Left, D) -> person & position . _r .~ (interpol r 49 0) & position . _c .~ 50 & facing .~ Right + + (Up, E) -> person & position . _r .~ 99 + (Right, E) -> person & position . _r .~ (interpol r 49 0) & position . _c .~ 149 & facing .~ Left + (Down, E) -> person & position . _r .~ (interpol c 150 199) & position . _c .~ 49 & facing .~ Left + (Left, E) -> person & position . _c .~ 49 + + (Up, F) -> person & position . _r .~ 149 + (Right, F) -> person & position . _r .~ 149 & position . _c .~ (interpol r 50 99) & facing .~ Up + (Down, F) -> person & position . _r .~ 0 & position . _c .~ (interpol c 100 149) + (Left, F) -> person & position . _r .~ 0 & position . _c .~ (interpol r 50 99) & facing .~ Down + + otherwise -> error ("Crossing illegal boundary " ++ show (person, face)) + + where r = person ^. position . _r + c = person ^. position . _c + d = person ^. facing + interpol x start end = (signum (end - start)) * (x `mod` 50) + start + + +walk :: Person -> [PathElement] -> FieldContext Person +walk person [] = return person +walk person (step:steps) = + do person' <- walkOne person step + walk person' steps + +walkOne :: Person -> PathElement -> FieldContext Person +walkOne person (Forward n) + | n == 0 = return person + | otherwise = + do whatsAhead <- asks whatsAheadFunc + person' <- whatsAhead person + whatsAt <- asks whatsAtFunc + nextCell <- whatsAt (person' ^. position) + if nextCell == Wall + then return person + else walkOne person' (Forward (n - 1)) +walkOne person Clockwise = return $ person & facing %~ succW +walkOne person Anticlockwise = return $ person & facing %~ predW + +deltaOf :: Direction -> Position +deltaOf Right = V2 0 1 +deltaOf Down = V2 1 0 +deltaOf Left = V2 0 -1 +deltaOf Up = V2 -1 0 + + +-- Parse the input file + +successfulParse :: String -> (FieldMap, [PathElement]) +successfulParse text = (mkField $ takeWhile ((> 0) . length) $ init $ lines text, mkInstructions $ last $ lines text) + +mkField :: [String] -> FieldMap +mkField rows = M.fromList + [ (V2 r c, mkCell r c) + | r <- [0..maxR], c <- [0..maxC] + , isCell r c rows + ] + where maxR = length rows - 1 + maxC = (length $ head rows) - 1 + -- isCell r c = ((rows !! r) !! c) `elem` (".#" :: String) + mkCell r c + | cell == '.' = Tile + | cell == '#' = Wall + where cell = (rows !! r) !! c + +isCell r c rows = isRow && isCol && ((rows !! r) !! c) `elem` (".#" :: String) + where isRow = r < length rows + isCol = c < (length $ rows !! r) + +mkInstructions :: String -> [PathElement] +mkInstructions [] = [] +mkInstructions text@(t:ts) + | isDigit t = mkWalk text + | otherwise = mkTurn text +mkWalk text = (Forward $ read digits) : (mkInstructions remainder) + where (digits, remainder) = span (isDigit) text +mkTurn (t:ts) + | t == 'R' = Clockwise : (mkInstructions ts) + | t == 'L' = Anticlockwise : (mkInstructions ts) diff --git a/data/advent22.txt b/data/advent22.txt new file mode 100644 index 0000000..ac7395d --- /dev/null +++ b/data/advent22.txt @@ -0,0 +1,202 @@ + ...###..#...##................#......#....#........#.....#..#.......#...#.......#.......#..#........ + ..........##.........#.#.........#...#....#....#...........#...............#...................#.... + ..........................#.#..............##......#.........#.#......##......#.#................... + ...................#...........#........#.#....#.................................................... + ....#................#..................#.....#....#...........#......#..#..#.........#......#...... + ......................##..#.....................#..##...#..........#.......#.#...#.......#..#......# + ........#.....#..............#........................#........#......#...#...............#......... + #............#..#............#....#....#...#...#.......#........................#................... + .............#.##....#...#..#..#...........#.....#...#..............#.#.#..#........................ + ..........#........................#.................#..#.....#.......##..#......................... + .#.##....#..#............##.................................##.............................##....... + ...###....#...#....#.........#.#.....#.........#..#........#..........#........#....#....#......#... + .##...................#....................#.............##....#............##.......#..#........... + ...............#.......................#........#.................#..##...........#.......#........# + .....#..............................#..................##.#.............#..................#...#.... + ....#....................................#.......................#...#.....#........#............... + ..............#.......................#...........#..............#...............#..#..#....#....... + ......#.................##...........#...........#........#............#...#.#..............#..#...# + ..............#...#...............#......#.......#...............#..............#...............#... + ..........##................#...#..#.......#......#...................#..#...........#............#. + ............................#.........#.....#....#.....#.....#......#..#...#.....#...#.......#...... + ...##.................#...#....#.#.......#.....................#......#...##..................#..... + .#......#...#..............................................#.........#..................#........... + ...........................#.#.#.................#.....#.............#.......#..##.................. + .............#.......#.............#......#..............#.#...............#........................ + ...................#...#...#....#.....#....................#.......#.............#........#......... + ......#....#....#........#...........#..................#....#..........................##....#....# + .........##..##.#.#......#.#.....#.....................#.#.#..........#....##................#..#... + ...#...#.........#..........##.#.............##.#.....#...............#........#.................... + .................................#..#...............#.......................#...................#... + ..#.......#...#.......#.....#.........................#.#....#..#...........##..#................... + ...#...#..............#.......#...#..................#........#...................#...............#. + ....#..#....##..#....#....#...........................#............................................. + ...#...#.......................#..#...#..#..#................#........#............................. + ..........#.......................##...................#.................#......................##.. + ....#.#........#....#..#....#........#......#.....#...........................#...............#....# + ...................#..................................#........................#.................... + ..........#.#...#.............##.........................#........#...................#............. + .................#.......................................................#.............#............ + ...#..#...#....#..................#..................#.........................#................#.#. + ..#..#.........................................................................................#...# + .....##.............................#....#.....#.......#.......#.#..............#............#...... + .#..........#.....#.......#........#....#..............#................##.......................... + ..........#...##...........#.........................#...........#............#.....#.#.#........#.. + ........##.#.................#..#...................#...........##..#.......................#....... + ..#.............#...#...#...........#.....#..................#..........#......................#.... + ..#................###..................#..............#.....................#......#............#.. + ...........#.........#.....#.#.#.#................#.............#.#.................#.....#....#.... + .......#............#.......#......##.....#.......#.....#.###...............#......#.#.............. + .........#.....#.......#....#........#.......##....#................................#............... + ..............##..#...#.#.....................#... + ............##.........#.......................... + ..#.........#......###..#........#...#............ + ...##.#......#.................................... + .................#....#.#....#............#.#....# + .##........#...#........#..........#.............# + ................#..#.......#.......#.....#........ + ...##........#...#.#............#....#...........# + ............#...#...............#.#.......#....... + .##............................................... + ..........###..................#..#.#....#...#.... + ......#...........#....#..#..#........#........... + ....#.....#...#..#.#..#................#.......... + ........#...#......#...................#.#......#. + .#....#......................#..#...#.........#... + ..............#............#..............#....#.. + ...................#...........#.#........#.#.#... + ............#.................##.................. + ..#..........................#.#...#.......#..#... + ..#.....#.........#.........#..#...#......#....... + .........#..........#.#.....#......#...........#.. + .....#...........#.#........#......#.......#....#. + .#.........##...#...#......#.......#.............# + .........................##......#.....#........#. + ...#......#.#......#.............................. + .........#...........#...........#...#............ + ##.............#..................#.#............. + #.......................#.#.....#........#........ + .....#...........#....................#..........# + ........#.#..........#...................#........ + ......#.......#..........#...................#..#. + #....#................#............#...........#.. + ..#...##.#..............................#......... + ......##.....#.............#................#..... + ....#.....#..............#......#..#..#.#........# + #.........#...............##....#.#........#...... + ....................#.......................#..... + .....................................##........... + ........................#......................... + ..##....#.......#................#.#..........#... + .......#.............#.............#....#...#.#... + ....#.#......#.......##.#....#........#........... + ..#.....#..#.#.................................... + ...........................#.....#......#......#.. + .........#.....#........#........................# + .......#....#........#...#............#.#......... + ...#...#..............#...#...............##...... + ..#.......................................#....... + .................................................. + .........................#..#.............#....... +....#.....#....#......##........#......#..............#.........#........#..............#....#...... +...............##...##..............#......................#................................#....... +......................................#..........#..#.#...#......#..................#............... +....#......................#........................#.............#.......#...............#..#...... +..............................#....###.............#............................##...............#.. +...........................#...#...................#......#......#...........#...............#....#. +..........##............#................#....#................#.......#..........#....#............ +#.........#.......#..............#.............#....#.#...#................#.#.....................# +...#...............#...............#.............................#......#...#.......#......##.....#. +..............#...............#...#..........................#..........#......#.....#.............. +................#........................#.........#..........#..........#.............#....#....... +......#................#....#.....................#..#............#...#..#.....................##.#. +...........#....#.....#.....#..#..............#.........#...#...#......#.....................#...... +.....#...........#...........#...#.....#.##....#..#.......#........###.#...#..#..........#.##....... +##......#.................#.#........#.............#.........#...................................... +................#....#.#...........................#...................#.#.............#.........#.. +#......#......#...................#.#.#.#............#......#.................#.....#............... +##.......#..............#....................................#.....#.........#........#............. +.#.....#...........#..#................#.....#..#........#........#............#.....##..#.......... +.##........#......................#......#...........##............#...........................#.... +...#..................#........#..#....##.........##........#...#......##..##.......#..........#.... +.......................#..............#.......#..#......#.#......#...#........#.................#... +.#....#...........................#..#..............##......#.#......#................#............. +...........#.........................#.#................#..................#........................ +.......#..............#..............#.....#...........#......##........#.........#...........#..... +#..#.......................#.......###......#...##..................................#........#...... +...#...........#..........#..........#.....#...........................#.....#.....#........#.....#. +..................#.#........................#...#...........#...#...................#........#..... +.#..........#.#....................#..#...............#.......#...#.......#...#........#..........#. +............................#.................#.#..#...........#..............#..................... +.............#........#.#.#............#...............#...........#......................#......... +#..........#.......#......#................#.......#.........#.#......#......................#...... +.......#.#.............#...................#....#...............##......#...............#........##. +#.........#........................................#...#................#..#........................ +.............#.#........#.........#....#..................#.........#..#..............#.#.....##.... +...#..........#.............#...#....#.....#....................#....................#.............. +.......#................#...........#.............#.#....................#........................#. +..................................#.#......#....#................................#....#............. +....#..#...#..#.#.......#...........#.................................................#....#........ +#.....#..#..............#..#.....#.........#...#.....#....#......#..................#........#...... +.......#.#............................................#.#......#.#..................##...#..#.....#. +........................#.........................#...............................#................. +..............#..#............................#.....#...#...............#...#...#................... +.....#.#................................#..##...............#.....#.#.#..#.............#.#.......... +#...........................#....#......#.....#.......#..#..#..................#.............#....#. +...#.....................................#.......................................................... +......#....................................................#....#.......#..#......................#. +#............................#..........#...........................................#.##..........#. +.....#...#.............#..#...............................#..............#....##.#..#.#............. +..................................#................#...#......#...................#..........#...#.. +...........#..........................#.......#... +.......#...#..#........#.......................... +........#..##.##..#.............##..#...........#. +..#.#.......##..#..#.............#.............#.. +......#.....#.........#...##.#...#..#............. +............#..#.............#......#............. +.........................##.............#......#.. +.#..............................##................ +.#.......................#................#....... +....#.#...........#............................... +.............................#.........#.......... +...............#..#.#.....##...................#.. +...#..........................#.............#....# +................#.............#............##..... +#.......##..#..#....................#............. +.........#............#..#.....................#.. +........#...........#...#...#.............#....... +........#........#..#...#..#..#...#............... +....#....#...#.......#...........................# +......#........##....#.#.#........................ +............#....................................# +......##...................#............#.....#... +.........#..#.#.#..........#..........#..........# +#...........#...#..........#.##............#...... +.....#..#.....#......#.....#............#......... +..........................................#....... +...........#..#............#....................#. +......#.....#.....#...#.#......................... +....#.....#....#........##...........#............ +................#.......#....#.............#...#.. +..............#..#.#.#..................#........# +.............#........#.......................#... +........#......#.......#.#....#..#................ +.......................#..........#.......#.#..... +......#..........#...###.......#.......#.#........ +...............#..#..#..#...#....................# +..#.........#........#.....#........#............. +...#.............#....#...........#............... +.......#......#...##............................#. +....................#..##..#..#..#..............#. +......#........................................... +......#........#.......#.........#....#....#...#.. +..............#..#.#........................#..#.. +.................#.........................#.#.... +............#................#.................... +..#.....................#......................... +............#..............#...................... +..................#...................#...#....... +......#.##.......#.........................#...... +.........#........#...............#.....#......... + +9L44R12L3L7L37L27L37R32R15R42R15L41R50R41L47R26L44R26L3L20L2L9L13L45R19L9L26L5R27L44L3R38L38L29R50R28L6R26L40L36R46L22L43L47R16L44R17L44L46L2R25R17R7L35R24L26L24L35R42R28L14R34L4L16R15L23R17R43L34R13R1R35R25R35L21L22L45R50R47R42R12L30R46R31R5R41L27L2L13L38R46L48R11L48L44R17L42L8R48L44R10L38L43L15L26L27L32R24R21R34L47L30R38L36R4L32R44L45R37R42L46R16L30R23L29L25L4L37L29R18R39L44L8R10R29R19R5L5L34L44R1L4R41L26L36R25L48R25L50R32L47L39R39R8L48R43L41L12L43L32L36L14L7R27R28R43R33L5L9R24L30R27R47R30R36R36L6L6R8R2L18R10R42R25L36L33L35R6R26L48R13L14L41L6R19R8L37R19R14L23L47L34R46L11R33L29R5R18L2R14R31R4R15L27L42R50L34L4L47R17L28R6L4R11L25R39L24R2R31R6R12L50R29R1L29R32L2L22L28L35R27R3R22L7R47R47L43R29R29L41L5R44L20L6R41L33L14L39R13R16R8R3R46R39L9R20L43L32L39R43L5R43L34L47R9L8L36L50R30L48R17L4R12L36L36L29R48R11R39R24R27L40L47R31L34R35R4R14R47L38R15R18R16L22L38L11R6R31R8R27R50L26R9L37R24R6L19R6L25R42R6L21L40L10L10L47L22R44R8R26L15R49R30L5R12L36R41R22L4R44L47L5L43L2R18L24L37L28R18R9R32R45R24L6R44R33L48L17R21R21L32R35R38L38R28L45R2L41L24L16L10L37R14R25R44R25L43L10R33L39R16R12R43L35L9L50L2R18L21L3L15R21R4R33L21R12R24L22L15L18R48R36R9R14L18R31L39R8R28L27R48R13R5R9R8R6L30L25L14L26L38L8R24L10L25R6R8R45L36R48R16R11R14R2R36R45L12L27L39R50L26L44L39R41L30R48L27R25R40L28R43R32R46R43R34L40R45L42R5L7R37L44L39L26L42R37R3R38R42R47R16L15R30R18R24R26L46R25R40R44R1R17R4L41R44R44L38L33R48R37L28L23L19R47R28R3L10R6R4R10R17L28R47R21R15R17L29R42L37L44L27R20R31R7L16R33R6R12L40L33R2L25L7L32R16L21R1R36R4L24L15R34R31L10L28R3R41L49L4L49L45R37R19L47R12R27L20L1L7R13R18L17L13L11R1R13L41R34L11R18L34R18L18R44L5L33R42L25R22L6R40R29L17R45R26R3R12R44R49L20R30L32R8R20R8R26L31L24R35L50L33L40L6L4L35L44L45R12R48R13R3L21R9R31R6L15R23L9R12R48R23R11L16R4R40R20L13R35R21L3L30L26L5L10L15L31L23L47R49R10L25L46R2R35L14R23R41L6L3R3L7L49R2L34R42L46L45L37L2L15L4R33R27R30R40R25L43L34L32R8R40L36R32R16L18R32L48R19L12R14L36L16R9R34L44L30L1R7L18L48L12L40R40R14R25R16R36L16L26R47L27L33L7L43L35R40L41L30L19L5R8L49R48R27R19R41L39L36L50L27L49L10R11L34R46L5L28L12L17L47L26L33R18R43R3L2R34R46L39L35L38R35R26R34R8R15R11L44R44L3R19L14R15R42R36L32L35R20L6L25L22L32L24L24R9L4L15R28L17R13L44L17L13L40R36R10R27L35R22L48L38L33R34L26R29L34R43L5L28R49R17L2R13L9R20L26R8L18R40R23L17L25L11L33R15L48L18R11R22L46R15L47L33R31R47R22R10R47R12L48L15R27L44L6R44R36L43R30R39R28R25R32L34R44R29R26R1R11R50R43L42L14R46R3R5L6L38L24R12R49L26L29L40R24L49R9R43L36L33R48R38R33L19L7L21R37R7L32L50R6R15R30R5L14L47L22L30L4R18L36L11L42L3L3R41L48L30L40L6R20R26L45R37L18L41R39R31L18R10R44R9R37R22L22R50R7R20R7R38L34L7R34R14R16L46R35L6R7L50L2L4L5R5R48R29L44R4R2R25R7L18R46L11R8R33L17L47R19R48R11R29L22L11R14L19L13L3L42R38R33R20L44L1R12L18L9L37R3R10L5R49L45R40L10R41L4R9R41R25L40L9L18L45L21R25R2L49L45R39R37L42R24R3L6R38R16R48R36L47L37R49R8R12R27R44R47L28L34L17L46R8L31R16L19L14R28L39R28R40R36R22L45R10L41R22L16L24R7L7R29L5L11R14R6R41R44L49R27R43R42R44R16L20L47R4L12L1R26R9R10R46L19L23R30L29R25R39R7L46R24L34L38R23R10L33L19R32R35R29L13R9L21L32L39L40R18R44L15R22L27R42R7R4R47R26L26R50R20L36L42R36R39L10L26R2R40L5L44R32R12R32R2L24R50L22L37L34L47R10L32R19L36L42L26L12R21L47R44R17L13R23R3R4L12L34L47R13L45R3L28R16R35R6L33R49R36R3R17R43L19R10L39L5L18R19R45R23R23L26L32R11L33R43R45L30R9L11R38L44L38R43L44L39R7L41R6L10R22R41R20R27L22L35L34R10L42R13R5L45R15L20R48R50L16R48L6L24L22R7R23L11R37L9L32L42R30L28L11R1R7R50R2R36L26R13R45R49R19R25R49L10L5R18L50L13R31L41L38R43R14L5R25R22R28L42L8R28L22L21L24L17L27R43L7L30R39R31R48L47L19R13R42L50R17L9L47R19L18R18L42L22R23L41R46L17R20L45R28R45L47R16R12R34R49L31R20L18L6R16R9L32L42L3L40R41R29R22R7R14R37R39R23L20L22L32R12L11L35L50R19L34R11L48L2R11L30L48L19R11R34L32R23R31L1L13R26L3R23R22R36L34R33R43L2R27R48L38R18L50L42L41L19L37R12R41L15R50R36L27R50R31R28R34L29R43R23L30R8R19L46L19R16L18R14L43L45L30R46R18R1R37R15L46L36L22R14R25L5L21L24R48L7R41L32R41R30L6R6L13R34R13R37L47L3L37R33R23R47L49L13R10L12R9L45R44L11L15R26L16L19L48R35R37L29L26L43L19L26R44R45R49R11R38R7R43R37R41L47L47R42L28L24R16R14R40R25R24L1R2L40L11R14L5R4R43L4R4L27R24R13L17L14L25L42L37L47L47L26R11L24R40R8L42R48L9R27L41R37L17R20L31L44R37R39R35R26L12L47L27L7R7L43R35L17R12R25R49L33R16L21R27L20R18R49L20L45L41L50L39R8L35L41L43R36L20R28R15L43L32L25L15R7R42L44L33R45R14R17L45R2L43L29R50L21L46R33L19R18L23R37R3L24L21R18L50R3R16R9L47L33L43R7R42R10L50R4R16R50R22R2L19L42R43L15R11R39R42L45L26L25L12R35L45R12R22R14R37R17R1R19L7R20R50R21R33R31R48L20R4L31L38L28L35L37R5R33L20L28R5R48L12L8R12R43L26R30L7R41L18L41R27L38L5R45L7R22L32L49R6R7R3L31L13R12R2R31L46R12L31L10R4R12L48L33L41R22R25L22R15R17R12L45R36R30L10L46R10R29R23L5L19R18L27L48R36L41R44L1R41R5L23R11R25R50R41R42R50L26L28R31R3L33L25L34L42R3R35R1R50R16R22L10L33R31L41L5L49R4L47R45L4L3L44R16L35L26R27R17L21L43L18R33R15R33R49L49R21R26R37R39L37L23L23R12L49R44L12R39R16L22R12R32L38R11R14R28R6L13L26R12R9L46L34L25R6R5L19L13L18R10R28L8R27L26L15L38L23R35L21R47R7R1R18L10R44L31L43L45R49R20L14R35R15R13L45R30R36L35R42L8R11L1L49L5R14L17R5L33R40L35R43L43R46R40L2R36L47L19R11R1L21L36R2R7L3R48R31R12R19R29L22R47L40R38L7R24L42L5R7R22L43L11L37R38L18R6R1L6L29R39L12L13L29R27R31R24R46L29L28L10L42L37L16L25R4L22L22R10R16L18L42R28L33L14R46R19R42L18R5L11R27L47R10R47L11R1L31L36R24L8R39R21L40L48L39R17L8L45L40L30L39R25L1L11L23L37R10R48L6L48R23L44L46R23R2R44R19R35L35L34L37R24L1R12L38R44L38L3R35L37R7R7R50R21R20R9L7R37R48L23R9L32R11R27R30R20R3R4L21L16R16R6R32R37R22L33L3L19R27L42L24L19L3L11R36R22R20L1R47R22R42L3L26R41R15L44L47L33L35R44R13R39L30R50R45R15L24R45L8R6L33L22R12L23R46R16R45L20R5R21R12L46L45R37R26L19L22R47L41R16L36L5L40L25L20L50L46L36R3L13R14L22R8L8R37R26R47L26L19L6L24R3L41R17R39L20L5L29L27R10R37L26R24L30L41R42L37R20R7L7R49L9L4L19L48R16R7R38L37R48R40R1R1L15R8L22L16R44L38L20R13R9R19L20R28R5R40L2R22R1 diff --git a/data/advent22a.txt b/data/advent22a.txt new file mode 100644 index 0000000..6e06946 --- /dev/null +++ b/data/advent22a.txt @@ -0,0 +1,14 @@ + ...# + .#.. + #... + .... +...#.......# +........#... +..#....#.... +..........#. + ...#.... + .....#.. + .#...... + ......#. + +10R5L5R10L4R5L5 \ No newline at end of file diff --git a/problems/day22.html b/problems/day22.html new file mode 100644 index 0000000..f6c1d3e --- /dev/null +++ b/problems/day22.html @@ -0,0 +1,243 @@ + + + + +Day 22 - Advent of Code 2022 + + + + + + + + +

Advent of Code

Neil Smith (AoC++) 44*

       Î»y.2022

+ + + +
+

--- Day 22: Monkey Map ---

The monkeys take you on a surprisingly easy trail through the jungle. They're even going in roughly the right direction according to your handheld device's Grove Positioning System.

+

As you walk, the monkeys explain that the grove is protected by a force field. To pass through the force field, you have to enter a password; doing so involves tracing a specific path on a strangely-shaped board.

+

At least, you're pretty sure that's what you have to do; the elephants aren't exactly fluent in monkey.

+

The monkeys give you notes that they took when they last saw the password entered (your puzzle input).

+

For example:

+
        ...#
+        .#..
+        #...
+        ....
+...#.......#
+........#...
+..#....#....
+..........#.
+        ...#....
+        .....#..
+        .#......
+        ......#.
+
+10R5L5R10L4R5L5
+
+

The first half of the monkeys' notes is a map of the board. It is comprised of a set of open tiles (on which you can move, drawn .) and solid walls (tiles which you cannot enter, drawn #).

+

The second half is a description of the path you must follow. It consists of alternating numbers and letters:

+
    +
  • A number indicates the number of tiles to move in the direction you are facing. If you run into a wall, you stop moving forward and continue with the next instruction.
  • +
  • A letter indicates whether to turn 90 degrees clockwise (R) or counterclockwise (L). Turning happens in-place; it does not change your current tile.
  • +
+

So, a path like 10R5 means "go forward 10 tiles, then turn clockwise 90 degrees, then go forward 5 tiles".

+

You begin the path in the leftmost open tile of the top row of tiles. Initially, you are facing to the right (from the perspective of how the map is drawn).

+

If a movement instruction would take you off of the map, you wrap around to the other side of the board. In other words, if your next tile is off of the board, you should instead look in the direction opposite of your current facing as far as you can until you find the opposite edge of the board, then reappear there.

+

For example, if you are at A and facing to the right, the tile in front of you is marked B; if you are at C and facing down, the tile in front of you is marked D:

+
        ...#
+        .#..
+        #...
+        ....
+...#.D.....#
+........#...
+B.#....#...A
+.....C....#.
+        ...#....
+        .....#..
+        .#......
+        ......#.
+
+

It is possible for the next tile (after wrapping around) to be a wall; this still counts as there being a wall in front of you, and so movement stops before you actually wrap to the other side of the board.

+

By drawing the last facing you had with an arrow on each tile you visit, the full path taken by the above example looks like this:

+
        >>v#    
+        .#v.    
+        #.v.    
+        ..v.    
+...#...v..v#    
+>>>v...>#.>>    
+..#v...#....    
+...>>>>v..#.    
+        ...#....
+        .....#..
+        .#......
+        ......#.
+
+

To finish providing the password to this strange input device, you need to determine numbers for your final row, column, and facing as your final position appears from the perspective of the original map. Rows start from 1 at the top and count downward; columns start from 1 at the left and count rightward. (In the above example, row 1, column 1 refers to the empty space with no tile on it in the top-left corner.) Facing is 0 for right (>), 1 for down (v), 2 for left (<), and 3 for up (^). The final password is the sum of 1000 times the row, 4 times the column, and the facing.

+

In the above example, the final row is 6, the final column is 8, and the final facing is 0. So, the final password is 1000 * 6 + 4 * 8 + 0: 6032.

+

Follow the path given in the monkeys' notes. What is the final password?

+
+

Your puzzle answer was 26558.

--- Part Two ---

As you reach the force field, you think you hear some Elves in the distance. Perhaps they've already arrived?

+

You approach the strange input device, but it isn't quite what the monkeys drew in their notes. Instead, you are met with a large cube; each of its six faces is a square of 50x50 tiles.

+

To be fair, the monkeys' map does have six 50x50 regions on it. If you were to carefully fold the map, you should be able to shape it into a cube!

+

In the example above, the six (smaller, 4x4) faces of the cube are:

+
        1111
+        1111
+        1111
+        1111
+222233334444
+222233334444
+222233334444
+222233334444
+        55556666
+        55556666
+        55556666
+        55556666
+
+

You still start in the same position and with the same facing as before, but the wrapping rules are different. Now, if you would walk off the board, you instead proceed around the cube. From the perspective of the map, this can look a little strange. In the above example, if you are at A and move to the right, you would arrive at B facing down; if you are at C and move down, you would arrive at D facing up:

+
        ...#
+        .#..
+        #...
+        ....
+...#.......#
+........#..A
+..#....#....
+.D........#.
+        ...#..B.
+        .....#..
+        .#......
+        ..C...#.
+
+

Walls still block your path, even if they are on a different face of the cube. If you are at E facing up, your movement is blocked by the wall marked by the arrow:

+
        ...#
+        .#..
+     -->#...
+        ....
+...#..E....#
+........#...
+..#....#....
+..........#.
+        ...#....
+        .....#..
+        .#......
+        ......#.
+
+

Using the same method of drawing the last facing you had with an arrow on each tile you visit, the full path taken by the above example now looks like this:

+
        >>v#    
+        .#v.    
+        #.v.    
+        ..v.    
+...#..^...v#    
+.>>>>>^.#.>>    
+.^#....#....    
+.^........#.    
+        ...#..v.
+        .....#v.
+        .#v<<<<.
+        ..v...#.
+
+

The final password is still calculated from your final position and facing from the perspective of the map. In this example, the final row is 5, the final column is 7, and the final facing is 3, so the final password is 1000 * 5 + 4 * 7 + 3 = 5031.

+

Fold the map into a cube, then follow the path given in the monkeys' notes. What is the final password?

+
+

Your puzzle answer was 110400.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file