From 8efeb399316493403504187e2343ab8dd64e5d1e Mon Sep 17 00:00:00 2001 From: Ben Ashton Date: Sat, 31 Dec 2022 11:14:56 -0700 Subject: [PATCH] Day 22 --- 22/input | 202 ++++++++++++++++++++++++++++++++++++++ 22/solution.mjs | 256 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 458 insertions(+) create mode 100644 22/input create mode 100644 22/solution.mjs diff --git a/22/input b/22/input new file mode 100644 index 0000000..71d33f8 --- /dev/null +++ b/22/input @@ -0,0 +1,202 @@ + ..........##..#...#...............#.........#.#..........................#.....#..#..#..#.........## + #..#......#..#...........#.##................................#......#....#........#...............#. + ..............#.#..................#................#....................##..#..........#........... + .....#...........##..#..............................#......................#................#....... + ......#......#.................##........#.......#.............#.....#..#............#.............. + #...........#....#.#.#.........#...............#............#....#.........#........................ + #................#........................##........#.....#....#.........#...#.#.................... + ..................#.............#......#..........#...................#...............#...#......... + ......#..........#.##...#..#...........#.....#...#...............##.........#.................#..... + .#..##..#..##........#...##..........#.##....#............#........................#................ + .............#..#...#........#........#.....#...#................#.........#.....#..##.............. + ....................#....#................#.........#........#.##...............#...#...........#... + ........#....#.......#..#.......#.....#.........................#......#.#..#.............#......... + .......#.......#.#....#..........#.......#........#........#..#.....#............................... + ..#....#......#.......#......#...........#......#.............#.................................#... + .#.#.........#.....................##...............##................#................#....#....... + ...................#...........#..............................##..#...........#...#................. + .................#...........##..........#............#..........##.#...#............#........#..... + ..........##...#.......#..........#....#..............#.##..........#.#....#...........#............ + #...#...#....#.....................#..................#.........#...#.........#...........###....... + ....#...........##.....#..#........#...............#............#.#...........................#..... + ...........#....#.#......#.............#............#......#........................................ + ................#..........#.........#......#...#............#..........#..#......#...........#....# + .........................#............#...#...........#..........#...#..........#.......#.......#... + ...........##...........#...##..#....#.......#....#.#.................................#............# + ......#.........#...........................#..#.......#.....##.....#........##.........#..........# + ...#..#....................#........#.#.#.#..........#.....#.#...........................#.##....... + #..#.......#........................#...............#.......#...........#................#.......... + ........#...............#...#....#...............#......##.....#......................#............. + ....................#.#......#..........#....#...#.......##......#..#....#.#...#..............#..#.. + ...##.#.#...##...........#......#.#.....................##.#....................#................... + .....................##...#....................#..#...........#..........#.#..##.....#........#...#. + ..##.#......#......#......................###.#..............#......#.#..........#.#....#..#...#.... + .#.....#.......#..#......................................#..#..#.#........#..#...................... + .#.#......#....#.............................#..............##.......##.#....#...#.................. + #...........#.#.......#..#...........#.....#...............#.........#.......#......#..#............ + ..........................................#.........#..........#.#..#...#............#....#......... + .#...............#...#...................#.....#..#.....#..........#................................ + ......#...#..#............................#............#..#.......................................#. + .......................###...........................#.....#......#..............#....#...........#. + ..#.......#.................#......##...#.............#...#........#.....#...................#.....# + .........................#.........#...............#.#...........................#.....#.......#.... + #..#....................#.#............................#....#..#.......#....#....................... + ..........#...............#......................#....#............................#...........#.... + .#..............#...#......##.#.....................................#...#..#..#........#..#.....#... + #..............................#.........#..........#...............#...#.....#...#....#............ + .........#........................................#......#........#..........#..................#... + .................#........#............#.................#............#.....#.........#..#.#.#...... + ..#............#...........#.#...##.......#.......#.##.....#...................#.#.........#.#...... + #...............#....................##.....##...........#.........#....##..............#..........# + .................................................. + ...........#....#................#................ + .#............#..............#...##....#.......... + ........#...#.......#..........#.....#.#.......... + .............................................#.... + .......#.#..........................#............. + ......#................#.......#..............#... + .......#..#................................##..... + ......#...............#....#...................... + ....###..##..................#......#....#........ + ....##..#.##......#..#.....#......#............... + .........................##........#..#........... + .................#........#...#..........#......#. + ...........#.........#.............#....#..#...... + .........#.#........#............................. + ...#.....#....#.....................#....#.....#.. + .#.......#...............#....#........##...#..... + .....#.....................................##..... + .............#......#...#......#............#..... + #..........#..#.....#..............#..#........... + .......#...........##.........#.#.....#......#.... + ...........#...#....#...................#......... + ......#..#.....#.#..................#...#...#....# + ..........................................#...#... + ............................##..............#..#.. + ......#............................#...#......#... + ...#................##......#.......#.#........#.. + .....#............#...#.##....#......#............ + ..#..#............#................##............. + #.....#......#................#.#..#............#. + ....................................#.......#....# + ...#...#..............#.........#.........##...... + #.......#.#.............#......#...............#.. + .............#.......#......#..#.....##........... + ...........................#.#.......#.......#..#. + #......#.............#.#.......................##. + .#......................#.............#......#.... + ....#.........#......................#..........#. + ......#...#...........##...#...................... + .......#...#..........#.......#.....#.....#....... + .......#.....#.......................#............ + ......#..........#..............#......#.......... + ..#..##..............#....#........#.............. + .................#..#......#.#.#....#........##.#. + #.............#...##...........#.#.#.#........#..# + .....#..#....#....##............#...........##.... + ..............#..#...#...#...............##....... + ..#........#..#............................#.#.... + #........#..........#...........#................. + .................#..#.....#..................#..#. +............#.......#.............#...#.........#....#..#.#...........##........#............#...#.. +#....................#.#....................#........#..........#.##........#.....#..#.........#..#. +..............##...#......#..#..........................#.........................#............#.... +...............#......#................#.......#..........##....#....................#.............. +#................#..#.............##....#..#............#..........#.......#.##.........#........... +......................#...#....#...................#........#.......#..##......................#.... +......#......#.#....#........#..................#........................#............#.....#..#.... +..................#......#.#........#.#..........##............................#......#............. +.......................#..#........#............##..................#.....##........#..............# +.....#..........................#......##........................#..##.#...........................# +#......#..........#..#......#......#..................#.#...................................#....#.. +...#.......##...#......................#..............#............#....#.....#...........#......... +....#....#.##............#.........................#....................................#.....#..... +..............#......................#...............#..#...#..................#...........#....#... +...................#.......#...#.........#..............#.....#............#................#..#.... +..........###..........................................#...#........................#........#...#.. +.........................#...........#....#...........#...................#...#............#....#... +.................#..#..##......#..#........#..............#.....#.............................#....# +....#...............#......#.......#.............#...................#.....................##..#.... +..............#............#..................#.#......#.........#.....#.................#.......... +....#.........##.#...#.........##...................##......#...#...#............................... +............##..#................#.....................#......#.....#............................##. +...#.........................................................#.#.#....#............................. +........#...#.......#................#.......................#..#................................... +.................#..........................#........................#...........#..............#... +...................#....#...................#.........#............#.....................#....#..... +...........#........................#.....#...................#......................#..#......##... +#.#......#....#......#.#............#..............#..........#.#................#..........#....... +##..........................................#..........#......#................................#.... +..##.#........#.....#.##..#..#........#....#...#.#....#.#......#....#..#...............#......#..... +..........##...............#.......#....................#....#..........#....#...........#.....#.... +......##......#...#..##.........................................................#................... +...#...............#.#..................#.......##....................#.....#...#.........#.......#. +..................#.#...#.#........#...#...#..##....#.........#.....#..#................#.......#... +..........#.#......#....##.................##...............#....#..............................#... +....#...........#.....................#..................#........#...................#............# +..#....#............#.#.....................................#..................#..............#.#.#. +............................#......#................#.....#...#......#..................#........... +#..............#..#.........#...........................#..#..............#.....#.............#..... +......#...............#..................#.......#.........#.....#..................#.....#......##. +..#................#..........#..........#.........#.##.##..#.......#..#.#.......................#.. +.....#.....##...................................#.#.#....#..........#.........#.#.........#.#....... +....#....................#.....#.##........#...............#....#...#..........#..........#......... +...#.........#.............#....#...................................##.............................. +#......#..............#......#.............#.......................##.#...#............#.....#.#.... +........#.......#.##......#.........................#.....#..#..#....#.................#......#..... +......##...#...................................###.................###................#.......#...#. +..##...................#......#.......#..#........#.....#.....##...#.#.................#......#..... +..................#..........#...........#.........#.....#....#......................##.......#..... +........#.......................#..............#.....#...#.......#..#...#.#..............#.......... +....................####....###...#..#............ +#.....................#........................... +........#...#..................#.......#.......... +................................#..............##. +.............................#.................... +......#......................#.....#.#.#.....#.... +...................#....................#.#....... +...........#.........#.....#.##..#......#.#....... +...#........#....................#...#..##.....##. +.....#......#.............#...#.........#........# +...#.#...................#.........#......#....... +.................................................# +..#................................#.........#.#.. +.......#..#.......#...#......#.....#.............. +.....#..................................#....#.... +....#...#......#......##..#.............#......... +...........#...........#..#....................... +......#..#....#.....#.........#..#.........#...... +...............#..........#......................# +................................#................. +.............#..........#...#............#........ +............#..............#....#.....##.......... +.............#................#.#......#.......#.. +.#.....#...........................#.##..........# +..............#..........#.##.##........##........ +#.......#..................#.......#..#........... +...................#.#....................#.#..... +.....#............................................ +.................#....#................#......#... +.......................#..#.....#..#...........#.. +......#............#.#.###..#.#.............#..... +..........#....#.....#...#......#............#.... +........#......##......##......#..#.............#. +###..#......#.........##....#........##....#..#..# +...#......................#....................#.. +#........#...........#......#....#....#.....#..... +........#......#.................................. +........................#......................#.. +........................#.......#................. +.............................#.........#.........# +..................#......#..#...#................# +.........#..#..................................... +....#.#.#........#.......#..##..................#. +.............................................#.... +..........#........................#.........#.... +..........#......#..............#........#...#.... +#......#...............#.......................... +.#....#........#......#.......#..#................ +.........................................#........ +......................#..#.........#..#.#...#....# + +5R10L8R8L17R3L1L34R46R28R43L46R12L12L41R9L15R13L43R27R47L46R19L22R42L6R32L2R26L8R13L22R22R24L37R35R20L36R1L34L7L47L38R42R22L7R4L32L37L28L6L46R18L6L26L16L39R16L22R45R23L48L50R13L28L25L9L19R25L47L32L33L25R5L18L46R3L38R17L46L15L23R47L30L38R26L50R8R6L38R24L36L19L46L28L16L19L3L6R10R25L21L35R37R14R48L45L47L16L37R45L46L36L14R5R24L32L50R1R1L43R2L2R28L26R7L26L46L20L38L25L14R38L27R7L45L44R3L40L13L2L5L35L38L44R37R2R36R7L50R2L17R16R46L7L32R36R2L30R44R16R12R42R9L47R13L4R13R1L38R23R6L10R3R48R13R25L9L10L6L5L16R2L29R36R34R26R42R48L6R19R48R45L24L33R15R21L40L45R35R11R33R11R10R32R48R40R27R12L7L30R37R22R14R38L39R30L36L36R21R22L7L7R30R12R24R7R45L28R3R40L32L29R19L30L30R3L30L21L46R47L6R29L45R35L43L15L25L43R16R20R1R25R26R2L49R5L12L6L45R25L43R28R9L37L22R46L40R19R32L17R50R12R30L50L15R44L23L1R9L34L43R5R42L11R44R27R13L33R39L10R17R6L37R18R17L5L29R42L6L10R19L43L5L15L13R18R21R25L25L29R18L3R50L50R10R44R47L1L4R19R31R13L42L10R42L8R35L17R4L28R12R25L38L46R45L47R30L28R11R49L30L15L27L14L19L2R7L1L33R37R18L11L20L7L20R46L5L4R8L5L33R48L7L41L30R7L13L42R33R29L2R7L44L39R5L44L22R22R27L20L30R48R11L49R41L24R25L24R34R7L3R3L11L7L5L19R4R2L20L8R3L31L37L5R23L36R21L34L22L7L12R29L25R19L16L6R36L4R15L9L32R11R24R3R12L8R7L32L24L29R17L16L13R17R9R19L16R50L21R10R45L40R29L21R10R35L19L26L28R8L10R26L43L16R41R34R7R2L30R5R12R3L19L50L4L25L41L4R16L2R8L13L4R44L4L2R37L36L41R39R50R10R34L36L13R48R30L46R1L2R47L14L4R48R6L27L25L36L33R34R25L23R45L39L46L1R23R46L35L13R47L28L42R37R17L18L9L49L29R11R32R5R47L50L29L18L27R18R33R29R19L32R36R40L10R50L2R34R41L17L43R39L2L15L7L34L5R19R6L23L50L32L30R32L48L44R48R22L22R43L9R45L42R34R16R34R21L36L6L50R11L50L3L43R48L5L33L35L42R16R31R46L11L22R41L28R29L9L48L13L46R23L30L17L32R48L15L10L20R2L24R46L12L44R14L14L45R48L23L43R6R32R21L42R28L36R11L23L22L34L7L39R25R40L13L7L4R38L29R20L18R5R47R30R7L22L3R2L19L6R43L33L15L30R49L19L13L22R37L8R21R3L20L7R50R20R10R16R17R12L27R12L11R13L43R49R46R49R4L39R24L41R26L6R36R3L5L14R46L34R26L24R42R19L35L10R7L21L28R42L43R28L42R31R45L20R25L35L47R13R3L27R19L8L45R17L18L40R42L9L2R5L18R9L11R44R2R18L2R34R2L13R3L10L26L38R6L33R31L12R28L21L42L28L6L12R42L3L27R44L35R1L38L35L21R12R1L45L35R26R7L1L32R26L10R26L11R36L15R6R14R11L7R7R39L25R31R36L29R33L48L22R7R37L4R37L11L20L40L12L38R21R19L38L39L1R27L30L39R18R38L2L50R7L13L49L19L40R45R18R38L20L5R27L7R17L1R3R40L41L29R34L42L6R25R28L25R46L40L17L4L18R9R11L50L7R12L4L6L19R29L49R46R36R30L25L8L2L16R25R13R10L18R34L4R44L36R24L49L36L41R39L2L28R15R25R42L10L40L50R30L26R36R34R6L5L9R10L21L36R11R35R17L45R4R20L19L3R32L12R19L1L23L33L46R30R42R37L16L26L6R43R45R1R37R8L43L43L31R47R9R36L23L50L28R28L34R4R45L25L17L38R41R34L36L13R47R37L35L45R19R4L47R41L9L31L36R28L8R19L3L12R26L37R37R38R13R39R21R21R40L50R40L50L31L1R13L23L3L47R23L16R33R30R10R29L30L33L47L8R4R32L27R11L13L4R8L39L10L6L27R20L13L7R24L49L44L24R47R7L48L11L50L23L47R28R32R21R21L49R27L39L11R36R39L44R4L8R5R4L8R21L2L15R32L15R13R5L14R20L44L33L23L14R24L33R1R36L42L11R14L2R31L1L38L47R12L45R3L7L17L44L48R9R43R48L6L47L28L32L50R2L23R37L2R14R27L14L41R30R22R45L50L19L26L3R26L45L22R30R12R49L14R40R35R22L9L17R13L4R33L30R5L48L45L29R29L1R39R5L35L24R28R5L11L14L9L25L19L49R18R4R33L30R27L18L8L35L3L11L9L9L22L38L49L28L39L5R24L10R18L43R1R19L31R50R37R32L5L29R49L2L23L37L21L20L44L40L26R10L14L11L33R7L9L47L8L43L2L29L50L2R2R9R2L32R13L25R38R6L7L29L25L16R25L49L11R48R5L44R19L21L19R6R33R25L37L26R18R31L7L23L34L11R18L31R50R16R29R32L24R29L50R27L48L32L46R3R1R49L27R15R44L41R6L16R9L5L47R19L35L31L23L24R16R40R1R5R18R12L32L9L4R38R26R3L26L43L23R23R45R27R25L4L15L41L1R35R3L39L8L37L14R11R48L45L46L35L32R23R26L4R6R25L36L4L29R32R7L29R40L9R17R26L8R18R29R8R11R23L19L21L12L32R45R10R4L9R48L13L37R17R28R32L19R40L49R49L2R48R24R50L35L28R28L20L30L15L23R11R28R31L44L12R33L20L8R38L47L37L4R2L40L14R20L33R32R23L24R6L3L34L6R9R12L8R12L8R8R21L29L46R1L15R37L17L41L21R34R47R38L6R41L1L45R5L3L24L22R31R34R10R26L10R43L25R20R1R25R26L49L50L2R34L10L35L27L23L40L11R30R4R24L11L30R39R10R24L14L18R23R9R4R38R42L50L20L12R42L4R49R22L41R43R48R31R13R46L26R1R8L40R22R34L35R44L7R14L3L44L21R37L19L44L27R21L49R50R14L33R45L42R19R38R7R16R31L9R49R21R3R21R3L7L2R25R43L42L1L29R9R28R22R45L9L34R46R12R37R8L50R2R48L23R1L14L24L23R28R35L8L17L23R26R37R49L44R40R32R33L17L48L49L1L15R41R29L22L47R18L7L25R20R13R45L38L43L45R45R29L1L29L8R24L43L24R2L45L30R46L30L6L48L15R22L29L26R17L40L39R12R48L17L13L18L18R50R45L35R3R47R49R40L22L40R4R29R41R18L31R38L8L9R40L34L31R45L12L3L25L27R12R46L19R26L14R31R31R41L42L24R31R28R44L1R33L22L15L28L27L39L27L43R36L36L46L41R49R15R37R18R10L9R33L46R27L11R34L2R30R32L26L7L31L29R35L26R5R17R31L2R43R20L24R33R9L1R18R5R27L36R3R34L1R12L43R6L30R41R41L44R33L47R47L20R32R10L27L8R6R12L31R36L45R3L44L33R45R48L5L28L45L47L44L38R21L9L24R12L43L13L23R27L33L9L31R50L12L41L46R34L43R14R50L41L32R5R46R43L47R17L33R50R38R22L26L25L3L15R5R22R45L7L8L21L44R43L16L7R33L4R37L27R35L1L46L35R19L32L10R42L2L7R1R23R32R33L1R12R24R50R37R36R2R44L47R5L8R39L1L8L40R12L27L10L49L41R18R27R6R42R22L49R3R46L35L2L6L17L27L48L44R15L20R45L46R16R14R32L20R32L5R22L22R37L48L28R4R25L3R18R21L30L31R1R20L21L3R48R2R14R46R45L17R25R49L39R26L19R11L20L13R37R39L31R36R50R26L35R32L4R20L13L18L21L9L42L44L29L44R44R13L14R28L21R9R8R23R19R8L10R38L39L2L7R42R1R40R23L27L39L3L50L38L44R47L27R34L22L43R29L8L21R16R15L27R4R27R19L10L33L3L11L34L23L45L41L30L13L48R49R23R15L9R20L49L11L25R16L25R13R21R18R15R50R34R48L50R48R15L13L36R19L46L43R12L38R15L15R4R15R34R19R11R29R22L36R6R27L28R46R47R35R3L12R17L43R50L11L34R12L10L29L23R7L41L10R29L16R34R22L22L27L8R10L6L21R49L31R29R34L41L4R25R41R20R43R27R21L38L14R43R4R9L41R48L43L21R49L49L48L20L21R39R14R6R44R34L5L10R49R10R24L23R18R20L24R17R14L12L40R3R43R21R35R2L22L17L15R17R45L29R43R47R5L50R19L30R1L49R26L27R18L45R31L36R22L24R29R15R23R48L30R39L27L10R31L16L10R38L50R40L3L17R48R16L22L50L11L18L44R2L41L21R26R23L26R28L42R6 diff --git a/22/solution.mjs b/22/solution.mjs new file mode 100644 index 0000000..819a4d0 --- /dev/null +++ b/22/solution.mjs @@ -0,0 +1,256 @@ +import { readFileSync } from 'node:fs'; + +const input = readFileSync('input', 'utf-8'); + +const [mapData, pathData] = input.split('\n\n'); + +const F = { + R: 0, + D: 1, + L: 2, + U: 3, + invert: (facing) => { + switch (facing) { + case F.R: return F.L; + case F.D: return F.U; + case F.L: return F.R; + case F.U: return F.D; + } + }, + rotateR: (facing) => { + switch (facing) { + case F.R: return F.D; + case F.D: return F.L; + case F.L: return F.U; + case F.U: return F.R; + } + }, + rotateL: (facing) => { + switch (facing) { + case F.R: return F.U; + case F.U: return F.L; + case F.L: return F.D; + case F.D: return F.R; + } + } +}; + +class Map { + constructor(mapData) { + this.data = mapData + .split('\n') + .filter(line => !!line.trim()); + + this.height = this.data.length; + this.width = this.data.reduce((max, row) => Math.max(max, row.length), 0); + + this.portals = {}; + } + + get startingPosition() { + const pos = [1, 1]; + + while (this.get(...pos) !== '.') { + pos[0]++; + } + + return pos; + } + + cubeMode() { + if (this.width !== 150 || this.height !== 200) { + throw new Error('Cube mode only works with full size map'); + } + this.portals = {}; + + const e1 = this._edge([51, 1], [100, 1]); + const e2 = this._edge([101, 1], [150, 1]); + const e3 = this._edge([51, 1], [51, 50]); + const e4 = this._edge([150, 1], [150, 50]); + const e5 = this._edge([101, 50], [150, 50]); + const e6 = this._edge([51, 51], [51, 100]); + const e7 = this._edge([100, 51], [100, 100]); + const e8 = this._edge([1, 101], [50, 101]); + const e9 = this._edge([1, 101], [1, 150]); + const e10 = this._edge([100, 101], [100, 150]); + const e11 = this._edge([51, 150], [100, 150]); + const e12 = this._edge([1, 151], [1, 200]); + const e13 = this._edge([50, 151], [50, 200]); + const e14 = this._edge([1, 200], [50, 200]); + + this._portalEdge(e1, F.U, e12, F.R); + this._portalEdge(e2, F.U, e14, F.U); + this._portalEdge([...e3].reverse(), F.L, e9, F.R); + this._portalEdge([...e4].reverse(), F.R, e10, F.L); + this._portalEdge(e5, F.D, e7, F.L); + this._portalEdge(e6, F.L, e8, F.D); + this._portalEdge(e11, F.D, e13, F.L); + } + + flatMode() { + this.portals = {}; + for (let row = 1; row <= this.height; row++) { + let col = 1; + while (this.get(col, row) === ' ') col++; + const start = [col, row]; + while (this.get(col + 1, row) !== ' ') col++; + const end = [col, row]; + + this.portals[[start, F.L]] = [end, F.L]; + this.portals[[end, F.R]] = [start, F.R]; + } + + for (let col = 1; col <= this.width; col++) { + let row = 1; + while (this.get(col, row) === ' ') row++; + const start = [col, row]; + while (this.get(col, row + 1) !== ' ') row++; + const end = [col, row]; + + this.portals[[start, F.U]] = [end, F.U]; + this.portals[[end, F.D]] = [start, F.D]; + } + } + + get(col, row) { + const x = col - 1; + const y = row - 1; + + if (y < 0 || y >= this.data.length || x < 0 || x >= this.data[y].length) { + return ' '; + } + + return this.data[y][x]; + } + + nextPos(pos, facing) { + if (this.portals[[pos, facing]]) return this.portals[[pos, facing]]; + return [[ + pos[0] + (facing === F.R ? 1 : (facing === F.L ? -1 : 0)), + pos[1] + (facing === F.D ? 1 : (facing === F.U ? -1 : 0)), + ], facing]; + } + + _portalEdge(fromEdge, fromFacing, toEdge, toFacing) { + if (fromEdge.length !== toEdge.length) { + throw new Error('fromEdge must be the same length as toEdge'); + } + + for (let i = 0; i < fromEdge.length; i++) { + const fromPos = fromEdge[i]; + const toPos = toEdge[i]; + + this.portals[[fromPos, fromFacing]] = [toPos, toFacing]; + this.portals[[toPos, F.invert(toFacing)]] = + [fromPos, F.invert(fromFacing)]; + } + } + + _edge(start, end) { + if (start[0] !== end[0] && start[1] !== end[1]) { + throw new Error("Edges can't be diagonal"); + } + + const colDiff = end[0] - start[0]; + const colIncr = colDiff / (Math.abs(colDiff) || 1); + + const rowDiff = end[1] - start[1]; + const rowIncr = rowDiff / (Math.abs(rowDiff) || 1); + + const edge = []; + + let col = start[0]; + let row = start[1]; + + edge.push([col, row]); + do { + col += colIncr; + do { + row += rowIncr; + edge.push([col, row]); + } while (row !== end[1]) + } while (col !== end[0]) + + return edge; + } +} + +class Walker { + constructor(map) { + this.map = map; + this.facing = F.R; + this.pos = this.map.startingPosition; + } + + walk(paces) { + for (let p = 0; p < paces; p++) { + const [nextPos, nextFacing] = this.map.nextPos(this.pos, this.facing); + let nextTile = this.map.get(...nextPos); + + if (nextTile === ' ') { + throw new Error('Escaped Map!'); + } + + if (nextTile === '#') { + break; + } + + this.pos = nextPos; + this.facing = nextFacing; + } + } + + get password() { + return ( + this.pos[1] * 1000 + + this.pos[0] * 4 + + this.facing + ); + } + + rotateCounterclockwise() { + this.facing = F.rotateL(this.facing); + } + + rotateClockwise() { + this.facing = F.rotateR(this.facing); + } +} + +const instructions = pathData + .split('\n') + .filter(line => !!line.trim()) + .join('') + .split(/(L|R)/) + .filter(instruction => !!instruction.trim()) + .map(instruction => { + const num = parseInt(instruction); + return isNaN(num) ? instruction : num + }); + +function runInstructions(walker) { + for (const instruction of instructions) { + if (typeof instruction === 'number') { + walker.walk(instruction); + } else if (instruction === 'L') { + walker.rotateCounterclockwise(); + } else if (instruction === 'R') { + walker.rotateClockwise(); + } else { + throw new Error(`Invalid instruction: ${instruction}`); + } + } +} + + +const map = new Map(mapData); + +map.flatMode(); +const flatWalker = new Walker(map); +runInstructions(flatWalker); +console.log('The password for the flat map is:', flatWalker.password); + +map.cubeMode(); +const cubeWalker = new Walker(map) +runInstructions(cubeWalker); +console.log('The password for the cube map is:', cubeWalker.password);