diff --git a/inputs/day22.txt b/inputs/day22.txt new file mode 100644 index 0000000..799b5cf --- /dev/null +++ b/inputs/day22.txt @@ -0,0 +1,202 @@ + ................................#............#.....#..........#.........................#........#.# + .........................#.........##..#..#.#........#.#........#...........#....#...........#.#.... + .......#......##........##.....#.........................#.............#..................#......... + .....................#..#.#.....##.......#.#...........#..#.....#......#..............#..#...#...... + ..........#...................#.....#.....................#...........................#............. + .............................#.....................#........#...#.................#.......#......... + ..............#...#..#.......................##..................##.#.......#........#.............. + .#.........#...........#.#...............#....##...#...##.............#...#...........#....#........ + ...#.................................................#...........#.............................#..#. + ....................#......#......#..................#....................#..................##..... + .....#...#.##..................#.............#.........#.##.............................#.#...##.... + ...#......#..........#.....................#....#........#...#...........##.#.....#......##......... + .........#.....#.....#.....................#..................#.......#................#............ + .##.......#........#................................#.....#.#.......#............................... + .........#.........#.......#...#...#......#.......#.#......#.......#......#......................... + .#.#....#..#...............##..........##.#..#...............#..............#..#.................... + ......................##.............#......#......#...#..............#.............##...##......... + #..........................................#.......................................................# + ...........#........#...................#...........................................#............... + ...#....#.......#..............#..........#............................#.......##..............#.##. + .......#..................#..#......................#....................#...#...##..........#.##... + ................................#.........................................##.#..........#........#.. + .......#..............#..#..........#..#....#....#.....................#...#..........#.......##.... + .#..#.......................#.#........................................#...........#...##.#.....#.## + #.#............##...............##.............#........##...............#.....#...#................ + .........................#..#.......#......................................#.............#.......... + ..........#.#..##..#.........#.#.............#..#.....#......#..#......#.......................#.... + ......#...........#.#.......#........#.#...............#..........#...#..............#...........#.. + ........##.............#.#....#...........#..#............#...#....................#..........##...# + ........#...#.......#...#..............#...................#...........##.....#...#...........#..... + .....#....#...#................#..............#........#.....##......#..#.........#..#............#. + .....#.#....#.....#.......#.#.......#.............#.##........................#..#........#.##.#...# + .............#...........##........#..................#........#...............#.................... + .#.........#......#.........#...###.#.#......................#......#...#.............#...#......... + ..................#..............#...#....................#....................#.................... + ...............#....................#...#..............#............#..#.#......#..#..#............. + ....#.....#.........##..#..........##................##....#..............#.........#.#...#.....#.#. + ...............#....#........#.#.......................#...#..........#...................#......... + ..........#.........##..........#............#..................#.....................#....#..#...## + ........#.............#.....................#..#............#......#.#..............#........#..#... + ............#............#.........#...............##..##....#...#...........##....##............... + .....#..#..........................#..#.....#.............#....#......#.....................#.#..... + .................................#....##.......................#.......#...........#.#....#......... + ...#...#...........#..#...............................#............#.....#....................#...#. + .......##...#...#...#........#.#......#.....................................#.......#..##........... + ..............#.#....###................#...........................#.................#............. + ...#.......#..#....#.......#.......##.....#.......#.............#...............#..#................ + ...................#......#.......#..#.#..#....................#...............##...........#....... + .#.#.................#.#..#.#........................#.........................#.............#.#.... + #...#.........................#........#..............................#...#......................##. + ..................................#..#.##......... + .#.......................##..#...#...#.......#.... + ........................##................#......# + ......#.......................................#... + ...#..........##.....#....#.#..#.........#.......# + ........##...#....#..........#...#........#....#.. + ....#..#....#.#....................#.............. + .#.....#....#......................##......#...... + ...##.....#........#.....#.#............#......... + ..................#....#........................#. + ..........#.....#......##.......#..#.#.....#...... + ......................##...#......#......#....#... + ##..............#........#......#..............#.. + ...............#...#.....................#..#..#.. + .................#.........#...##.....#..#.....#.. + #.#....#..#...#...#...##.....#.................... + ...#..#...#.................................#...#. + .#...#............................#.....#......... + ...................#....#..#...............#..#... + .....#.............#................#.#..#...#.... + .............#..#..#.....#..#.#.#................. + ..........#...#..........#..........#........##... + ......#...................................#..#.... + #.............#.....#........#.#.....#..#...#..... + ............##................#..........#........ + ..................##............................## + .#.......#.#..........#.............#............. + ..........#.............#....#..#.........#....#.. + .................#......#...#..........#.##....... + .................#....................#......#.#.. + #.....#...........#.#..............#......#....... + .......#....................#..##..........#..##.. + ..#....#.....#.........#.......................... + .#.#........#....##....#..........##..#........... + ##...............#.............#.................. + .#.....#..#........#........#..#.......##......... + ....................#....#............#........... + #.##......#....#...#...#..#.#.......#............. + ........#.....#.##...#...#....#.............#.#.#. + ..........................###...............#..... + .....#...........##.............#.#.##.....#.....# + ....#.#................##..........#.....#..#..... + ..............................................##.. + ......#.....#...............#........#............ + ....#...##.........#..#.....#........#...#........ + #....................#.#...#.................#.... + ..#...................................#.........#. + ........#..........#...............#....#........# + .......#.........#....#....#...#.................. + .#...................#......#..........#.......... +............#.#.......#...........#...##...............#...#.#...#.....#........#..#....#........... +....................##...........#...........#..........#..........#....#....#............#.#....... +#..................#........#.......#................#.....#.#......##..#.............#.........#... +.##.................................................#........#...#.......................#........#. +.#...................#........#..#..#...#............#.........................................#.#.. +....#.......#...#....................#......#....#.#.#........#.##.#.#..#.......#........#........#. +......................#................##...#.#.......#..#..#.##..#......#...#...#.................. +...#...............#..#.......#.......#..#........................#..#........#..#...#.............. +...........#.....................##...................#..#.....#..............#...............#..... +..........#......#........#....#......#.................#...........................#.............#. +#..###............#.......#....#.#.#.............................#...#...#.##..............#....#... +.............#.#.....#...............................#...................#........#................. +..................#.................................#..#...................#...#.................... +.....#..#.........#......##...........#.......#......................#..............##..#...#....... +.....#....#.........#..........#....#......##...#.#............................#...............#.... +.....#...#.#...#..#.#..............#.......##..........#...........##............#.......#..#.....#. +#.....#............##............................#........................##.#.....................# +.............#...#..#........##...............................#...........................##........ +.#.......#....#.........#.............#...#.#...................................#.....#...#......#.. +................#...........#..#......#...............#......#..#............#....#...............#. +..#........................#...............................#..........................#.#....#...... +.........#.............#......#........#...#...#.....#.......#....#..##..#..#..................#.... +...........#....#...#.......#............................#.....##......#....#.......#.............#. +.........#......................#.....#..........#..............#.......................#......#.... +..............#...........##...........#...........................................#................ +...............................#....#...#......................##...........#..............#...#..#. +......#........#........#.................#.#...#........#........#..#....................#......... +...........##..............................#...........##.............##.............#.............. +...#..###......................##...........#......#.......#..#..#........................#.#...#... +................#.....#........#.....#....#.#...#.#......#.........................#................ +....#........##.....#..........................................#..........#...............#.......#. +#.#.......#.#.......#...........#...........##..#..........................#.....#...........###.#.. +#.........#.........................#...................##......#........#.......................... +....#.......#...............................#.#..................................................#.. +...................#............#...#.....#.....#......#..........#..##.#........#.................. +.......#........#....#.....#.........................#.#.................#..#.........#..........#.. +.#..........#...#.......#.....#.......................#.......#....#..........#......#.............. +..#..#.#...........#.........#......................................#...#.......................#... +..........................................................#.....#.....................#............. +...............#......#.........#...........#.#..................................................... +.#.#.......##...........#..........#..#......#.............#...............................#....#... +.......#...#....#......#.......#...#....................#.....#..............##..................... +.................#....#............#..................#.............#............................... +#.......##...#.#....#..#...........#....................#.#...#..........##.#................#....## +.....#.......#.........#............##..#...............#..........#......#................#...#...# +.....#..........#.#.#....#............#...........#...........#......##....................#........ +..............#.......#...#................#.........#.#.#...........#.#...#........................ +........#.........#........##...#................#......#.....#.....#...........#................... +.........................#...........................#.............#.......#....................#... +.....#................................#.....#......#.........#........#.............#...#........... +.........................................#.....#.# +......#........................#.........##....#.. +.........#.............................#.......... +.....#..........#........................#........ +.............#................#................... +...#...#..........#......#............#........... +.###................................#............. +..........#........................#.............. +..#...#.........##...........##....#.............. +............#.......#.............#.........#..... +..#......#....#..#....#..#....#.....#......#...... +#................................................. +.......#.#............#..............#...#........ +.................................................. +........................##.......#...........##... +.#.............#.#...............#..#............. +............................................#..#.. +.................##....#.....#..........#......... +.............#.....##.....................#....... +...#...........#............#..#.........#.#...#.. +.............#.......#.......#...#................ +...............##................................. +............#.............................#....... +.........#....#..#.....#..........#..........#.... +#.........#..............#.......#.#.........##... +...#....................#.#..............#........ +...##........................##.#...#...#......#.# +...........................#.......#.............. +..........#....#.................#.....#.......#.. +.........#.......##.......#.#....#.......##..###.. +#....................#......................#..... +.............#...#.##................#.#.......... +.........#...#......#....#........................ +#....#..........#.....#............#...#.......... +..........#...#...#............................... +.................#...............#..#............. +#.................................#......#........ +.......#.#....#..................#................ +.##...........#...........#........#.#.....#....#. +........#..#.................#..#.#.....#.......#. +.......#..............#...#.........#............. +.......#..#.............#...........#.#....#...... +......#..#...........................#..........#. +.......#...#........#..#.....#....#.......##.#.#.. +......#.......#........#.........#..........#..... +.....#..##....#...#.#..........##.........#..#...# +#......##.....#...........#..#....#.#..........#.. +...#.........................#..#...........#.#... +#....#...............................#............ +........#.........................#........#....#. + +23L34R4L9R5L23R42L32L17L25R37R35R40R44R3L37R13R12L28R39R23L34R28L39L45L23R35R22R22R16R4R12L11L16R50L36R32R20R15R9L42L42R33R44R14R5R11R4R13L29L11L3R33L8R16L48R8L32L33L7L25L36R7L40R5R28R22L18L12R12L28L11R14L6R37L7L46R26L35R29L10L32L18R9R47R4L27L42R19R38R43L29L33R43R27R18L28L11R3L39R23L47R41L40R47R43R39R25R9L27R34L24R18R39L42L37R29R29L39R19R23L49L9R50L19R33L36L32R27R50L47R40R20R10L18L40R8L5R43R22R6L48R49R19R3L39L11R28R19L19R43L47L40L29L12L38R36R15L13L44R10L26L34R29R23L11L31L13R33L36R46R18R46R41R30R24R25L1L1R4R48L30R4R44L26L19R20L47R46L5R28L12R40L41R45R12L36R28R19R48R46R45L47R7R33L12L29R17L41L4L19L3R47R41L27L7R10R16R37R48L17L31R39L29L46L48L46L33R14R19R10L14L1R39R2R50L13L39L48R33L18R25L37L22L30R17L36R12R32L12L30R30R31R2L46L25L39R47R31R23L1L40L8L8L29R22R6L30R44L47L3L24L10L37L2L31L32L41L16L24R3L1R3R12R1R38L14L29L3R42L38R16R26L33R41R30L48L5L23R22R15L38L4L26L50R30L45R37R17L49R16L19L20R44L46R17R3R16L6R43L33L11L16R27R15L18L25L3L42L33R43R17R41R50L40R44R2R30R17R20L41R37L21R15L48R1L38L8R25R23R26L33L2L38R9L40R13L8L32L19L43L16L39R23R6R12R35L42L25R40R20R31R32L25L28R27R10R19R17L34L18R25R31L27R24L10R7L10L17R24R32L39R31L27R48L12L33R24R6L2L50L43R20R47R33R33L40R21R48L40R12L47L19L1R47R29R46L21L4R28L28R34L37L14L48L17R2R44R8R40R47L44R22L13L6L43R5L25L19R37R36L14L36R22L33R31L3R44R35R45R17L50L7L47R13L10R23L34L32L43R17L18L11R5L49L46R20L25R41R8R3R26R13R35L41L42L27R5L31L16L45L18L23R32R2R23L4R47L3R10R30L41L46R37R17R36L20R40L48R27L48L50R40R44L25L23L40L3L38L45R15R28R33R31L6R36L27L44L21R10L9R10L26L34L48L16R12L7L33L6R25L17R40L6L26R17R33R38L35R18R41L26R19L17R45R40R16L42L18L48L33L6R45R39R18R48L17R2L48R27R31L25L46L19L36R9L9R8L26L41L14L47R30L32R18L47L10L34R40L27L34R27R36L50R20L22R33R23R24L22L19L23L6L28R49R47R31R23R10L3R6R25L5R28R18R50L40R47L1L20L39R4R39L34R21R2R39L33R31R47L21L46R15R41R17R32L2R1R26R9L1L12L31L7R45L32L39R46R29R13R5L13R41R21R29L6L12L20L50R43L32L22R5R7R37L43L13R42R11R21R14R49L46L23L15L32L10L17L17R45L10L22L7R49R4L32R36L45R16L32R4L39R8L24L38R43R22R36R26L18R29L13L41L43L9R31L5L41R14R10L3L22R27R3L35R4L35R15L33L21R31R5L35R35L26R35R44L42L29R46L20L44R38L32L16L5L16R15R3R48L10L36R16L8R34L29L21R22R29R22R10L23R22R45L38L18R17L38L1R8R48L18R10L25L42R22L44L33L41R44L13L34R10L22R1R11L8R15L18R15L9L21R11L38R4R45R29R41L45L18R12L36L9R16L38R33R15L31R15L33L42R28R49R1R17R5R46L44L13L33L40L19R42L18R25L28R44R4R46L32L2R34L25L46L6R49R10L20R20R29R12L13L21L48R46L37R21R9R21L23L36L36R1L12L8L30L3R35R20R4R32L22L50L46L25R32L45R50R3R50R36R39R21R32R39R36L48L49R49L29L13L15R36R47L30L49L33R24L28L29L44R1R16R27L39R8R33R31L37L20L26R32L1R38L16L21L44L23L46R49R42R31R43R14L45L8R37R7L39L24L49R41L2L31R15L49L29L24L21L40R16L32L50R22L10R10L47L4R17L1R35R17R29R12L27L19L42L44L50R39R1L19L30L38R44L21L44L33L32R18L34L6R16L6L46L40R36L20L46L42L24L44L34L8L24R25L13R26L37R38L10L49R22L45R12L26L6R25R45L42L6R7L34L26R7R37L39L43R6R46L41L35L34R42L12L39L35R17L25L37L13L20R29R22R13L21L31L3L23L6L15L43R21L2L47R11R1L39R23R16L41L25L38L39L48L49R36L27R27R24L20L26L37R15L14R15R42R4R9L38R22R48R15R5L34L7R26L7L13L2R35R21R9L11L9L40L43R42L23L45R8R36R5L19R30R9L45R23R46L50L47R48L19R18R25R8L27R39R29L32L38R13L50L11R45L47L50R49L25L47R10R37R19L18L22R43R47R22L28R43L49R46R38L47R28R43L21L19R4L10L6L31L41R35R31L10R22R45R43L49L48L18L34R50L46L50L43R24R24R48R47L6R49R44L37L7L37R5R9R24L8L15R1L33R9R5R40R45R48L37L40R17R41L38L24L23L31L21R23L11L39R3L42R27L13R18L49L28R40L42L31L30R43R43R14R26L12L35R25L4L14L28R48L47R3L30R8L44R10L45R26L5L27L40R35L5R37L22R26R19R41L15L19L21R12R37L26L47L35L37R48L45L16R20R39R41R8R25R29L14R4R32R1R4L17L23R34L2L19R26L27R2L11R35R24L36L17R32L41R31R12L50R49L25R25L44L15L13L17L50L29R29L26L25L28R5R24R16R35R17L44R48R38R12R13R7L49R2L22R44R34R19L29L38L1R37R36R6L34L4L16R25R39L22R47L50L30R28L10L35R48R36L27L8L13L27L38L30R3R31L29L23L13R38L41R45R43L12L15L45L4L9L1R23R34R14L33R26L35R41L3R5R28L39R27L44R17R46L2L18R11R34R14L26R49R37R45L12R21R31R18L5R37L19L36L39R4R33R33R39R13R9R29R43R20L18R17L39R37L37R9R23L34L40R26R40L10R34R21L13L34L7L18R15L15R20R20L31R33L44L38R30R20R45L9L41L22R11L17L8R5L49R24R28R13R34R4R33L16R30L31L47R7L14R45R41R17R14L18R11R41R41R1L43L4R31L16L18R1R47L43R2L37R29R9L4L13R6L47L2R47R34R38L44L10L49R41L37R16R41R50R9L41L49L16R33L25L1R45L32L33L1R17L22R22R9R18R20L37L10R4L44L26L50R30L35R6R36R24L19L25L24L48L43R6L44L12R45R36L28R46R7L40R19L37L47L42R1L15R28L48R37R25L36R50L25L6L34R47R10L17L49R21L17L24R7R2R28L11R50L30L37L20L22L9L45R10R22L36R10L41R49L11R1R20R27L45R15L32L30L43L50R40L32L19R27L45L13L26R27R46R39L46L20L28L5L6R20R4L10L44L47R5R27L34L37L39R44L30R5R5L49L38R35L9L19R15L35L33R6R17L38R40L40R38R14R8L5R10R38R2L8R47R23R27L50L39R12L5R6R39R48R26R17R48L3R6L34R42R38L14L34R20L4L4L37R6R1L9R11R13L14R30R22R30R39R3R22L29L48R16R11L14R17R43R46L16R31L5L1R33L6L35R31R18L35L41L11R43L10R41R41R28L20L34L9L6L46R30L9L28R7R9L23L1L14R50L24R27R22L13R45R1L42R48R18L27L6R13R36L43L35R24R46R4R28L31L28L50R19L37L39R30R41L32L27L18L44R3L27R34R5R25L50L32R12R47R13L48L10L28R40L15L17L17R17L19L18L22R1R47R40R35L21R12R49L35L2R27L8R44R35L48L4L37L24R37L36R28L48L37R23R25R32R21R37L45R18R19L48R5L46L3L42R33L22L4R3L14L44L25R46L45R12R40L6L24L40L21L40R3L8R34L34L40R34L19R22L48R15L30L34R19L12L14R20L3L9R30R33R33R14L44L50L2R5R7R7L33L25L9L21R29R29L10R37R17L35L17L16L37L33L27L50R33R35L7R6R24R50R7R25R41R43L12R45L8R46L1R9L36L25R39L24L3R7R16L39L44R45L46L4R14L20R21R32L10L48R33L45R24R34L4L32R11R44L34R19R21R26L14R36L43L17L16R34L15L16R9R19R45R29R43L13L7R14R1L47L29L9R29L50R47L47L18R45R44L49L24L23R11L20L35L10L33L3L27R30R16R50L24R14L37R34L40L15R13R40L46L30L17L45L41L4R20R12R11L32R27R48L13L40L6R38L13L37L43R45R24L45L46R16L24L9R47L43R28L43L7R7R44R23R47R47L9R38L45L47R10R32R12L45R4R2L7R27R34L11R24L32R8L50L29L44R1R40L46L35L21R26R12L20L27L38L48R42L14L9L45L26L16R21R49R23R7R32L18R45R12R27R50L44L43L39R26R11L48L13R40L9R41L29R32L5L16L23L26L12L39R1R22L11L31R29R40L31L18R39R30L35R40 diff --git a/src/bin/day22_1.rs b/src/bin/day22_1.rs new file mode 100644 index 0000000..3a494dd --- /dev/null +++ b/src/bin/day22_1.rs @@ -0,0 +1,8 @@ +use std::fs; + +use aoc2022::day22::process_part_1; + +fn main() { + let file = fs::read_to_string("./inputs/day22.txt").unwrap(); + println!("{}", process_part_1(&file)); +} diff --git a/src/bin/day22_2.rs b/src/bin/day22_2.rs new file mode 100644 index 0000000..de3f28d --- /dev/null +++ b/src/bin/day22_2.rs @@ -0,0 +1,8 @@ +use std::fs; + +use aoc2022::day22::process_part_2; + +fn main() { + let file = fs::read_to_string("./inputs/day22.txt").unwrap(); + println!("{}", process_part_2(&file, 50)); +} diff --git a/src/day22.rs b/src/day22.rs new file mode 100644 index 0000000..a7aca48 --- /dev/null +++ b/src/day22.rs @@ -0,0 +1,434 @@ +use itertools::Itertools; +use nom::branch::alt; +use nom::bytes::complete::tag; +use nom::character::complete::{digit1, newline, one_of}; +use nom::combinator::map; +use nom::multi::{many1, separated_list1}; +use nom::sequence::separated_pair; +use nom::IResult; + +pub fn process_part_1(input: &str) -> usize { + let (grid, instructions) = parse_input(input).unwrap().1; + let mut player = Player { + x: grid[0] + .iter() + .position(|t| matches!(t, Tile::Free)) + .unwrap(), + y: 0, + dir: Dir::R, + }; + for i in instructions { + player.walk1(&i, &grid); + } + 1000 * (player.y + 1) + (4 * (player.x + 1)) + isize::from(&player.dir) as usize +} + +pub fn process_part_2(input: &str, cube_size: usize) -> usize { + let (grid, instructions) = parse_input(input).unwrap().1; + let mut player = Player { + x: grid[0] + .iter() + .position(|t| matches!(t, Tile::Free)) + .unwrap(), + y: 0, + dir: Dir::R, + }; + for i in instructions { + player.walk2(&i, &grid, cube_size); + } + 1000 * (player.y + 1) + (4 * (player.x + 1)) + isize::from(&player.dir) as usize +} + +#[derive(Debug)] +struct Player { + x: usize, + y: usize, + dir: Dir, +} + +impl Player { + fn walk1(&mut self, instr: &Instruction, grid: &[Vec]) { + match instr { + Instruction::Left => { + self.dir = (isize::from(&self.dir) - 1).rem_euclid(4).into(); + } + Instruction::Right => { + self.dir = (isize::from(&self.dir) + 1).rem_euclid(4).into(); + } + Instruction::Walk(dist) => { + for _ in 0..*dist { + let (next_x, next_y, next_tile) = match &self.dir { + Dir::R => { + let row = &grid[self.y]; + let (next_x, next_tile) = row + .iter() + .enumerate() + .cycle() + .skip(self.x + 1) + .find(|(_, t)| !matches!(t, Tile::None)) + .unwrap(); + (next_x, self.y, next_tile) + } + Dir::D => { + let col = grid.iter().map_while(|row| row.get(self.x)); + let (next_y, next_tile) = col + .enumerate() + .cycle() + .skip(self.y + 1) + .find(|(_, t)| !matches!(t, Tile::None)) + .unwrap(); + (self.x, next_y, next_tile) + } + Dir::L => { + let row = &grid[self.y]; + let (next_x, next_tile) = row + .iter() + .enumerate() + .rev() + .cycle() + .skip(row.len() - self.x) + .find(|(_, t)| !matches!(t, Tile::None)) + .unwrap(); + (next_x, self.y, next_tile) + } + Dir::U => { + let col = grid.iter().map_while(|row| row.get(self.x)).collect_vec(); + let (next_y, next_tile) = col + .iter() + .enumerate() + .rev() + .cycle() + .skip(col.len() - self.y) + .find(|(_, t)| !matches!(t, Tile::None)) + .unwrap(); + (self.x, next_y, *next_tile) + } + }; + match next_tile { + Tile::Free => (self.x, self.y) = (next_x, next_y), + Tile::Wall => break, + Tile::None => unreachable!(), + } + } + } + } + } + + fn walk2(&mut self, instr: &Instruction, grid: &[Vec], size: usize) { + match instr { + Instruction::Left => { + self.dir = (isize::from(&self.dir) - 1).rem_euclid(4).into(); + } + Instruction::Right => { + self.dir = (isize::from(&self.dir) + 1).rem_euclid(4).into(); + } + Instruction::Walk(dist) => { + for _ in 0..*dist { + let (next_x, next_y, next_dir) = self.next_coord(size); + let next_tile = &grid[next_y][next_x]; + match next_tile { + Tile::Free => { + self.x = next_x; + self.y = next_y; + self.dir = next_dir + } + Tile::Wall => break, + Tile::None => unreachable!(), + } + } + } + } + } + + fn next_coord(&self, size: usize) -> (usize, usize, Dir) { + let face = get_face(self.x, self.y, size); + let (xl, yl) = global_to_local(self.x, self.y, size); + match face { + 1 => match self.dir { + Dir::R => (self.x + 1, self.y, Dir::R), + Dir::L => { + if xl == 0 { + let (nx, ny) = local_to_global(xl, size - yl - 1, 4, size); + (nx, ny, Dir::R) + } else { + (self.x - 1, self.y, Dir::L) + } + } + Dir::U => { + if yl == 0 { + let (nx, ny) = local_to_global(yl, xl, 6, size); + (nx, ny, Dir::R) + } else { + (self.x, self.y - 1, Dir::U) + } + } + Dir::D => (self.x, self.y + 1, Dir::D), + }, + 2 => match self.dir { + Dir::R => { + if xl == size - 1 { + let (nx, ny) = local_to_global(xl, size - yl - 1, 5, size); + (nx, ny, Dir::L) + } else { + (self.x + 1, self.y, Dir::R) + } + } + Dir::L => (self.x - 1, self.y, Dir::L), + Dir::U => { + if yl == 0 { + let (nx, ny) = local_to_global(xl, size - 1, 6, size); + (nx, ny, Dir::U) + } else { + (self.x, self.y - 1, Dir::U) + } + } + Dir::D => { + if yl == size - 1 { + let (nx, ny) = local_to_global(yl, xl, 3, size); + (nx, ny, Dir::L) + } else { + (self.x, self.y + 1, Dir::D) + } + } + }, + 3 => match self.dir { + Dir::R => { + if xl == size - 1 { + let (nx, ny) = local_to_global(yl, xl, 2, size); + (nx, ny, Dir::U) + } else { + (self.x + 1, self.y, Dir::R) + } + } + Dir::L => { + if xl == 0 { + let (nx, ny) = local_to_global(yl, xl, 4, size); + (nx, ny, Dir::D) + } else { + (self.x - 1, self.y, Dir::L) + } + } + Dir::U => (self.x, self.y - 1, Dir::U), + Dir::D => (self.x, self.y + 1, Dir::D), + }, + 4 => match self.dir { + Dir::R => (self.x + 1, self.y, Dir::R), + Dir::L => { + if xl == 0 { + let (nx, ny) = local_to_global(xl, size - yl - 1, 1, size); + (nx, ny, Dir::R) + } else { + (self.x - 1, self.y, Dir::L) + } + } + Dir::U => { + if yl == 0 { + let (nx, ny) = local_to_global(yl, xl, 3, size); + (nx, ny, Dir::R) + } else { + (self.x, self.y - 1, Dir::U) + } + } + Dir::D => (self.x, self.y + 1, Dir::D), + }, + 5 => match self.dir { + Dir::R => { + if xl == size - 1 { + let (nx, ny) = local_to_global(xl, size - yl - 1, 2, size); + (nx, ny, Dir::L) + } else { + (self.x + 1, self.y, Dir::R) + } + } + Dir::L => (self.x - 1, self.y, Dir::L), + Dir::U => (self.x, self.y - 1, Dir::U), + Dir::D => { + if yl == size - 1 { + let (nx, ny) = local_to_global(yl, xl, 6, size); + (nx, ny, Dir::L) + } else { + (self.x, self.y + 1, Dir::D) + } + } + }, + _ => match self.dir { + Dir::R => { + if xl == size - 1 { + let (nx, ny) = local_to_global(yl, xl, 5, size); + (nx, ny, Dir::U) + } else { + (self.x + 1, self.y, Dir::R) + } + } + Dir::L => { + if xl == 0 { + let (nx, ny) = local_to_global(yl, xl, 1, size); + (nx, ny, Dir::D) + } else { + (self.x - 1, self.y, Dir::L) + } + } + Dir::U => (self.x, self.y - 1, Dir::U), + Dir::D => { + if yl == size - 1 { + let (nx, ny) = local_to_global(xl, 0, 2, size); + (nx, ny, Dir::D) + } else { + (self.x, self.y + 1, Dir::D) + } + } + }, + } + } +} + +// Look, i'm just hardcoding it +// _12 +// _3_ +// 45_ +// 6__ +fn get_face(x: usize, y: usize, size: usize) -> usize { + if y < size { + if x < 2 * size { + return 1; + } + return 2; + } + if y < 2 * size { + return 3; + } + if y < 3 * size { + if x < size { + return 4; + } + return 5; + } + 6 +} + +fn global_to_local(x: usize, y: usize, size: usize) -> (usize, usize) { + let face = get_face(x, y, size); + match face { + 1 => (x - size, y), + 2 => (x - 2 * size, y), + 3 => (x - size, y - size), + 4 => (x, y - 2 * size), + 5 => (x - size, y - 2 * size), + 6 => (x, y - 3 * size), + _ => unreachable!(), + } +} + +fn local_to_global(x: usize, y: usize, face: usize, size: usize) -> (usize, usize) { + match face { + 1 => (x + size, y), + 2 => (x + 2 * size, y), + 3 => (x + size, y + size), + 4 => (x, y + 2 * size), + 5 => (x + size, y + 2 * size), + 6 => (x, y + 3 * size), + _ => unreachable!(), + } +} + +#[derive(Debug)] +pub enum Tile { + None, + Free, + Wall, +} + +#[derive(Debug)] +pub enum Instruction { + Walk(usize), + Left, + Right, +} + +#[derive(Debug)] +pub enum Dir { + R, + D, + L, + U, +} + +impl From<&Dir> for isize { + fn from(value: &Dir) -> Self { + match value { + Dir::R => 0, + Dir::D => 1, + Dir::L => 2, + Dir::U => 3, + } + } +} + +impl From for Dir { + fn from(value: isize) -> Self { + match value { + 0 => Dir::R, + 1 => Dir::D, + 2 => Dir::L, + 3 => Dir::U, + _ => panic!("invalid direction"), + } + } +} + +fn parse_input(input: &str) -> IResult<&str, (Vec>, Vec)> { + separated_pair(parse_grid, tag("\n\n"), parse_instructions)(input) +} + +fn parse_grid(input: &str) -> IResult<&str, Vec>> { + separated_list1( + newline, + many1(map(one_of(" .#"), |c| match c { + ' ' => Tile::None, + '.' => Tile::Free, + '#' => Tile::Wall, + _ => unreachable!(), + })), + )(input) +} + +fn parse_instructions(input: &str) -> IResult<&str, Vec> { + many1(map(alt((digit1, tag("R"), tag("L"))), |c| match c { + "R" => Instruction::Right, + "L" => Instruction::Left, + dist => Instruction::Walk(dist.parse().unwrap()), + }))(input) +} + +#[cfg(test)] +mod tests { + use super::*; + + const INPUT: &str = " ...# + .#.. + #... + .... +...#.......# +........#... +..#....#.... +..........#. + ...#.... + .....#.. + .#...... + ......#. + +10R5L5R10L4R5L5"; + + #[test] + fn day1() { + assert_eq!(process_part_1(INPUT), 6032); + } + + #[test] + fn day2() { + // no test, because the test folding is different than the input folding + // SO FUCK THAT GARBAGE + // assert_eq!(process_part_2(INPUT, 4), 5031); + assert_eq!(0, 0); + } +} diff --git a/src/lib.rs b/src/lib.rs index 8623deb..03f582c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,3 +22,4 @@ pub mod day18; pub mod day19; pub mod day20; pub mod day21; +pub mod day22;