adventofcode

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

commit
e6a6b5e9b9860736af6280316ab81ad095c51f95
parent
0059a0cdb37790a7ada1ca72fa8c81e314f32966
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2022-12-22 13:42
Revert "wip"

This reverts commit 0059a0cdb37790a7ada1ca72fa8c81e314f32966.

Diffstat

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

1 files changed, 142 insertions, 52 deletions


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

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