My attempts to solve puzzles of Advent of Code
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

WIP: day 22

+907 -2
+202
data/day22.txt
··· 1 + ........................#.#...........#........#.#.......#......#......#........#..#........#....... 2 + ....#.........#....##....#......#................................................................#.. 3 + ....#...#.#................##...#..#.....#.#..#.............#...........#...#..##......##.....#.#... 4 + .#.......#.........#...##....#.............#..#...#....#..........#.......#.............#........... 5 + .##..............#.#.#.....#........#.##.#.........#.........#.......#.....#........#.....#.#....... 6 + ........#.........................#..........#..............#.........#...#...............#........# 7 + ......#...#...........##.................#.....#....##.....#.............#.......#..#........#...... 8 + .............#.........#.#.....#.......#....#.............#..#.##.......#....#...............#...... 9 + #.............................#........#........#.......#...........#..#....#................#...... 10 + .......................##...#.#.........#..............#............................................ 11 + ...........................#.#...#..#....#..#.....#.........................#...............#......# 12 + ..#..#......#.#..................................##.......#.............#....#.#.................... 13 + ....#.....#.#..................#..................#...#........#..#............................#.#.. 14 + ...................#.#........#.........#.#...........#......#............#..#...................... 15 + ..............##..###.................................................#.......##.................... 16 + .....#.#.....#.......#..........#...#.##....#.........#.....#.........#.........................##.. 17 + .#.......#................#.....#.....#..##........#..................#.....##.........#.....#...... 18 + ........#................#.....#..##.....#..............................#...#.............#...#..#.. 19 + .#.................##.#.......#........................#.#...................#.#...#........#.#..... 20 + .........#..#...................................#...#...........#..#.#.............#...#............ 21 + #..#......#........#.......................#...................#......................#............. 22 + ...........................#.............#.......#.....#......#..#.......#...#........#.#...#..#.... 23 + ....#.#...........#................##...............#...#...#......#..#............................. 24 + ......#.........##...##..................#.........#.................#.............................. 25 + ....................#......................#.#......#.......#.....#..#.............................. 26 + ...................#....#....................#................................................#..... 27 + #..#......#...........#....................#................#.......#...#...........#.........#..... 28 + ..............#............#....................#.#..#.............#.............#..#...........#... 29 + ..........#..#.##..........#....................#....#...#......................#.................#. 30 + ............#...............#......##............................................#........#......... 31 + ....#..#...................#..#......#.....#...#.#....#.........#..#........#...........#.........#. 32 + ..........#...........#.#......................#..........................#.#....................... 33 + #................#.....#...#.......................#...............#........#.#.....#..........#.... 34 + .......#...........................#......................#...#.#..............#.....#.............. 35 + ...#.....#.....................#.........#....#.....#....#........##.........#.#.#.................# 36 + ####............#.#............#...#........#....#...#.......#..................#............##..... 37 + ..........................#...................................#...............#........#...#........ 38 + .......#....#.........................#...#............................#.......................#.... 39 + ........#.....................#..#...#.............#..##.#...#....#..#..........#.#.............#... 40 + ....#............#...#............#.............................#...................#....#......#.#. 41 + ........#.........#...#.#........#..#.......#..........#..........#..#...#..........#............... 42 + ........#.............................#...............#............#.......................#..#..#.. 43 + #............#.....#..................#.............##.#............#...#..#.#........#............. 44 + ............................#..........................#.........#.................#.......#....#... 45 + ..#.#..............................................#...................#......#..#........#........# 46 + ..#.....#.#............#.#.#.#.............#...........................#...........#.#.#...#........ 47 + ....#...#.........##.........#..#......#...........#.........##......................#.............. 48 + .#.....#................#............##..#........#....#.#...............#.##...#...........#......# 49 + ...#.......#...........................#..................#.#..##.#...#.#........................... 50 + ..............................#.....#...#.......#.........#...........#.......#..................... 51 + ...........#.....#........#.........#.....#.....#. 52 + ...........#.........#....................#....... 53 + ...........#...........#.........................# 54 + ...#......#......#............#................... 55 + ........#............................#.#.........# 56 + #..................#...................#.#........ 57 + ...#................#...#...#..#.......#.#......#. 58 + ...#.......#............#...#..................... 59 + ........#.................##...................... 60 + .#...#.........#..........#.......#.......#....... 61 + ..................##.##.........#................. 62 + .#.#..............................#.....##..#..#.. 63 + ..........................#....##...#.......#.#... 64 + .........#....#.....#..........................#.. 65 + .......#.....#.................#.#.##............. 66 + .............#..........#.....#................... 67 + .......#.#.........#.....#................###.#... 68 + .................................#................ 69 + ..............#...#..#............................ 70 + .............#........#..........#....#........... 71 + ................#..........#..#...##....#........# 72 + .......#.#.......................#....#..........# 73 + .................................................. 74 + ...........................#.......#...#.....#.... 75 + ........##..............#.#...#.......##..#....... 76 + .....................#.....#........#............. 77 + ...#.......................#.......#.....#..#..#.. 78 + ..##...........#................#.#............... 79 + .........................#.#....#.#.........##.... 80 + ..#......#........#...#....#..#...#.............#. 81 + .....#.......#............###...................#. 82 + ...#............#...........#.......#......#...... 83 + .........#.....##..........##............#........ 84 + ..#..##...#..................#.#.........#....#... 85 + .......#....#............................#..#..... 86 + ...#....#...........#.#........#.................# 87 + ......#........#.....#........#................... 88 + ............#..........#....#..#...........#...... 89 + ................#....#.....#........#...........#. 90 + ...##....#................#.#.....#.............#. 91 + ..............................................#..# 92 + ..#..#..#..#..#................................... 93 + .............#..............................#..... 94 + ..#...#....#...........##......#..............#... 95 + .........##....#...........................#.....# 96 + ...........................##.........#........#.. 97 + ......#.............#........#..........#....#.... 98 + ................................#............#.... 99 + ...........#..#...........#...#.#.........#....... 100 + ...................#.#.#........#.............##.. 101 + .#..........#..............................#..........#...#.#..#...#.......#............##.......... 102 + ..##...........#.........#.........................##......#.............#................#......... 103 + #.#.........#.....#.#............#.........#.#.#....##..............#........#.#..#................. 104 + ..............................#..#.......................................#....##.....#........#..... 105 + ............................##....#..##.....###.....#.......##...#...............................#.. 106 + ...........#....#...........#.......#..........#.........#........#.............#.........#......... 107 + .............................#...........................#.........#..................#....##....... 108 + .................#...................#...........#.............#...............#.................... 109 + ..............#................#.............#......#....#..........#.#..#.......................#.. 110 + .#........#...##.......#.#...#......#...........#...................#....#......##...#.............. 111 + ............#..#.#......#..#.................#....#.....#.#..#.......................#.............. 112 + #.#..#.#.........#........#...........#.#.....#...#..#........#..#........#.......#.#.......#....... 113 + .......##.......#.........#..#.......#...........#...............#.................................. 114 + ...........#.........#.................#.......#..#.#......#...............#.#...................... 115 + .............#........#..........##.......................#...#..............#....................#. 116 + ......#...#.#....#......#........#.......#.................#.....#....#..#..#.....#.......#......... 117 + .......#.##.............##.#...##....#.#........#.........#..............#...#......#............... 118 + ...#..................#.#.......#.............#.............#......................................# 119 + .#............#..........#...........#.#.............#..#.......................................#... 120 + .....#.##...#...#...............#.....................#...#.#.....#....................#............ 121 + ........#......................................#...........#......#..#..##...........#....#..#...... 122 + ..#...................#................#.................................#.......................... 123 + .#.......................#.......#......#.........................#................................. 124 + ...#...#...................#...........#....#........#....#..............#.......#.................. 125 + .....#....................#..........#..#........#...........................#..#...#.......#...#..# 126 + .........#...........#...#...#............#...................#.............#....................#.# 127 + ..#....#.....#.#.......#................#...#.............#.............##......#.................## 128 + ....##...#...............##..........#....#.........#.....#...............#........#...............# 129 + .....#.................#.......#.................#.............#.#....#....................#.#...... 130 + .........#...#...#......................................#.....................#..................... 131 + .........#..............#.....#.............................#....#.....#............#.............#. 132 + ............#.#......#.....#...........................#........##.......................#....#..#.. 133 + ........................#.............#..........#.#...........#..#....................#.##....#.... 134 + ........##.#....#................##.........................#........#...........#.......#.........# 135 + .#..........#....#...........#...........#..........................#.........##...........#........ 136 + ...#....#..#.................................#......#.........#.#....#.........##.....#.......#..... 137 + #.....#....#......#...#.........#...........#..#............###........#...........##......##.#...#. 138 + .......#.#.....#....................................#....#....................##.#.................. 139 + #....................................##.....................##....#...........#..........#.......... 140 + .........................#......#....#...........#..#......#.........#........#..#...............#.. 141 + ...#....#.........#..................#..................................#.....#.......#.#.#......... 142 + ........................#....................#..#...............#............#......#............#.# 143 + #.................#......................#..........#.........#.......................#.......#..... 144 + ..#....................#.................#.....#..........#...............##.#............#......... 145 + ...........................#......#..............#.....#...#......................................#. 146 + ..................#..#..#...................#..#...#......#..#.........#..##..#...#....#............ 147 + ...#........#.#......#....#...................#........#.#...#..................#............#...... 148 + ....................#..........#..#........#....#.......#..........................#.........#...... 149 + .......#...#..........................#........##............#..#................................#.. 150 + ........##.......#................#.....##..#...........##.............#...##........#.....#.......# 151 + #.................#....#..............#...#....... 152 + ..............................#..................# 153 + #........................#....##...#........##.... 154 + ............#..................................... 155 + ............#....................................# 156 + ...#.#..............#..............#.............. 157 + ...........................##....................# 158 + ......#.#.......#..................#.....#...##..# 159 + ......#..............#..#.............#........... 160 + .....#.....##.....#............................... 161 + .............................#....#...#........... 162 + ........#......................................... 163 + ................#..........................##..... 164 + .#............##.#..............#.#...#........#.. 165 + .......#....#...#.....##.#...#...#................ 166 + ................#.#...................##.......... 167 + ..............................#................... 168 + ..............##.................#..........#..#.. 169 + ..#..................................#............ 170 + ..........................................#..#.... 171 + ...#......#...#....#..............#.............#. 172 + .#....#..#...............................#........ 173 + .......#.#.....................#.......#.......... 174 + ....#................#............................ 175 + .......##.....##....#................#............ 176 + ...#........#........#......#...#.#.........#..... 177 + ......##.#................#...............#.#..... 178 + ....#.#........#................#...........##.... 179 + ..#......................#....#.................#. 180 + ..................#.............#..........#...... 181 + ....#.....#....................................... 182 + ....#.......##..#.#..#.......................#.... 183 + .................#...........#.....#.....#......#. 184 + .......#.........#.......#.##....#........#....... 185 + ................#.......#...................#..... 186 + #.#..................................#......#.#... 187 + #........#..##.......##..#........#............#.. 188 + #..............................................#.. 189 + .#.....#.......#..##........#..#..............#... 190 + .........#.......#...#.....#.#...#................ 191 + .................................#..........#..... 192 + #...#..............##..............#....#......... 193 + .............................#........#....#...#.. 194 + ...#....#..............#...........#......#....... 195 + ....#......................#.......#...#.......... 196 + ...#.............#......#.#.......#..#.......#..## 197 + .......#...........................#............#. 198 + ........#.......................#................. 199 + ..........#..#...#......##.........#........##.... 200 + .#.....#....###.............#.##.................. 201 + 202 + 16L48L13R34L44L21R31R17R42R37R48L39L40R16R29L12R23R8L42R19L26L31L34R8L37R35R22R22L34R4L11R28L42L30L42L24L40R31L14L2L46L1L15R45R4L44R6L13R20L38R16R26L8L6R15L47R10L13R30R27L21L5R16R23R24L23L13L3R14R3R41R17R18R35R45R50L10R28R28R23R29R43R33R6R10R3R49R48R17R18R10L10R36L24R43L41L15R35L16R47R27L1R24L6R50R30L35R46R9L5R8L49R45L9R41L29L25L46R17L47L32R25L7R24R33L23L20L12R43L16L8R38L9L30L33R4R41L6L41L49L36L34L41R12L12R41R23R29L24R39L17L9R4R17L10L30R43R27L29R39R44L16L24R28L14L14L45L6R4L26R17L22L28L31L33R14L21R24L10R20L17R5R18L41L20L1R39R23R8R21L47R21L12R5R1L14L23R50L2L43L45L14L15L50R18R21L39L24R8R7L13R33L15L40R29L34R38R42L5L17R28R49R35L45L4L1R8L33R22L12R37R26L48R45L47R39R35L50R18L22L19R31R10L7L10L27L28R6R28L48R28L44L11L37R48R33L13L23L45L7R34L24R10R47R21R39L11R12R14R7R39L50R39R40R50R3L20L15R6L14R31R30L19R14L2L23R6R5R50R42L4L13R18L45R4R44R15R28R27L35L25R15R18L37R31L38L23L21L36L7R17R24L22L18R19L43L7L1R26L6R42R2R38L9L30R13R37R34L41R20L39R44R11R27L48R30R37L19L30L34L44R17R50R49L46L37L15R35R42R25L19L44L40R5L47R19R44L6R39L1R11R42L27L37R37L41R6R36L47L4R31R34R3R32R7L46R8R3R48L33R39L29R43R40R39L5L25R42L37R15R32R50L36R38L29L30R6L22L17R28R21L42L44L41R35L40L45L22R28R24L2R29L14L7R31L40L14L13L21R12L40L23R34R22L37L24R34L23R33L41R26R18R34L27L15L21L29R21R30R10L10L19L45L42R3R9R18L39R14L21R6L24R44L7R16R24L23L14L28R34R33L26L44R36L23L17L40L23R10L49L8L2L22L45R34L46L1R41L37R26L22L31R12R29R24R44R26L22R20R9L6L36R23R21R44L26R9R32L9R23R15L10L28L48L41R45R35L30L25R41R29L25R23R3R29L30L28L34L29R40L30R40L3R19L36R11L26R48L17L48R12R32R35L26L42R15R6R42R8L49R30R3R14L16L44L8R10L50R18R32R1R29R34R32L24R50R44L13R48L4R20L14L50L15L21R47R3L7R37L15R42R5L40R27L18R3R44L4R47R7R35L46R40R45R24R23R48R9R48R21R7L15L42R27R14R30R48L5L38R5R16L25R22L28R48R17L27R10L25L13R6L7L21R47L43L41R33R6R40R31L32L20L1L16L19R23L28R16R44L26R21R9R29L6L37L31L16L9R37L24L4L19L20R40L3L38L39R17L38L21L26R6R19R12L34L15L31L23L1R8L35R13L23L2L21L20L22R16L49R41R30R13L46R26R50R1R42R40R40R42L22L19R31R4L4L1L45R8L39R3L42R4L44L29R10L17R19L24L43L50R28R8R3L49L8R25R11R14R41L39L47R28R45L34L29R49R38L43L3R15R38L17L37R49R11L8L15L28R3L1R7R1R1L7R17L47L1R28L49L18L20R43R30L20L37R18L3L16R32L9R48L21L22R35L46L31R20L13L16R46R9R49L32L38R42L13L1R16R10L14L45L27L1R4R10R6L38R42L25L33L15L11R17R34L12R47L46R41L42L19L26R22R28R37L26R41L21R8L25R18R26R37R18L38R5R3R8L25R20R2R38R50R17L36L9L6R44R23R5L38L37L1L1L30R28L19L33L25R35R23R32L50L46L42L4L9R12L46L7L2L2R45R11L19R2R47R33R11R25R42R8R41L24R36R7L45L29L20L35L28R3R43R33R25R25L1R33R2R34R25R24R35L46R6L48R37L21R45R5L41L44R32R46L17R5R19R47R39L4R20R26R8R24L30L15R29R5R7R33R30L49R34L48L2L47R1L15L1L25L47R15L14R1R15R20R42R10R44L7L46L28L15R42L3L44L49R14L28R43R33R33R26R3R9R36R24L1L45L26L25L39L15L1R40R47R30L7R25L50R38R6L33R48R28L2L11L41L14R43L48L11L47L39L18R15L23R26R44L27L17L45L21R6R16L37L6L37R50L39L34R34L19R19R32R14R43L16L19L20L44L40L11R49R2L19L17R1L42R9L21L5R2R47L39L29L25L41R19R43R26L41R49R46R9L2R38L22R43R6L5L14R29R27R34R43L25L46R22R13R1R18R28L24R34L45R27L43R6L32L12L35L39L38R16R39L13L36R22L19L20R33L3R13R15R28L45L5R25R9R32L32L34L23R25R43R13L23L45R34R46L9R13R15R9R16R21L38L7L32R1R22R34R44R18L39R43R5R7L19L16L39L26L28L23R41L32R18R22R41L46L34R6L44L29R25R26L5L18R6R44R48R22L47L50R29R24R41L8R48L5R50L49L38R47L39R7L15R23R36R14R48R17R14R32L25L36R10L36R17L30L22R15L19L10L3L24R36R1L6L26L39L48R15L3L26R11R49R2R46R28R18R13L50R39L5R29L31L40L44L43R49L6L26L33L49L30R48R10L4R6L26L15L18L31R20L48R7R17L40R36L41R49L25L21L5R48R19R3L48R46R22R22L50L39R18R17L13L4R5L28R10R40L30R40R19R11R49R9R32L25R48R47L26L40L38R44R22L22R37R3L16R34R11R48L47R2L29R48R31L4R44L40R20L40R30R15R11R43L30R5R40L45R42R39R19R49L41R26L18R17R15L39L26L10R15L30L38L44L47R35R42R40L26R46R11R22R25R28L12R35R1R38L2R40R3L5L15R18R21R46R1R26R42L7L38L11R4L17R45L46L11R39R45R27L8L35R3L50R25R14R3R5R46L6L31R40L28R12L35L1L33L32R49R48L16L20L3R20R50R8L29R44L12R17L12R4L7R2R1R50L30L4L17R12R43L17R4L20L32R46R39L42L18L30L19R15R37L14L5L3R36L31L16L10L10L26R45R19R38L30R49L6L25L30R27L41L17R27R47L37L28R22L44R18L48L44R2L33R2R9L13L1R13L30R43L50R17R32R14R28L1R29L6L2L37R40R12R1L16R32L31L35R10R4R18R41L15L29R3R43L24L29R43L37R33R16L5L12L32R43R43R2R39R41R20R39R6L35R21R44R44L25L31L7L49R42R20L24L3L7R26R35L4L46L37L33R38L33R25L4L1L50L19R3R3R9L25L2R15L7L10R37L5L18R50L23R4L16L19R8R5R36L25L27R18R26L50L46R10R31R31R30R29L24L8L14R40R4R47R5R37R46R35R48L14R23R1L21L47R45R10L8L15R48R15R11L24L39L3R7L38L8L47R37R10R38L34R42R26L17R24R30R41L11L9R50L16L48R5L7R44L11L6R11R22R44R50L5R18R38L46L35L45R9L45L19R22L40R21R23R29L14R35L47R8L8R27R33R22L3L2R19L7R41L33L46L26R9L9L22L15L45L36L46L28R40R10L40R26L47L1R47L25R3R44L35R21L36L43R28L4L21R20L6R2R1R42R10L11R33R44L1R37R48L26L46R11R45L22R18L34R43L26L41L27L14R9R14L2R41R19R47L1R42L11R17R21L12L33L10L30R43R46R47L9R17L14L15L6L43R44L22L40L50L2L19R30L38R48L1R23L32R33R31L45R5L47R1R19R15L40L17R7L10R19R40L29R26R10R21R47L33L22R23L10L1R29R39R14L35L21R18R25L20L46R15L42R6R33R26R10R9R31R32L48R49L15R48L39L41L12L35R24L2L9L27R34L32R6R25L16R47L24R14L48R2R39L2L19L21L23R50R29L44L16R15L14L50R39L13R15L19R46R16R38L8R34R44L46L38L49R7L5R32R47R4R29L24R7R4L30R34R8R15L4R34L25L24L8L10L6R36L18R21R13R50R17L12L50R48L2R31L22L12R31L1L39R19L8R43L15L37L32L34R15R48R39R26L40R20L26L33R21L39R8L38R10L14R41L35R32R23R19R25R26R25L27R26L43R12L45L15L19L39R14L26L9R25R16R33R44R33L17L37R13R29L18L24L2L39L32R17R46L41R31R23R5R17R26R6L36L40R5L33R4R43L45L45R26R47L2L35L36L42R8R32L35L30R42R50R10L44L9L46L25L45L7R18L15R22L47R44L41R41L1R36R19L16R33R40R45R31L23R34L47R13L42L2L32L23R40L14L19R29R50R47L11R40L26L13L1L39R29R43R40R41R4L24R45L8L47L23L45R13L47L25L35L5L23R5L7L5R47R16L34L36L22L31L8R5R21L44R41L41L46R5L7R11R7R44R10L15L48L22L11L9R36L34R25L34R33R33L17R2L50L45L14R12R17L10L47L15R33R25R1R46R20R3L5R42R13R27L22L8L37R3R2R46L3L12R27L5L4R33L18R4R26L30R5L44L10
+2 -2
src/main.rs
··· 1 1 use crate::solutions::year_2022::{ 2 - Day10, Day11, Day12, Day2, Day3, Day4, Day5, Day6, Day7, Day8, Day9, 2 + Day10, Day11, Day12, Day2, Day22, Day3, Day4, Day5, Day6, Day7, Day8, Day9, 3 3 }; 4 4 use aoc_lib::Solver; 5 5 pub mod solutions; 6 6 7 7 fn main() { 8 - let sol = Day12::solve_part2(); 8 + let sol = Day22::solve_part2(); 9 9 println!("{:?}", sol); 10 10 }
+701
src/solutions/year_2022/day22.rs
··· 1 + use std::{fmt::Debug, iter::repeat_with}; 2 + 3 + use aoc_lib::{Input, Solver}; 4 + use nom::{ 5 + character::complete::{anychar, digit1}, 6 + combinator::{map_res, opt}, 7 + multi::many1, 8 + sequence::pair, 9 + IResult, 10 + }; 11 + 12 + pub struct Day22 {} 13 + 14 + const CUBE_FACES: [[(usize, usize); 4]; 6] = [ 15 + [(0, 50), (0, 99), (49, 50), (49, 99)], // back 16 + [(0, 100), (0, 149), (49, 100), (49, 149)], // left 17 + [(50, 50), (50, 99), (99, 50), (99, 99)], // top 18 + [(100, 50), (100, 99), (149, 50), (149, 99)], // front 19 + [(100, 0), (100, 49), (149, 0), (149, 49)], // right 20 + [(150, 0), (150, 49), (199, 0), (199, 49)], // base 21 + ]; 22 + 23 + const EDGE_WRAPS: [[[(usize, usize); 2]; 4]; 6] = [ 24 + // [ Right , Left , Top , Bottom ] 25 + [ 26 + [(0, 100), (49, 100)], 27 + [(100, 0), (149, 0)], 28 + [(150, 0), (199, 0)], 29 + [(50, 50), (50, 99)], 30 + ], // back 31 + [ 32 + [(149, 99), (100, 99)], 33 + [(0, 99), (49, 99)], 34 + [(199, 0), (199, 49)], 35 + [(50, 99), (99, 99)], 36 + ], // left 37 + [ 38 + [(49, 100), (49, 149)], 39 + [(100, 0), (100, 49)], 40 + [(49, 50), (49, 99)], 41 + [(100, 50), (100, 99)], 42 + ], // top 43 + [ 44 + [(49, 149), (49, 100)], 45 + [(100, 49), (149, 99)], 46 + [(99, 50), (99, 99)], 47 + [(100, 49), (199, 49)], 48 + ], // front 49 + [ 50 + [(100, 50), (149, 50)], 51 + [(49, 50), (0, 50)], 52 + [(50, 50), (99, 50)], 53 + [(150, 0), (150, 49)], 54 + ], // right 55 + [ 56 + [(149, 50), (149, 99)], 57 + [(0, 50), (0, 99)], 58 + [(149, 0), (149, 49)], 59 + [(0, 100), (0, 149)], 60 + ], // base 61 + ]; 62 + 63 + impl Solver for Day22 { 64 + type OutputPart1 = usize; 65 + 66 + type OutputPart2 = usize; 67 + 68 + fn day() -> u8 { 69 + 22 70 + } 71 + 72 + fn solution_part1(input: aoc_lib::Input) -> Option<Self::OutputPart1> { 73 + let (mut board, commands) = Board::setup(input); 74 + commands.iter().for_each(|c| { 75 + board.play(c, false); 76 + }); 77 + let (x, y, f) = board.current_pos; 78 + 79 + Some(1000 * (x + 1) + 4 * (y + 1) + usize::from(f)) 80 + } 81 + 82 + fn solution_part2(input: aoc_lib::Input) -> Option<Self::OutputPart2> { 83 + let (mut board, commands) = Board::setup(input); 84 + commands.iter().for_each(|c| { 85 + board.play(c, true); 86 + }); 87 + let (x, y, f) = board.current_pos; 88 + 89 + Some(1000 * (x + 1) + 4 * (y + 1) + usize::from(f)) 90 + } 91 + } 92 + 93 + #[derive(Debug)] 94 + enum CubeFaces { 95 + Top(usize, usize), 96 + Back(usize, usize), 97 + Front(usize, usize), 98 + Left(usize, usize), 99 + Right(usize, usize), 100 + Base(usize, usize), 101 + } 102 + 103 + impl From<(usize, usize)> for CubeFaces { 104 + fn from(i: (usize, usize)) -> Self { 105 + match i { 106 + (x, y) if (0..50).contains(&x) && (50..100).contains(&y) => CubeFaces::Back(x, y), 107 + (x, y) if (0..50).contains(&x) && (100..150).contains(&y) => CubeFaces::Left(x, y), 108 + (x, y) if (50..100).contains(&x) && (50..100).contains(&y) => CubeFaces::Top(x, y), 109 + (x, y) if (100..150).contains(&x) && (50..100).contains(&y) => CubeFaces::Front(x, y), 110 + (x, y) if (100..150).contains(&x) && (0..50).contains(&y) => CubeFaces::Right(x, y), 111 + (x, y) if (150..200).contains(&x) && (0..50).contains(&y) => CubeFaces::Base(x, y), 112 + _ => unreachable!(""), 113 + } 114 + } 115 + } 116 + 117 + impl CubeFaces { 118 + fn next_cell(&self, dir: Facing) -> (usize, usize) { 119 + println!("{:?}", self); 120 + let handle_back = |(&x, &y): (&usize, &usize)| { 121 + let (start_x, start_y) = (CUBE_FACES[0][0].0, CUBE_FACES[0][0].1); 122 + let (end_x, end_y) = (CUBE_FACES[0][3].0, CUBE_FACES[0][3].1); 123 + let (dx, dy) = (x - start_x, y - start_y); 124 + match dir { 125 + Facing::Right => { 126 + let ny = y + 1; 127 + if ny > end_y { 128 + (EDGE_WRAPS[0][0][0].0 + dx, EDGE_WRAPS[0][0][1].1) 129 + } else { 130 + (x, ny) 131 + } 132 + } 133 + Facing::Left => { 134 + let ny = y - 1; 135 + if ny < start_y { 136 + (EDGE_WRAPS[0][1][0].0 + dx, EDGE_WRAPS[0][1][1].1) 137 + } else { 138 + (x, ny) 139 + } 140 + } 141 + Facing::Up => { 142 + let nx_opt = 143 + x.checked_sub(1) 144 + .and_then(|x| if x < start_x { None } else { Some(x) }); 145 + if let Some(nx) = nx_opt { 146 + (nx, y) 147 + } else { 148 + (EDGE_WRAPS[0][2][0].0 + dx, EDGE_WRAPS[0][2][1].1) 149 + } 150 + } 151 + Facing::Down => { 152 + let nx = x + 1; 153 + if nx > end_x { 154 + (EDGE_WRAPS[0][2][0].0, EDGE_WRAPS[0][2][0].1 + dy) 155 + } else { 156 + (nx, y) 157 + } 158 + } 159 + } 160 + }; 161 + 162 + let handle_top = |(&x, &y): (&usize, &usize)| { 163 + let top_face = CUBE_FACES[2]; 164 + let top_edge_wrap = EDGE_WRAPS[2]; 165 + let (start_x, start_y) = (top_face[0].0, top_face[0].1); 166 + let (end_x, end_y) = (top_face[3].0, top_face[3].1); 167 + let (dx, dy) = (x - start_x, y - start_y); 168 + match dir { 169 + Facing::Right => { 170 + let ny = y + 1; 171 + if ny > end_y { 172 + (top_edge_wrap[0][0].0, top_edge_wrap[0][0].1 + dy) 173 + } else { 174 + (x, ny) 175 + } 176 + } 177 + Facing::Left => { 178 + let ny = y - 1; 179 + if ny < start_y { 180 + (top_edge_wrap[1][0].0, top_edge_wrap[1][0].1 + dy) 181 + } else { 182 + (x, ny) 183 + } 184 + } 185 + Facing::Up => { 186 + let nx_opt = 187 + x.checked_sub(1) 188 + .and_then(|x| if x < start_x { None } else { Some(x) }); 189 + if let Some(nx) = nx_opt { 190 + (nx, y) 191 + } else { 192 + (top_edge_wrap[2][0].0, top_edge_wrap[2][0].1 + dy) 193 + } 194 + } 195 + Facing::Down => { 196 + let nx = x + 1; 197 + if nx > end_x { 198 + (top_edge_wrap[2][0].0, top_edge_wrap[2][0].1 + dy) 199 + } else { 200 + (nx, y) 201 + } 202 + } 203 + } 204 + }; 205 + 206 + let handle_front = |(&x, &y): (&usize, &usize)| { 207 + let front_face = CUBE_FACES[3]; 208 + let front_edge_wrap = EDGE_WRAPS[3]; 209 + let (start_x, start_y) = (front_face[0].0, front_face[0].1); 210 + let (end_x, end_y) = (front_face[3].0, front_face[3].1); 211 + let (dx, dy) = (x - start_x, y - start_y); 212 + match dir { 213 + Facing::Right => { 214 + let ny = y + 1; 215 + if ny > end_y { 216 + (front_edge_wrap[0][0].0, front_edge_wrap[0][0].1 - dy) 217 + } else { 218 + (x, ny) 219 + } 220 + } 221 + Facing::Left => { 222 + let ny = y - 1; 223 + if ny < start_y { 224 + (front_edge_wrap[1][0].0 + dx, front_edge_wrap[1][0].1 + dy) 225 + } else { 226 + (x, ny) 227 + } 228 + } 229 + Facing::Up => { 230 + let nx_opt = 231 + x.checked_sub(1) 232 + .and_then(|x| if x < start_x { None } else { Some(x) }); 233 + if let Some(nx) = nx_opt { 234 + (nx, y) 235 + } else { 236 + (front_edge_wrap[2][0].0, front_edge_wrap[2][0].1 + dy) 237 + } 238 + } 239 + Facing::Down => { 240 + let nx = x + 1; 241 + if nx > end_x { 242 + (front_edge_wrap[2][0].0 + dx, front_edge_wrap[2][1].1) 243 + } else { 244 + (nx, y) 245 + } 246 + } 247 + } 248 + }; 249 + 250 + let handle_left = |(&x, &y): (&usize, &usize)| { 251 + let left_face = CUBE_FACES[1]; 252 + let left_edge_wrap = EDGE_WRAPS[1]; 253 + let (start_x, start_y) = (left_face[0].0, left_face[0].1); 254 + let (end_x, end_y) = (left_face[3].0, left_face[3].1); 255 + let (dx, dy) = (x - start_x, y - start_y); 256 + match dir { 257 + Facing::Right => { 258 + let ny = y + 1; 259 + if ny > end_y { 260 + (left_edge_wrap[0][0].0 - dx, left_edge_wrap[0][1].1) 261 + } else { 262 + (x, ny) 263 + } 264 + } 265 + Facing::Left => { 266 + let ny = y - 1; 267 + if ny < start_y { 268 + (left_edge_wrap[1][0].0 + dx, left_edge_wrap[1][1].1) 269 + } else { 270 + (x, ny) 271 + } 272 + } 273 + Facing::Up => { 274 + let nx_opt = 275 + x.checked_sub(1) 276 + .and_then(|x| if x < start_x { None } else { Some(x) }); 277 + if let Some(nx) = nx_opt { 278 + (nx, y) 279 + } else { 280 + (left_edge_wrap[2][0].0, left_edge_wrap[2][0].1 + dy) 281 + } 282 + } 283 + Facing::Down => { 284 + let nx = x + 1; 285 + if nx > end_x { 286 + (left_edge_wrap[2][0].0, left_edge_wrap[2][0].1 + dy) 287 + } else { 288 + (nx, y) 289 + } 290 + } 291 + } 292 + }; 293 + 294 + let handle_right = |(&x, &y): (&usize, &usize)| { 295 + let right_face = CUBE_FACES[4]; 296 + let right_edge_wrap = EDGE_WRAPS[4]; 297 + let (start_x, start_y) = (right_face[0].0, right_face[0].1); 298 + let (end_x, end_y) = (right_face[3].0, right_face[3].1); 299 + let (dx, dy) = (x - start_x, y - start_y); 300 + match dir { 301 + Facing::Right => { 302 + let ny = y + 1; 303 + if ny > end_y { 304 + (right_edge_wrap[0][0].0 + dx, right_edge_wrap[0][1].1) 305 + } else { 306 + (x, ny) 307 + } 308 + } 309 + Facing::Left => { 310 + let ny_opt = 311 + y.checked_sub(1) 312 + .and_then(|y| if y < start_y { None } else { Some(y) }); 313 + if let Some(ny) = ny_opt { 314 + (x, ny) 315 + } else { 316 + (right_edge_wrap[1][0].0 - dx, right_edge_wrap[1][1].1) 317 + } 318 + } 319 + Facing::Up => { 320 + let nx_opt = 321 + x.checked_sub(1) 322 + .and_then(|x| if x < start_x { None } else { Some(x) }); 323 + if let Some(nx) = nx_opt { 324 + (nx, y) 325 + } else { 326 + (right_edge_wrap[2][0].0 + dx, right_edge_wrap[2][1].1) 327 + } 328 + } 329 + Facing::Down => { 330 + let nx = x + 1; 331 + if nx > end_x { 332 + (right_edge_wrap[2][0].0, right_edge_wrap[2][0].1 + dy) 333 + } else { 334 + (nx, y) 335 + } 336 + } 337 + } 338 + }; 339 + 340 + let handle_base = |(&x, &y): (&usize, &usize)| { 341 + let base_face = CUBE_FACES[5]; 342 + let base_edge_wrap = EDGE_WRAPS[5]; 343 + let (start_x, start_y) = (base_face[0].0, base_face[0].1); 344 + let (end_x, end_y) = (base_face[3].0, base_face[3].1); 345 + let (dx, dy) = (x - start_x, y - start_y); 346 + match dir { 347 + Facing::Right => { 348 + let ny = y + 1; 349 + if ny > end_y { 350 + (base_edge_wrap[0][0].0, base_edge_wrap[0][0].1 + dy) 351 + } else { 352 + (x, ny) 353 + } 354 + } 355 + Facing::Left => { 356 + let ny_opt = 357 + y.checked_sub(1) 358 + .and_then(|y| if y < start_y { None } else { Some(y) }); 359 + if let Some(ny) = ny_opt { 360 + (x, ny) 361 + } else { 362 + (base_edge_wrap[1][0].0, base_edge_wrap[1][0].1 + dy) 363 + } 364 + } 365 + Facing::Up => { 366 + let nx_opt = 367 + x.checked_sub(1) 368 + .and_then(|x| if x < start_x { None } else { Some(x) }); 369 + if let Some(nx) = nx_opt { 370 + (nx, y) 371 + } else { 372 + (base_edge_wrap[2][0].0, base_edge_wrap[2][0].1 + dy) 373 + } 374 + } 375 + Facing::Down => { 376 + let nx = x + 1; 377 + if nx > end_x { 378 + (base_edge_wrap[2][0].0, base_edge_wrap[2][0].1 + dy) 379 + } else { 380 + (nx, y) 381 + } 382 + } 383 + } 384 + }; 385 + 386 + match self { 387 + CubeFaces::Top(x, y) => handle_top((x, y)), 388 + CubeFaces::Back(x, y) => handle_back((x, y)), 389 + CubeFaces::Front(x, y) => handle_front((x, y)), 390 + CubeFaces::Left(x, y) => handle_left((x, y)), 391 + CubeFaces::Right(x, y) => handle_right((x, y)), 392 + CubeFaces::Base(x, y) => handle_base((x, y)), 393 + } 394 + } 395 + } 396 + 397 + #[derive(Debug, Copy, Clone, PartialEq, Eq)] 398 + enum Cell { 399 + Blank, 400 + Open, 401 + Wall, 402 + } 403 + 404 + impl From<char> for Cell { 405 + fn from(cell: char) -> Self { 406 + match cell { 407 + '#' => Cell::Wall, 408 + '.' => Cell::Open, 409 + _ => Cell::Blank, 410 + } 411 + } 412 + } 413 + 414 + #[derive(Debug, Clone, Copy)] 415 + enum Turn { 416 + Clockwise, 417 + CounterClockwise, 418 + } 419 + 420 + #[derive(Debug)] 421 + struct Command { 422 + steps: usize, 423 + turn: Option<Turn>, 424 + } 425 + 426 + impl Command { 427 + fn parse(input: &str) -> Vec<(usize, Option<char>)> { 428 + let p: IResult<&str, Vec<(usize, Option<char>)>> = many1(pair( 429 + map_res(digit1, |d: &str| d.parse::<usize>()), 430 + opt(anychar), 431 + ))(input); 432 + p.expect("could not parse commands").1 433 + } 434 + 435 + fn setup(steps: usize, t: Option<char>) -> Self { 436 + if let Some(t) = t { 437 + if t == 'R' { 438 + Command { 439 + steps, 440 + turn: Some(Turn::Clockwise), 441 + } 442 + } else { 443 + Command { 444 + steps, 445 + turn: Some(Turn::CounterClockwise), 446 + } 447 + } 448 + } else { 449 + Command { steps, turn: None } 450 + } 451 + } 452 + } 453 + 454 + #[derive(Debug, Clone, Copy)] 455 + enum Facing { 456 + Right, 457 + Left, 458 + Up, 459 + Down, 460 + } 461 + 462 + impl Facing { 463 + fn turn(&self, t: Turn) -> Self { 464 + match self { 465 + Facing::Right => match t { 466 + Turn::Clockwise => Facing::Down, 467 + Turn::CounterClockwise => Facing::Up, 468 + }, 469 + Facing::Left => match t { 470 + Turn::Clockwise => Facing::Up, 471 + Turn::CounterClockwise => Facing::Down, 472 + }, 473 + Facing::Up => match t { 474 + Turn::Clockwise => Facing::Right, 475 + Turn::CounterClockwise => Facing::Left, 476 + }, 477 + Facing::Down => match t { 478 + Turn::Clockwise => Facing::Left, 479 + Turn::CounterClockwise => Facing::Right, 480 + }, 481 + } 482 + } 483 + } 484 + 485 + impl From<Facing> for usize { 486 + fn from(f: Facing) -> Self { 487 + match f { 488 + Facing::Right => 0, 489 + Facing::Left => 2, 490 + Facing::Up => 3, 491 + Facing::Down => 1, 492 + } 493 + } 494 + } 495 + 496 + #[derive(Debug)] 497 + struct Board { 498 + // each row will have list of cells + length of row 499 + cells: Vec<Vec<Cell>>, 500 + current_pos: (usize, usize, Facing), 501 + height: usize, 502 + width: usize, 503 + } 504 + 505 + impl Board { 506 + fn setup(input: Input) -> (Self, Vec<Command>) { 507 + let (board_input, commands_input) = input 508 + .lines 509 + .split(|p| p.is_empty()) 510 + .collect::<Vec<_>>() 511 + .split_first() 512 + .map(|(a, xs)| (*a, *xs.first().expect("commands string is empty"))) 513 + .expect("expecting a proper input"); 514 + let xs = board_input 515 + .iter() 516 + .fold((0, (vec![])), |(width, mut xs), l| { 517 + let mut cs: Vec<Cell> = l.chars().map(Cell::from).collect(); 518 + let c = cs.len(); 519 + if c < width { 520 + cs.extend(vec![Cell::Blank].repeat(width - c)); 521 + let c = cs.len(); 522 + xs.push((cs, c)); 523 + (c, xs) 524 + } else { 525 + xs.push((cs, c)); 526 + (c, xs) 527 + } 528 + }); 529 + let cells = 530 + xs.1.into_iter() 531 + .map(|(mut cs, sz)| { 532 + let width = xs.0; 533 + if sz < width { 534 + cs.extend(vec![Cell::Blank].repeat(width - sz)); 535 + cs 536 + } else { 537 + cs 538 + } 539 + }) 540 + .collect::<Vec<_>>(); 541 + let height = cells.len(); 542 + let width = cells.first().map(|f| f.len()).expect("proper width"); 543 + let current_pos = Board::tiles_in_a_dir(&cells, (0, 0), Facing::Right, height, width) 544 + .find(|o| match o { 545 + Some((_, _, c)) => match c { 546 + Cell::Blank => false, 547 + Cell::Open => true, 548 + Cell::Wall => false, 549 + }, 550 + None => false, 551 + }) 552 + .map(|o| (o.unwrap().0, o.unwrap().1, Facing::Right)) 553 + .unwrap(); 554 + 555 + let commands = commands_input 556 + .first() 557 + .map(|cs| { 558 + Command::parse(cs) 559 + .iter() 560 + .map(|(sz, c)| Command::setup(*sz, *c)) 561 + .collect::<Vec<_>>() 562 + }) 563 + .expect("could not parse commands"); 564 + 565 + ( 566 + Board { 567 + cells, 568 + current_pos, 569 + height, 570 + width, 571 + }, 572 + commands, 573 + ) 574 + } 575 + 576 + fn tiles_in_a_cube<'a>( 577 + cells: &'a [Vec<Cell>], 578 + start: (usize, usize), 579 + dir: Facing, 580 + // height: usize, 581 + // width: usize, 582 + ) -> Box<dyn Iterator<Item = Option<(usize, usize, Cell)>> + 'a> { 583 + println!("{:?} {:?}", start, dir); 584 + let mut current = start; 585 + Box::new(repeat_with(move || { 586 + current = CubeFaces::from(current).next_cell(dir); 587 + println!("{:?}", current); 588 + let row = cells.get(current.0); 589 + row.and_then(|r| r.get(current.1).map(|c| (current.0, current.1, *c))) 590 + })) 591 + } 592 + 593 + fn tiles_in_a_dir<'a>( 594 + cells: &'a [Vec<Cell>], 595 + start: (usize, usize), 596 + dir: Facing, 597 + height: usize, 598 + width: usize, 599 + ) -> Box<dyn Iterator<Item = Option<(usize, usize, Cell)>> + 'a> { 600 + match dir { 601 + Facing::Right => { 602 + let mut index = start.1; 603 + Box::new(repeat_with(move || { 604 + let row = cells.get(start.0); 605 + let ret = row.and_then(|r| r.get(index).map(|c| (start.0, index, *c))); 606 + index = (index + 1) % width; 607 + ret 608 + })) 609 + } 610 + Facing::Left => { 611 + let mut index = start.1; 612 + Box::new(repeat_with(move || { 613 + let row = cells.get(start.0); 614 + let ret = row.and_then(|r| r.get(index).map(|c| (start.0, index, *c))); 615 + let i = index.checked_sub(1); 616 + if let Some(i) = i { 617 + index = i; 618 + } else { 619 + index = width; 620 + } 621 + ret 622 + })) 623 + } 624 + Facing::Up => { 625 + let mut index = start.0; 626 + Box::new(repeat_with(move || { 627 + let row = cells.get(index); 628 + let ret = row.and_then(|r| r.get(start.1).map(|c| (index, start.1, *c))); 629 + let i = index.checked_sub(1); 630 + if let Some(i) = i { 631 + index = i; 632 + } else { 633 + index = height; 634 + } 635 + ret 636 + })) 637 + } 638 + Facing::Down => { 639 + let mut index = start.0; 640 + Box::new(repeat_with(move || { 641 + let row = cells.get(index); 642 + let ret = row.and_then(|r| r.get(start.1).map(|c| (index, start.1, *c))); 643 + index = (index + 1) % height; 644 + ret 645 + })) 646 + } 647 + } 648 + } 649 + 650 + fn play(&mut self, command: &Command, follow_cube: bool) -> &mut Self { 651 + println!("{:?}", command); 652 + let Command { steps, turn } = command; 653 + let (x, y, facing) = &self.current_pos; 654 + let handle_moves = |ms: Box<dyn Iterator<Item = Option<(usize, usize, Cell)>>>| { 655 + ms.filter(|o| o.map(|(_, _, c)| c != Cell::Blank).unwrap_or(false)) 656 + .enumerate() 657 + .take_while(|(step, mv)| { 658 + println!("{:?}", mv); 659 + mv.map(|(_, _, cell)| { 660 + if cell == Cell::Wall { 661 + false 662 + } else { 663 + step <= steps 664 + } 665 + }) 666 + .unwrap_or(false) 667 + }) 668 + .map(|x| x.1.expect("could not play")) 669 + .last() 670 + }; 671 + if let Some(turn) = turn { 672 + let moves = { 673 + if !follow_cube { 674 + Board::tiles_in_a_dir(&self.cells, (*x, *y), *facing, self.height, self.width) 675 + } else { 676 + Board::tiles_in_a_cube(&self.cells, (*x, *y), *facing) 677 + } 678 + }; 679 + let facing = facing.turn(*turn); 680 + if let Some((x, y, _)) = handle_moves(moves) { 681 + self.current_pos = (x, y, facing) 682 + } else { 683 + self.current_pos = (*x, *y, facing) 684 + } 685 + } else { 686 + let moves = { 687 + if !follow_cube { 688 + Board::tiles_in_a_dir(&self.cells, (*x, *y), *facing, self.height, self.width) 689 + } else { 690 + Board::tiles_in_a_cube(&self.cells, (*x, *y), *facing) 691 + } 692 + }; 693 + if let Some((x, y, _)) = handle_moves(moves) { 694 + self.current_pos = (x, y, *facing) 695 + } else { 696 + self.current_pos = (*x, *y, *facing) 697 + } 698 + } 699 + self 700 + } 701 + }
+2
src/solutions/year_2022/mod.rs
··· 10 10 mod day10; 11 11 mod day11; 12 12 mod day12; 13 + mod day22; 13 14 pub(crate) use day1::*; 14 15 pub(crate) use day2::*; 15 16 pub(crate) use day3::*; ··· 22 23 pub(crate) use day10::*; 23 24 pub(crate) use day11::*; 24 25 pub(crate) use day12::*; 26 + pub(crate) use day22::*;