adventofcode

git clone https://git.ce9e.org/adventofcode.git

commit
4ee99ef41cc784c0a73d2ac997e5871e2b6dd84d
parent
9d096be059fc0df2a322a48dfc7649a78ac975c1
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2022-12-22 11:01
fixed

Diffstat

M 2022/22/solution.rs 113 ++++++++++++++++++++++++++++++++++++++++++++-----------------

1 files changed, 82 insertions, 31 deletions


diff --git a/2022/22/solution.rs b/2022/22/solution.rs

@@ -110,108 +110,133 @@ fn part1(map: &Vec<Vec<Tile>>, path: &Vec<(usize, bool)>) -> usize {
  110   110 
  111   111 fn wrap2(x: usize, y: usize, dir: usize) -> (usize, usize, usize) {
  112   112     // hardcoded cube topology
   -1   113     let k = 50;
  113   114 
  114    -1     let dx = x % 50;
  115    -1     let dy = y % 50;
   -1   115     let dx = x % k;
   -1   116     let dy = y % k;
  116   117 
  117    -1     return match (x / 50, y / 50, dir) {
   -1   118     return match (x / k, y / k, dir) {
   -1   119         // 1 -> 2
  118   120         (1, 0, 0) => (
  119   121             x + 1, y, dir
  120   122         ),
   -1   123         // 1 -> 3
  121   124         (1, 0, 1) => (
  122   125             x, y + 1, dir
  123   126         ),
   -1   127         // 1 -> 4
  124   128         (1, 0, 2) => (
  125    -1             0 * 50,
  126    -1             (3 * 50 - 1) - dy,
   -1   129             0 * k,
   -1   130             2 * k + (k - 1) - dy,
  127   131             0,
  128   132         ),
   -1   133         // 1 -> 6
  129   134         (1, 0, 3) => (
  130    -1             0 * 50,
  131    -1             3 * 50 + dx,
   -1   135             0 * k,
   -1   136             3 * k + dx,
  132   137             0,
  133   138         ),
   -1   139         // 2 -> 5
  134   140         (2, 0, 0) => (
  135    -1             2 * 50 - 1,
  136    -1             (3 * 50 - 1) - dy,
   -1   141             1 * k + (k - 1),
   -1   142             2 * k + (k - 1) - dy,
  137   143             2,
  138   144         ),
   -1   145         // 2 -> 3
  139   146         (2, 0, 1) => (
  140    -1             2 * 50 - 1,
  141    -1             (2 * 50 - 1) - dx,
   -1   147             1 * k + (k - 1),
   -1   148             1 * k + dx,
  142   149             2,
  143   150         ),
   -1   151         // 2 -> 1
  144   152         (2, 0, 2) => (
  145   153             x - 1, y, dir
  146   154         ),
   -1   155         // 2 -> 6
  147   156         (2, 0, 3) => (
  148    -1             0 * 50 + dx,
  149    -1             4 * 50 - 1,
   -1   157             0 * k + dx,
   -1   158             3 * k + (k - 1),
  150   159             3,
  151   160         ),
   -1   161         // 3 -> 2
  152   162         (1, 1, 0) => (
  153    -1             2 * 50 + dy,
  154    -1             1 * 50 - 1,
   -1   163             2 * k + dy,
   -1   164             0 * k + (k - 1),
  155   165             3,
  156   166         ),
   -1   167         // 3 -> 5
  157   168         (1, 1, 1) => (
  158   169             x, y + 1, dir
  159   170         ),
   -1   171         // 3 -> 4
  160   172         (1, 1, 2) => (
  161    -1             0 * 50 + dy,
  162    -1             2 * 50,
   -1   173             0 * k + dy,
   -1   174             2 * k,
  163   175             1,
  164   176         ),
   -1   177         // 3 -> 1
  165   178         (1, 1, 3) => (
  166   179             x, y - 1, dir,
  167   180         ),
   -1   181         // 4 -> 5
  168   182         (0, 2, 0) => (
  169   183             x + 1, y, dir
  170   184         ),
   -1   185         // 4 -> 6
  171   186         (0, 2, 1) => (
  172   187             x, y + 1, dir
  173   188         ),
   -1   189         // 4 -> 1
  174   190         (0, 2, 2) => (
  175    -1             1 * 50,
  176    -1             (1 * 50 - 1) - dy,
   -1   191             1 * k,
   -1   192             0 * k + (k - 1) - dy,
  177   193             0,
  178   194         ),
   -1   195         // 4 -> 3
  179   196         (0, 2, 3) => (
  180    -1             1 * 50,
  181    -1             1 * 50 + dy,
   -1   197             1 * k,
   -1   198             1 * k + dx,
  182   199             0,
  183   200         ),
   -1   201         // 5 -> 2
  184   202         (1, 2, 0) => (
  185    -1             3 * 50 - 1,
  186    -1             (1 * 50 - 1) - dy,
   -1   203             2 * k + (k - 1),
   -1   204             0 * k + (k - 1) - dy,
  187   205             2,
  188   206         ),
   -1   207         // 5 -> 6
  189   208         (1, 2, 1) => (
  190    -1             1 * 50 - 1,
  191    -1             3 * 50 + dy,
   -1   209             0 * k + (k - 1),
   -1   210             3 * k + dx,
  192   211             2,
  193   212         ),
   -1   213         // 5 -> 4
  194   214         (1, 2, 2) => (
  195   215             x - 1, y, dir
  196   216         ),
   -1   217         // 5 -> 3
  197   218         (1, 2, 3) => (
  198   219             x, y - 1, dir
  199   220         ),
   -1   221         // 6 -> 5
  200   222         (0, 3, 0) => (
  201    -1             1 * 50 + dy,
  202    -1             3 * 50 - 1,
   -1   223             1 * k + dy,
   -1   224             2 * k + (k - 1),
  203   225             3,
  204   226         ),
   -1   227         // 6 -> 2
  205   228         (0, 3, 1) => (
  206    -1             2 * 50 + dx,
  207    -1             0 * 50,
   -1   229             2 * k + dx,
   -1   230             0 * k,
  208   231             1,
  209   232         ),
   -1   233         // 6 -> 1
  210   234         (0, 3, 2) => (
  211    -1             1 * 50 + dx,
  212    -1             0 * 50,
   -1   235             1 * k + dy,
   -1   236             0 * k,
  213   237             1,
  214   238         ),
   -1   239         // 6 -> 4
  215   240         (0, 3, 3) => (
  216   241             x, y - 1, dir
  217   242         ),
@@ -261,9 +286,35 @@ fn part2(map: &Vec<Vec<Tile>>, path: &Vec<(usize, bool)>) -> usize {
  261   286     return 1000 * (y + 1) + 4 * (x + 1) + dir;
  262   287 }
  263   288 
   -1   289 fn test_wrap(x: usize, y: usize, dir: usize) {
   -1   290     let (x2, y2, dir2) = wrap2(x, y, dir);
   -1   291     let (x3, y3, dir3) = wrap2(x2, y2, (dir2 + 2) % 4);
   -1   292     let dir4 = (dir3 + 2) % 4;
   -1   293     assert_eq!((x, y, dir), (x3, y3, dir4));
   -1   294 }
   -1   295 
  264   296 fn main() {
  265   297     let (map, path) = get_input();
  266   298 
   -1   299     for (y, row) in map.iter().enumerate() {
   -1   300         for (x, tile) in row.iter().enumerate() {
   -1   301             if *tile != Tile::Out {
   -1   302                 if x % 50 == 0 {
   -1   303                     test_wrap(x, y, 2);
   -1   304                 }
   -1   305                 if (x + 1) % 50 == 0 {
   -1   306                     test_wrap(x, y, 0);
   -1   307                 }
   -1   308                 if y % 50 == 0 {
   -1   309                     test_wrap(x, y, 3);
   -1   310                 }
   -1   311                 if (y + 1) % 50 == 0 {
   -1   312                     test_wrap(x, y, 1);
   -1   313                 }
   -1   314             }
   -1   315         }
   -1   316     }
   -1   317 
  267   318     println!("part1: {}", part1(&map, &path));
  268   319     println!("part2: {}", part2(&map, &path));
  269   320 }