- commit
- 0059a0cdb37790a7ada1ca72fa8c81e314f32966
- parent
- 4ee99ef41cc784c0a73d2ac997e5871e2b6dd84d
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2022-12-22 13:42
wip
Diffstat
M | 2022/22/solution.rs | 194 | +++++++++++++++++-------------------------------------------- |
1 files changed, 52 insertions, 142 deletions
diff --git a/2022/22/solution.rs b/2022/22/solution.rs
@@ -108,140 +108,50 @@ fn part1(map: &Vec<Vec<Tile>>, path: &Vec<(usize, bool)>) -> usize { 108 108 return 1000 * (y + 1) + 4 * (x + 1) + dir; 109 109 } 110 110111 -1 fn wrap2(x: usize, y: usize, dir: usize) -> (usize, usize, usize) {112 -1 // hardcoded cube topology-1 111 fn wrap2(map: &Vec<Vec<Tile>>, x0: usize, y0: usize, dir: usize) -> (usize, usize, usize) { -1 112 println!("{},{}:{}", x0, y0, dir); -1 113 return wrap(map, x0, y0, dir).unwrap(); -1 114 } -1 115 -1 116 fn wrap(map: &Vec<Vec<Tile>>, x0: usize, y0: usize, dir: usize) -> Option<(usize, usize, usize)> { 113 117 let k = 50; 114 118115 -1 let dx = x % k;116 -1 let dy = y % k;-1 119 for yk in 0..4 { -1 120 let y = yk * k; -1 121 let dy = yk as i8 - (y0 / k) as i8; -1 122 -1 123 for xk in 0..4 { -1 124 let x = xk * k; -1 125 let dx = xk as i8 - (x0 / k) as i8; -1 126 -1 127 if y < map.len() && x < map[y].len() && map[y][x] != Tile::Out { -1 128 let dt = if dir == 0 && dx == 1 { -1 129 Some((dy + 4) as usize) -1 130 } else if dir == 1 && dy == 1 { -1 131 Some((dx + 4) as usize) -1 132 } else if dir == 2 && dx == -1 { -1 133 Some((dy + 4) as usize) -1 134 } else if dir == 3 && dy == -1 { -1 135 Some((dx + 4) as usize) -1 136 } else { -1 137 None -1 138 }; -1 139 match dt { -1 140 Some(a) => { -1 141 let dir2 = (dir + a) % 4; -1 142 return Some(match dir2 { -1 143 0 => (x -1 144 }); -1 145 return Some((x, y, (dir + a) % 4)); -1 146 }, -1 147 _ => {}, -1 148 } -1 149 } -1 150 } -1 151 } -1 152 println!(""); 117 153118 -1 return match (x / k, y / k, dir) {119 -1 // 1 -> 2120 -1 (1, 0, 0) => (121 -1 x + 1, y, dir122 -1 ),123 -1 // 1 -> 3124 -1 (1, 0, 1) => (125 -1 x, y + 1, dir126 -1 ),127 -1 // 1 -> 4128 -1 (1, 0, 2) => (129 -1 0 * k,130 -1 2 * k + (k - 1) - dy,131 -1 0,132 -1 ),133 -1 // 1 -> 6134 -1 (1, 0, 3) => (135 -1 0 * k,136 -1 3 * k + dx,137 -1 0,138 -1 ),139 -1 // 2 -> 5140 -1 (2, 0, 0) => (141 -1 1 * k + (k - 1),142 -1 2 * k + (k - 1) - dy,143 -1 2,144 -1 ),145 -1 // 2 -> 3146 -1 (2, 0, 1) => (147 -1 1 * k + (k - 1),148 -1 1 * k + dx,149 -1 2,150 -1 ),151 -1 // 2 -> 1152 -1 (2, 0, 2) => (153 -1 x - 1, y, dir154 -1 ),155 -1 // 2 -> 6156 -1 (2, 0, 3) => (157 -1 0 * k + dx,158 -1 3 * k + (k - 1),159 -1 3,160 -1 ),161 -1 // 3 -> 2162 -1 (1, 1, 0) => (163 -1 2 * k + dy,164 -1 0 * k + (k - 1),165 -1 3,166 -1 ),167 -1 // 3 -> 5168 -1 (1, 1, 1) => (169 -1 x, y + 1, dir170 -1 ),171 -1 // 3 -> 4172 -1 (1, 1, 2) => (173 -1 0 * k + dy,174 -1 2 * k,175 -1 1,176 -1 ),177 -1 // 3 -> 1178 -1 (1, 1, 3) => (179 -1 x, y - 1, dir,180 -1 ),181 -1 // 4 -> 5182 -1 (0, 2, 0) => (183 -1 x + 1, y, dir184 -1 ),185 -1 // 4 -> 6186 -1 (0, 2, 1) => (187 -1 x, y + 1, dir188 -1 ),189 -1 // 4 -> 1190 -1 (0, 2, 2) => (191 -1 1 * k,192 -1 0 * k + (k - 1) - dy,193 -1 0,194 -1 ),195 -1 // 4 -> 3196 -1 (0, 2, 3) => (197 -1 1 * k,198 -1 1 * k + dx,199 -1 0,200 -1 ),201 -1 // 5 -> 2202 -1 (1, 2, 0) => (203 -1 2 * k + (k - 1),204 -1 0 * k + (k - 1) - dy,205 -1 2,206 -1 ),207 -1 // 5 -> 6208 -1 (1, 2, 1) => (209 -1 0 * k + (k - 1),210 -1 3 * k + dx,211 -1 2,212 -1 ),213 -1 // 5 -> 4214 -1 (1, 2, 2) => (215 -1 x - 1, y, dir216 -1 ),217 -1 // 5 -> 3218 -1 (1, 2, 3) => (219 -1 x, y - 1, dir220 -1 ),221 -1 // 6 -> 5222 -1 (0, 3, 0) => (223 -1 1 * k + dy,224 -1 2 * k + (k - 1),225 -1 3,226 -1 ),227 -1 // 6 -> 2228 -1 (0, 3, 1) => (229 -1 2 * k + dx,230 -1 0 * k,231 -1 1,232 -1 ),233 -1 // 6 -> 1234 -1 (0, 3, 2) => (235 -1 1 * k + dy,236 -1 0 * k,237 -1 1,238 -1 ),239 -1 // 6 -> 4240 -1 (0, 3, 3) => (241 -1 x, y - 1, dir242 -1 ),243 -1 _ => unreachable!(),244 -1 };-1 154 return None; 245 155 } 246 156 247 157 fn part2(map: &Vec<Vec<Tile>>, path: &Vec<(usize, bool)>) -> usize { @@ -256,10 +166,10 @@ fn part2(map: &Vec<Vec<Tile>>, path: &Vec<(usize, bool)>) -> usize { 256 166 for (steps, turn) in path.iter() { 257 167 for _ in 0..*steps { 258 168 let (x2, y2, dir2) = match dir {259 -1 0 => if (x + 1) % 50 == 0 { wrap2(x, y, dir) } else { (x + 1, y, dir) },260 -1 1 => if (y + 1) % 50 == 0 { wrap2(x, y, dir) } else { (x, y + 1, dir) },261 -1 2 => if x % 50 == 0 { wrap2(x, y, dir) } else { (x - 1, y, dir) },262 -1 3 => if y % 50 == 0 { wrap2(x, y, dir) } else { (x, y - 1, dir) },-1 169 0 => if (x + 1) % 50 == 0 { wrap2(map, x, y, dir) } else { (x + 1, y, dir) }, -1 170 1 => if (y + 1) % 50 == 0 { wrap2(map, x, y, dir) } else { (x, y + 1, dir) }, -1 171 2 => if x % 50 == 0 { wrap2(map, x, y, dir) } else { (x - 1, y, dir) }, -1 172 3 => if y % 50 == 0 { wrap2(map, x, y, dir) } else { (x, y - 1, dir) }, 263 173 _ => unreachable!(), 264 174 }; 265 175 @@ -286,9 +196,9 @@ fn part2(map: &Vec<Vec<Tile>>, path: &Vec<(usize, bool)>) -> usize { 286 196 return 1000 * (y + 1) + 4 * (x + 1) + dir; 287 197 } 288 198289 -1 fn test_wrap(x: usize, y: usize, dir: usize) {290 -1 let (x2, y2, dir2) = wrap2(x, y, dir);291 -1 let (x3, y3, dir3) = wrap2(x2, y2, (dir2 + 2) % 4);-1 199 fn test_wrap(map: &Vec<Vec<Tile>>, x: usize, y: usize, dir: usize) { -1 200 let (x2, y2, dir2) = wrap2(map, x, y, dir); -1 201 let (x3, y3, dir3) = wrap2(map, x2, y2, (dir2 + 2) % 4); 292 202 let dir4 = (dir3 + 2) % 4; 293 203 assert_eq!((x, y, dir), (x3, y3, dir4)); 294 204 } @@ -300,16 +210,16 @@ fn main() { 300 210 for (x, tile) in row.iter().enumerate() { 301 211 if *tile != Tile::Out { 302 212 if x % 50 == 0 {303 -1 test_wrap(x, y, 2);-1 213 test_wrap(&map, x, y, 2); 304 214 } 305 215 if (x + 1) % 50 == 0 {306 -1 test_wrap(x, y, 0);-1 216 test_wrap(&map, x, y, 0); 307 217 } 308 218 if y % 50 == 0 {309 -1 test_wrap(x, y, 3);-1 219 test_wrap(&map, x, y, 3); 310 220 } 311 221 if (y + 1) % 50 == 0 {312 -1 test_wrap(x, y, 1);-1 222 test_wrap(&map, x, y, 1); 313 223 } 314 224 } 315 225 }