diff --git a/2022/input/22/input b/2022/input/22/input new file mode 100644 index 0000000..b7bb4f6 --- /dev/null +++ b/2022/input/22/input @@ -0,0 +1,202 @@ + ..#......#........................#...#......#.............#...........#......#...#............#.... + ......#..........#...........#....................#....................................#..#......... + ...............#.##.............##...#..#..#.......#.#............#.#.#...#..................#...... + ...............................#..#........................#......#...#............#................ + .#....#.......#....#.......#.............#..#...........#.............#............#........#....... + ........#..................#.#........#......#.......#............#....##.........#.........#..#.... + #.........................##..............##...................#.....##..............#.............. + #............................................#..........#......#.#..........#...........#....#...... + ..........................#.#.....#......#.....#...................................#.....#......#... + .....#...........##.....................................................##.#...#.................... + .....#....#.#....................#.#....#.................#.........#.#................#...#........ + #..........#.#.........#....#...........................................##......#...........#......# + .........#..##......#.....#...........#....##.....#........#..##....#................#.............. + .....#..........##...........#................#.......#......#..............#......#.......#........ + ........#......##......#..........#......#...................................#...................... + ......................#..#........................................#.......#..........#.....#........ + .#...#......#......#...................#....#..##...............##...#........#......#.............. + .##...#.#....#.......##.......................#...........#...........#....#.#.........##........#.. + ...#......................#.#...#............#......#............#....#.......#.............##...... + .......................#..................#....#.....#.......#..............................#....... + #............#......#......#.#.....#.............#......#..............#.........#.......#....#..#.. + ............................##.........#..........#......#......#.............#..................#.. + ..............#..#.....#..#................#..#.................##.....#...........#.....#...#.##... + ...#..#...#.........###.........#....................................#......#....#.................# + ................#.......#.........#.........................#......#.........##.......#......#.#.... + ...#........#....#.....................#.................#.#......#.........#.......#....#..##...#.# + .....#.....#.....#........#.....................#..#....#..............#...............#.........#.. + ................#.............##.#.#..#..#.#..........#......#...#.#..............#...#..#.......... + .......###......#..#......#........#.........................#......##........#.#.......#..##.#..... + .#.......#................#................#.....##..............................#...#.............. + #...........................#...........#....##..........................#...........#.........#.... + .....................................#..........#.................#.#...........#..................# + ....#...#.##.............#..#................................#.............#.......##............... + ......##............#...#....#.........#...............#.......#............#..........#........##.. + ..#...#.................#.............#..#................#..........#.......#...................... + ......#..........................#...#.......##..#..#...........................#....#.#......#..... + ...##........#.............#.#.......#...................#............##.........#.....#............ + ...........#...................#.#.................#..............#.........#........##..#.....##... + .........#.....#.#......................................#.............#..............#....#......... + ...##.#.......#..#.......#........##..........#.............................#....#.........#.....#.. + #.........#..........#......##......#.......#.........................#....#.....##............#.... + ...#.#.#.......#.....#......#.............#......#.#............#....#.................#............ + .........#......#......#..#....#.#.#..#.....#.....#......##.....#...#.#..................##..#.##... + .......#......#........#...#.........#..#..........#..................#.............#.......#....... + ......................#.......#................#......#.##.......##...........#....#........##...... + ..#........#..#.....................##.................#...##....#..................#...###.....#... + .#...........................#........#......................#.......#.............................. + ....................#..#...#....#...........#.............................#.....................#... + .....#.....#.....................#...#.#....................................#....#....###.......#.#. + ....................#.....#......#................#........##...............#....................... + ...............#.#.............#.....#.#......#... + .........#...#.#.#................................ + ........................#..............#.......... + .....#........#......................#.##.....##.. + #................#.........................#.....# + .#....#.............#.......#....#.#......#....... + ...#........#...........#.....#...........#......# + ...#............#........#......#............###.. + .#.........#...#..............#.#.......#......... + .....#...........................#........#......# + ..........#.....#....#.##.............#...#....... + .###....##................#..............##......# + ......#.#..........#...................#........#. + ....................##...........#................ + ........#..........##............................. + .....#.#.................#...........#.....#...... + ....#.................#........................... + ..................................#............... + .....#.......#............#......#..#....###..#... + .#....#.....................................#..... + ..##........................#...........#.#....... + .......#.......#.#................#......#........ + .................#.#....#.............#.##........ + ...#.............................#.........#...... + ..#........#...............#..................#... + ..............................#......#............ + .#..........##.#..............#.........#....#..#. + ..#............##..........#..#..........#......#. + .....#.......#......#......................#...... + .............................##.............#..... + ..............................#................... + ....#......#.#..........#......#.........#..#..... + ........#.....#....#..##.#.#.#.................... + ...............##......................#.......##. + .......#..............#........#.......#...#....#. + ......#.............#...#..#.#.......#.##.....#... + .................#....#..#..............#......... + ...#......#....#.....#.......#........#...#.#..#.. + .........#....#.....#.............##.............. + ......................#........................... + ......#...#.........#............................. + ......#.#......................................#.. + ....#..........#................#....#.#.#........ + .........#.................##.......#.......#..... + ............#....#...#...............##....#.#...# + ....................................#............. + #..#..#...........#.................#.#........... + ....#.......................#........#.#...#...... + #............#.......................#........#... + .........#......#.......#.#..............#.##....# +....#...........#....#.............................#.......#........#.#.#..##....#.##......#......#. +...#..........................#.##.#.............................#.................###.#............ +...........#.......#.#..................................#............................#.#........#... +....#..............#.#......#.#.#.#...#.......................#.............#............#....#.#... +.............#....#..............#..#......#..........#...................#....#......#......#...... +..#...........#..#.......#.............#...........................#....#.#..#............#......... +.................#.......#.#..............##....................................##.#................ +.....##..#...#...#....#................#.#....#..................#.................................. +....#............#...........#.#.................#.....................#....................#...#... +............................#...#..............##..#....#....#.....#..........#..................... +.......#.......#.#......##.........#................#.#..#..#.................#..................... +..#...........................##.............#...................................#...#.#............ +..#..........................#.##.......#...............##.......................#.......#...#...... +...................#..#.......................#...##.........................#..................#... +...##..........#......................#.#...............#.#.....#.#...............................#. +................................#...........##.....#.....#.........#.........#...........#..#..#.... +..............................#....#.....#............#................#.##..................#..#.## +.....................#.#......#.....#...#.....##.............#......#.#............................. +.....##.................#.......#.................#.##............#..........#..#.....#..#.......... +#..#................#..........#.........#..#....................#......#..........#..........#..... +............#..............##................#.#...#........##.........#......#..#......#......#.... +....##....................#.........................#....##...............#...###.....#............. +.......#..............#...........##..####.....#.....#.####...........###......#.................... +......#....#...##...........#.....#.................#.....##.....#......#.........##................ +....#................................................#.........#..........................#..#.##... +##...#..........................#...#.............#.......................#.#..#.........###..#...#. +#...........#.#.....................#...............#.............#..#.......#...................... +....#..#.......................#...#..........#......#.#.....#..................................#... +...............................#...#..............#..#..#..........#.........................#...... +....#.......................#.........#........................................#.............#...... +....................#...........#..........#.....#......#..........#............#.........#.....#... +.....#...#..........#...............#..#............#...##.................#...............##....... +.........................#.................................#........................#.....#..#...... +...........#.#..#........#.#....##........#...#.........................#.#......................... +...............#.......#..#...#....#.......................#......................................#. +.........##..#......#..##...#............#..#.....#......##..............................#.......... +.............................#.#.#...................#............#....#.........#..##....#......... +........#.......#.....#..........#.................................#........................#....#.. +#.......#.........#...........#...........................#.##.#..........#....#.##.....#.........#. +.....#..........#..........#...##................#..........#....................#.....#..#.#......# +.........................#..#..........#...#.......#..............#.#..#.#...#...#.................. +...###.....#.#...............#........................................#.#.#...#.#........#...#...... +......#.........#.........................#......#.....##..............#........##...#.............. +...........#.........#...........#..#...#....#..................#.#....#........#...#.....#......... +......##...#...............#..................................#.#........#....##.##.#............... +....#..#..................................#..#....#....................................#..#.#....... +.....#.................#.#....#...#.#.....................#.......................................#. +.#..#.#.........#.........#..............##.#....#..#.#......##............##..........#........#... +...........#.....#.......#..#.................................#............#........................ +...#.#.....#........#................#.#.....#.....................#...#............#..##........... +....##..............#..............###..##........ +............##........##.......................... +.#.#.......#...................#.................. +....#..................................#....#..... +#.......#..#...#....#..#..#.............#......... +........#...#.................#...........#....... +.#...#..........#.........#.............##........ +.......#...................#........#........#.... +#......##..#.............#....#.#.#.........#..... +....................#..#.......................... +...#.......#........#....#.............#.#.....#.# +.#..............#...#........#....#............... +.........................#....#...##.......#...... +...#.............#..#..........#.#.#....#...#..... +.#...............#...#...#....#.......#........... +...#............................##............#... +...#...........................#...........#..#... +..#......#...........#...#...........##...#..#.... +........#.......................#..........#....#. +...............#..............#......#.......##... +..........#.....#.......#.....#...........#.#..... +#...#....#......#..#........#.........##......#.#. +.....##....#..........#.......#................... +#...#.....................................#......# +....#....#..#...#....#..............#..#.......... +............##...........#...#.................... +........#........#....#........#...........#...... +..#..#....................#..............#...#.... +...............#..........#.##.................... +.#.....#......#................................... +.....................................#............ +...#......#....................#.........#.......# +..........#..........#..........#....#............ +.....................#.....#..............##...... +................#..............#......#....#...... +.......#....#.##..##..#..............#...#.....#.. +.....#.......##...............#........#.......#.. +.......#...........#.............................. +.#..........#...........................###.#..#.. +...#......#..#..............#..................... +...........##....#..............##......##........ +............#.#........................#.#.#....#. +........#..............#.................#........ +.......#....................##....#............#.. +..#.##..............#......#............#......... +##..#...........#..#....#.....#...........#.#..... +.#.#.......#...#..#.............#................. +......................................#.....#..... +.....#.#.........#................................ +..................#........#........#.##.......... + +9L8R50R46L28R44L26R40R36L17L29L36L6R31L25R12L39L10R38L16R49L42L20R5L37L19R8L13R15R2R37L25R39L14L48R25R45R24R11R50R14L15L15L31L41R35L4R23L48L26R28L24L13R17R19R7R16L35L13L3L44L10R24R13L9R7L10R3L49L48R17L22L27R31L32R41L30R16L20L50L11R23L12R12L2R14L13L39L29L30R8L8R33L49L14L40R36L5L30L2L40L15R34L21R35L13L28L1R15L45R11R20R36R45L38L48L47L39R10R8L17R38R41R28L25L15R9R8L12R15R16L49L40L10R10L4L49R42L12R26L20L14L2L25R33L6R41R10R10L31L34R50R42R48R3R21R43L18R22L41L41R47R8L43L40R45R49R36L48R38L41L23R22L25L25R40R7R1R7R9R40R45L34R44R6R29L16L26R18R24R5L9L28R27R42L27L12L48L21R21L3R33L18R26R45R36L13L4R9R34R46L21L38R12R45L50L32R2R45R4L39L13L47R21R48L35R40R33L32L27L36L39L12L11R9R33R5L25R1R16L1L8R41R32L36R47L23R9L6R35R12L33R6L43L31R19L7L47L7R31R14L40L30L4L50R2L14L16L29L15L37R42L32R23R42R49L41L34R35R48L32L47L27R20R2R37L4R44R31R41L34L43L5R29L15R25L30R44L4L36R27R14R12R31L29L44R41L19L35R9R13R5R2L27L39L26L23R22L10L14R12L23R28R22R2R16R36L40R30R5L27R26L31R7R49R1L50L22R43L9L44R21R23R14R24R9L32L2R25L34R37R6R17R1L41R40L17L13L50R4R26L23R13R29R46L25L2L30L49L37L9L9L16R38R46R2R18R16R3L35L30R43L23R12R5R38R21R42R19L44L7L43L44R46L5L8L15R19L21R12L2L47L10R34L13R33L14L47R20R16R37R17R44L26R24L50R16L5L16R6L28R25R1L20L20R1R42L9L43L42L31R32L48L3R44R47R15L19R23L5R47L36R4R8R43R15R5R37R13L1L3R25R25L18L31L13R26L12R14L45R34L6R19L38L40R25R8R42R35R5L44R38L34L42R13L33L11R1R27L31L43R37L14L46R25L33R24R7L5R36R19R36R46R39R35L47L12R38L48R42L50L10L5R17L22R34R35R24R6L5R7R29L9L31R41R17R15R3L20L50L24L14R43L18L1L9R8R39R4L28R3R3R42R39L33L30R14L47R28R33L22L6R41L29R1R15L40R8L48L17L22L12R28L29L42R4R10L16L22L39L6R3R27L38R2L25R12R3R46R15L38L8L23R36R36L11R43R2R8R34R4L17R8R21L23L28R50R21L17R39R2L37L4L19L14L49R43R35R42L37L14L10R48L26R41L34L2R16R2R26R9R44L15L7L1R1L9R12R33L48L43L46L22R41R1R18R40R23R35R29R28L4L19L23R4R45L30L17L15L3L9L28L30L28R49L24L7R23R26L30L20R24L2L23R14L27L41R33R17L39L42L41L16L6R22R26L39L13R33L10R32L13L45L20L36R25L4L24L15L28R22L42L38L12R28L6L47R16R50R47L5L43R28L21R1R50L13R34R23L43R22R38R29R37L31L3R42L3R26L23L4L46L24R40L13R34L13R36L12R46L38R21R44R25L23R34L43R30R38R39L16R1L34L35R43L13L43R46R47R2L23L15R22R47R23R35R31R10L6R18R32L25L13L37R44R20R50R28L46R26R48R46L38L49R33R8R32L18L32L39L35L28R8L36L16L38R27L6L16L15L28R46L17L32L11R41L37L13R21L11L1R14L48R33L49L16R29R23L41L38R2R42R27R26L41R49R36L29R22L8L13R45R48L42L45R39L35L12L43L12L35R40L43R36R21L14R44L12R9R43L42L39L16R9L11R50R41L46L40L17L42R47R44R48L19R2R4L41R4R39R47L30R50R28L13R38L3R22L10L21R31L39L24R7L44L22R21R7L11R49L25R22L13R16R14R18R33L11L35R3R23R6L44R31L10L4R43L16R31R46L43R46R35L5R50R48R42L33R35L7L20R38L4R40L45R40R43R46R30L18R6L11L7L47L38R37R32R44R45R23L43R39L30R23R6R40R33R50L33R21L35R29L1R34R15L16R50L42R47R43R27R21R31R49R22L7L7L2L20R8L16R48R4L35R42L45L13R42R15L9R1R15L31L47R15R40L21R12R11L10R42R17R3R19L15L24R37L18L36L42L21L50R9L42L27L24L43L44L49R13L10L18L5R25R41R19R18L8R42L9L22L37L5L16L4L22L50L14L40R40R2L32L9L24L6R23L24R50L7R48R39R27L18R17R43R37L41R38R50R6L37L2R38R17R1R31L3L26L34R23L14L13L32L15R27R18R16L34L6L11L46R32L11L13R41L3R43L19R24L1R33L1R2R21R33R8L18L20R27L2R43R1L28R15L16R21R40R42L28L11R5L28L11L2L27R47L16R10L3R44R21L31L28R10L35L7R28L50L19L31L48L45L24R24R38R44R35L2R50L34L27R22L2R23R43R24R27R5R12L46L3R21L38R20R11L4L4L5L24L7R1R49L26R34R47L41L4R6R4L27R28R17R24R15R21R11R43R26R38R40L39L40L27R37R47L28R6R34L38R17L7L10R26L49R32L9R14L29L14R13L21R40R1R39R1R24R33L6L3R11L40L1L13L7L42R21L3R1L35R12L21R16R3R20R5L15R18R8R42L13L29L48R38L31L10L1L13R45L28L34R50L27R4L37L46R26R50R7R12L44L25R7R6R27L7L3R46L18R6R14L45L45R30R45L35R19R27R19R4R18L41L19R5R5L30L47R35L25R42R22R12L15L33R9L39R18R19L8R21L14L40R22R44R3R36L22L13L48R29R33L40L25R47R17L47L9L8R1L23L30L22R28R30L21L33R43L41L3L42R27R38R40R48L9L25L42R31L36R45R47L44R41L1L3R17L25R47R47L38R18L21L32L39R30L28L40L7R11R13R6L42L43R30L41L21R23L16L19L5R46L44L46L4L33R33R11R35R48R13R43R8L14L37R11R34R46L38L41L14R31R40R45R29R13R5R44L47R35L29L20R2L36L39L15R9R48R17R20L30L28L42R26L44R2L23L21R48L40L37R29R6L3R18L15L37R48L7R4L13R45R10R7L40L11R19L5L9L36R49L4R15L32L43L28R14L16R36R46R45L15L47R42L15R24L40L1L25L13R43R7R22L15R3L24L35R39L32R10L45R40R31R8R49L13R19R43R46R39R50L6R42L24R49L21L3R28R45R36L39L37L19R38L14L48R27R15L44R24L4R35R2R32L29R14L50R20R5R5R15R46L1R39L9L38L44L33L19R28R28L33L41R38L40R12R7L42L14R7R14L24R14R30R24R19R34L26R26L34L31R22R32L13L19R36R25R27L1R9L25R28R12R26L35R6L27L17L46R4L13L4R50R45R39L37L47R6R48R21R21R4L35L23L5R32R48L40L1L8R42L3L27R42R36L26L29R1L33R16R19R20R1R42R38R41L2L47L37L26L24R8L17L6L49R25R9L41R8L12L12L49R9R49R26L30L4R31R35L16R40R20R31R43L12L14R41L27L42R19L45R28R18L47R4L5L41L50L12R24L31R4R44R43R32L42L29R44R45R10L3L49R4R12L42R48R26R41L47L4R41R20L23R32R12R37R10L40L19L18R28R9R6L50L2R37R16R27L16L37R36L41L1L38R17L46R18L36R20R28L33L2R13L10L6L41R38L12L45R11L5L41R19R42L39L11L34R28R37L50L33R34R39R17L18L9R36R45R47R16R30R13L49R27L8R12R48R23L24L1L5R35R10L34L16R14L18L43L9L23R49L22L35L16L48R9R24R39R15R26L10R36L33L39R48R25R31L23L30L48R47R1R40L16R34L46R48L7L1R22R19R4R10R1L45L20L1R47L45L25R11L24L23R38R29L34R47L13L13R3L26R42L50L11L35L17R18R37R43R49R11L29R33R16L14R23R30L40L5L11L50L23L41L38L38R17R49R8R39L10R10L20R31R4R23R29L5R10L15R32L29L34L40R3R24R6L38R11R37R9R20R34L48R45L14R35R4R46L17R6R17R45R48L4R2L30L46R14R47L24R17R33L7L49R22R1R43L36R3L38L36R23R6L17R5R18L9R20R43L14R24R31L21L18R46R24R14L5R39R4L11R18R8L41R32L22R17L29R27L50L44R50R34L46R38R10L12L11L34L12L41L26R21L24R33L25L40R18R24R9L36L7R26L2L9R42L8L24L5R23R48R7L44R11L47L12L27R48L25L15R34R37R46R12L18L7L46R50L24R38R2L26L9L43L1R38L6L6R43L40L41R5R39R28L35R3L32L1R6L14L15R49L16L40R34L6R48R41R41R11L5R10L20L27R15R4R23L39R27R2L22R49R33R43L26R47R2L38R42R22L30L44R29R11L39L42L30R31R47L44L26L31L15R3R11R9L45L30R27L34L35L16R37L18R15L29R25R41R8L39R29R10R33R14R12L9L11L30L3L49R40L27R40 diff --git a/2022/input/22/test-1 b/2022/input/22/test-1 new file mode 100644 index 0000000..8bc2e1e --- /dev/null +++ b/2022/input/22/test-1 @@ -0,0 +1,14 @@ + ...# + .#.. + #... + .... +...#.......# +........#... +..#....#.... +..........#. + ...#.... + .....#.. + .#...... + ......#. + +10R5L5R10L4R5L5 diff --git a/2022/src/bin/day22.rs b/2022/src/bin/day22.rs new file mode 100644 index 0000000..bd74ec9 --- /dev/null +++ b/2022/src/bin/day22.rs @@ -0,0 +1,277 @@ +#![feature(test)] +use core::fmt; +use std::str::FromStr; + +use anyhow::Result; +use aoc::Solver; +use regex::Regex; + +// -- Runners -- +fn main() -> Result<()> { + Day::solve() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_test1() -> Result<()> { + Day::test(Day::part1, "test-1", 6032) + } + #[test] + fn part1_solution() -> Result<()> { + Day::test(Day::part1, "input", 93226) + } + #[test] + fn part2_test1() -> Result<()> { + Day::test(Day::part2, "test-1", 5031) + } + + // Benchmarks + extern crate test; + #[bench] + #[ignore] + fn part1_bench(b: &mut test::Bencher) { + Day::benchmark(Day::part1, b) + } + #[bench] + #[ignore] + fn part2_bench(b: &mut test::Bencher) { + Day::benchmark(Day::part2, b) + } +} + +#[derive(PartialEq, Eq)] +enum Tile { + Void, + Open, + Wall, +} + +#[derive(Debug)] +enum Direction { + Right, + Down, + Left, + Up, +} + +impl Direction { + fn rotate(&mut self, clockwise: bool) { + *self = match self { + Direction::Right => if clockwise { Direction::Down } else { Direction::Up }, + Direction::Down => if clockwise { Direction::Left } else { Direction::Right }, + Direction::Left => if clockwise { Direction::Up } else { Direction::Down }, + Direction::Up => if clockwise { Direction::Right } else { Direction::Left }, + } + } +} + +impl From<&Direction> for usize { + fn from(value: &Direction) -> Self { + match value { + Direction::Right => 0, + Direction::Down => 1, + Direction::Left => 2, + Direction::Up => 3, + } + } +} + +impl From for Tile { + fn from(value: char) -> Self { + match value { + ' ' => Tile::Void, + '.' => Tile::Open, + '#' => Tile::Wall, + _ => panic!("Invalid tile input: {value}"), + } + } +} + +impl fmt::Display for Tile { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", match self { + Tile::Void => '~', + Tile::Open => '.', + Tile::Wall => '#', + }) + } +} + +#[derive(Debug, Copy, Clone)] +struct Vec2 { + x: usize, + y: usize, +} + +struct Map { + map: Vec>, + pos: Vec2, + direction: Direction, +} + +impl FromStr for Map { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + let map = s + .lines() + .take_while(|line| !line.is_empty()) + .map(|line| { + line + .chars() + .map(Tile::from) + .collect::>() + }) + .collect::>(); + + // Get the starting point + let x = map[0].iter().position(|t| *t == Tile::Open).unwrap(); + let pos = Vec2 { + x, + y: 0, + }; + + Ok(Self { map, pos, direction: Direction::Right }) + } +} + +impl Map { + fn movement(&mut self, steps: usize) { + for _ in 0..steps { + let mut np = self.pos; + match self.direction { + Direction::Right => { + np.x += 1; + // Wrap around if we walk out of the map on the right + if np.x >= self.map[np.y].len() { + np.x = 0; + + // Move over the void, we only do this when we wrap around, since there are + // no void tiles on the right + while self.map[np.y][np.x] == Tile::Void { + np.x += 1; + } + } + }, + Direction::Left => { + // Make sure we do not underflow + if np.x == 0 { + np.x = self.map[np.y].len(); + } + + // Update our location + np.x -= 1; + + // Jump over the void + if self.map[np.y][np.x] == Tile::Void { + np.x = self.map[np.y].len()-1; + } + }, + Direction::Up => { + // Make sure we do not underflow + if np.y == 0 { + np.y = self.map.len(); + } + + // Update our location + np.y -= 1; + + // Jump over the void + if np.x >= self.map[np.y].len() || self.map[np.y][np.x] == Tile::Void { + np.y = self.map.len()-1; + } + + while np.x >= self.map[np.y].len() || self.map[np.y][np.x] == Tile::Void { + np.y -= 1; + } + }, + Direction::Down => { + // Update our location + np.y += 1; + + // Wrap around + if np.y >= self.map.len() { + np.y = 0; + } + + // Jump over the void + if np.x >= self.map[np.y].len() || self.map[np.y][np.x] == Tile::Void { + np.y = 0; + } + + while np.x >= self.map[np.y].len() || self.map[np.y][np.x] == Tile::Void { + np.y += 1; + } + } + } + + // If the space is open we update our location, otherwise we stay where we are + if self.map[np.y][np.x] == Tile::Open { + self.pos = np; + } else { + // There is a wall in front of us, so no point in trying the rest of the + // steps + break; + } + } + } + + fn rotate(&mut self, clockwise: bool) { + self.direction.rotate(clockwise); + } + + fn score(&self) -> usize { + 1000 * (self.pos.y+1) + 4 * (self.pos.x+1) + (usize::from(&self.direction)) + } +} + +fn parse_movement(input: &str) -> Vec<(usize, Option)> { + let re = Regex::new(r"(?P[0-9]+)(?PL|R)?").unwrap(); + + re.captures_iter(input).map(|capture| { + let steps: usize = capture.name("steps").unwrap().as_str().parse().unwrap(); + + let clockwise = capture.name("direction").map(|direction| { + match direction.as_str() { + "L" => false, + "R" => true, + _ => panic!("Invalid rotation: {}", direction.as_str()), + } + }); + + (steps, clockwise) + }).collect() +} + +// -- Solution -- +pub struct Day; +impl aoc::Solver for Day { + type Output1 = usize; + type Output2 = usize; + + fn day() -> u8 { + 22 + } + + fn part1(input: &str) -> Self::Output1 { + let (map, movement) = input.split_once("\n\n").unwrap(); + let mut map = Map::from_str(map).unwrap(); + let movement = parse_movement(movement); + + for mov in movement { + map.movement(mov.0); + if let Some(clockwise) = mov.1 { + map.rotate(clockwise); + } + } + + map.score() + } + + fn part2(input: &str) -> Self::Output2 { + 0 + } +}