Compare commits
	
		
			No commits in common. "cf5d423f50c78eee9bb0a88f7e97c85565df282f" and "285cefa67002e2615e54d23d080e0ae781773818" have entirely different histories.
		
	
	
		
			cf5d423f50
			...
			285cefa670
		
	
		
| @ -42,6 +42,7 @@ fn parse(input: &str) -> Input { | ||||
| #[aoc(day20, part1)] | ||||
| fn part1(input: &Input) -> usize { | ||||
|     let start = input.0; | ||||
|     let end = input.1; | ||||
|     let map = &input.2; | ||||
| 
 | ||||
|     #[cfg(not(test))] | ||||
| @ -123,6 +124,7 @@ fn part1(input: &Input) -> usize { | ||||
| #[aoc(day20, part2)] | ||||
| fn part2(input: &Input) -> usize { | ||||
|     let start = input.0; | ||||
|     let end = input.1; | ||||
|     let map = &input.2; | ||||
| 
 | ||||
|     #[cfg(not(test))] | ||||
|  | ||||
| @ -1,23 +1,56 @@ | ||||
| use aoc_runner_derive::{aoc, aoc_generator}; | ||||
| 
 | ||||
| #[derive(Debug, PartialEq, Eq)] | ||||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||||
| struct Path { | ||||
|     steps: usize, | ||||
|     path: [Option<DirButton>; 6], | ||||
| } | ||||
| 
 | ||||
| impl Path { | ||||
|     const fn from(buttons: &[DirButton]) -> Option<Self> { | ||||
|         let steps = buttons.len(); | ||||
|         let mut path = [None, None, None, None, None, None]; | ||||
|         let mut i = 0; | ||||
|         while i < steps { | ||||
|             path[i] = Some(buttons[i]); | ||||
|             i += 1; | ||||
|     fn new() -> Self { | ||||
|         Self { | ||||
|             steps: 0, | ||||
|             path: [None, None, None, None, None, None], | ||||
|         } | ||||
|         let steps = steps + 1; | ||||
|         path[i] = Some(DirButton::A); | ||||
|         Some(Self { steps, path }) | ||||
|     } | ||||
| 
 | ||||
|     fn push(&mut self, button: DirButton) { | ||||
|         self.path[self.steps] = Some(button); | ||||
|         self.steps += 1; | ||||
|     } | ||||
| 
 | ||||
|     fn append(&mut self, other: &Self) { | ||||
|         for i in 0..other.steps { | ||||
|             self.push(other.path[i].unwrap()); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     fn reversed(&self) -> Self { | ||||
|         let mut path = [None; 6]; | ||||
|         (0..self.steps).for_each(|i| { | ||||
|             path[i] = match self.path[self.steps - i - 1] { | ||||
|                 Some(DirButton::Up) => Some(DirButton::Down), | ||||
|                 Some(DirButton::Down) => Some(DirButton::Up), | ||||
|                 Some(DirButton::Left) => Some(DirButton::Right), | ||||
|                 Some(DirButton::Right) => Some(DirButton::Left), | ||||
|                 Some(DirButton::A) => Some(DirButton::A), | ||||
|                 None => None, | ||||
|             }; | ||||
|         }); | ||||
| 
 | ||||
|         Self { | ||||
|             steps: self.steps, | ||||
|             path, | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl IntoIterator for Path { | ||||
|     type Item = DirButton; | ||||
|     type IntoIter = std::iter::Flatten<std::array::IntoIter<Option<DirButton>, 6>>; | ||||
| 
 | ||||
|     fn into_iter(self) -> Self::IntoIter { | ||||
|         self.path.into_iter().flatten() | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| @ -30,94 +63,18 @@ enum DirButton { | ||||
|     A, | ||||
| } | ||||
| 
 | ||||
| const DIRBUTTON_PATHS: [[[Option<Path>; 2]; 5]; 5] = [ | ||||
|     // Up
 | ||||
|     [ | ||||
|         // Up
 | ||||
|         [Path::from(&[]), None], | ||||
|         // Down
 | ||||
|         [Path::from(&[DirButton::Down]), None], | ||||
|         // Left
 | ||||
|         [Path::from(&[DirButton::Down, DirButton::Left]), None], | ||||
|         // Right
 | ||||
|         [ | ||||
|             Path::from(&[DirButton::Down, DirButton::Right]), | ||||
|             Path::from(&[DirButton::Right, DirButton::Down]), | ||||
|         ], | ||||
|         // A
 | ||||
|         [Path::from(&[DirButton::Right]), None], | ||||
|     ], | ||||
|     // Down
 | ||||
|     [ | ||||
|         // Up
 | ||||
|         [Path::from(&[DirButton::Up]), None], | ||||
|         // Down
 | ||||
|         [Path::from(&[]), None], | ||||
|         // Left
 | ||||
|         [Path::from(&[DirButton::Left]), None], | ||||
|         // Right
 | ||||
|         [Path::from(&[DirButton::Right]), None], | ||||
|         // A
 | ||||
|         [ | ||||
|             Path::from(&[DirButton::Up, DirButton::Right]), | ||||
|             Path::from(&[DirButton::Right, DirButton::Up]), | ||||
|         ], | ||||
|     ], | ||||
|     // Left
 | ||||
|     [ | ||||
|         // Up
 | ||||
|         [Path::from(&[DirButton::Right, DirButton::Up]), None], | ||||
|         // Down
 | ||||
|         [Path::from(&[DirButton::Right]), None], | ||||
|         // Left
 | ||||
|         [Path::from(&[]), None], | ||||
|         // Right
 | ||||
|         [Path::from(&[DirButton::Right, DirButton::Right]), None], | ||||
|         // A
 | ||||
|         [ | ||||
|             Path::from(&[DirButton::Right, DirButton::Right, DirButton::Up]), | ||||
|             None, | ||||
|         ], | ||||
|     ], | ||||
|     // Right
 | ||||
|     [ | ||||
|         // Up
 | ||||
|         [ | ||||
|             Path::from(&[DirButton::Up, DirButton::Left]), | ||||
|             Path::from(&[DirButton::Left, DirButton::Up]), | ||||
|         ], | ||||
|         // Down
 | ||||
|         [Path::from(&[DirButton::Left]), None], | ||||
|         // Left
 | ||||
|         [Path::from(&[DirButton::Left, DirButton::Left]), None], | ||||
|         // Right
 | ||||
|         [Path::from(&[]), None], | ||||
|         // A
 | ||||
|         [Path::from(&[DirButton::Up]), None], | ||||
|     ], | ||||
|     // A
 | ||||
|     [ | ||||
|         // Up
 | ||||
|         [Path::from(&[DirButton::Left]), None], | ||||
|         // Down
 | ||||
|         [ | ||||
|             Path::from(&[DirButton::Down, DirButton::Left]), | ||||
|             Path::from(&[DirButton::Left, DirButton::Down]), | ||||
|         ], | ||||
|         // Left
 | ||||
|         [ | ||||
|             Path::from(&[DirButton::Down, DirButton::Left, DirButton::Left]), | ||||
|             None, | ||||
|         ], | ||||
|         // Right
 | ||||
|         [Path::from(&[DirButton::Down]), None], | ||||
|         // A
 | ||||
|         [Path::from(&[]), None], | ||||
|     ], | ||||
| ]; | ||||
| 
 | ||||
| impl DirButton { | ||||
|     const fn index(&self) -> usize { | ||||
|     fn pos(&self) -> (usize, usize) { | ||||
|         match self { | ||||
|             DirButton::Up => (1, 0), | ||||
|             DirButton::Down => (1, 1), | ||||
|             DirButton::Left => (0, 1), | ||||
|             DirButton::Right => (2, 1), | ||||
|             DirButton::A => (2, 0), | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     fn index(&self) -> usize { | ||||
|         match self { | ||||
|             DirButton::Up => 0, | ||||
|             DirButton::Down => 1, | ||||
| @ -127,12 +84,44 @@ impl DirButton { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     const fn paths_to(&self, other: &DirButton) -> &[Option<Path>; 2] { | ||||
|         &DIRBUTTON_PATHS[self.index()][other.index()] | ||||
|     fn to(&self, other: &DirButton) -> [Option<Path>; 2] { | ||||
|         let start = self.pos(); | ||||
|         let end = other.pos(); | ||||
| 
 | ||||
|         let mut x = Path::new(); | ||||
|         let mut y = Path::new(); | ||||
| 
 | ||||
|         if start.0 < end.0 { | ||||
|             (start.0..end.0).for_each(|_| x.push(DirButton::Right)); | ||||
|         } else { | ||||
|             (end.0..start.0).for_each(|_| x.push(DirButton::Left)); | ||||
|         }; | ||||
|         if start.1 < end.1 { | ||||
|             (start.1..end.1).for_each(|_| y.push(DirButton::Down)); | ||||
|         } else { | ||||
|             (end.1..start.1).for_each(|_| y.push(DirButton::Up)); | ||||
|         }; | ||||
| 
 | ||||
|         if start.0 == 0 && end.1 == 0 { | ||||
|             x.append(&y); | ||||
|             x.push(DirButton::A); | ||||
|             [Some(x), None] | ||||
|         } else if end.0 == 0 && start.1 == 0 { | ||||
|             y.append(&x); | ||||
|             y.push(DirButton::A); | ||||
|             [Some(y), None] | ||||
|         } else { | ||||
|             let x_copy = x; | ||||
|             x.append(&y); | ||||
|             x.push(DirButton::A); | ||||
|             y.append(&x_copy); | ||||
|             y.push(DirButton::A); | ||||
|             [Some(x), Some(y)] | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #[derive(Debug, PartialEq, Eq)] | ||||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||||
| enum NumButton { | ||||
|     One, | ||||
|     Two, | ||||
| @ -148,7 +137,7 @@ enum NumButton { | ||||
| } | ||||
| 
 | ||||
| impl NumButton { | ||||
|     const fn pos(&self) -> (usize, usize) { | ||||
|     fn pos(&self) -> (usize, usize) { | ||||
|         match self { | ||||
|             NumButton::One => (0, 2), | ||||
|             NumButton::Two => (1, 2), | ||||
| @ -164,84 +153,45 @@ impl NumButton { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     fn paths_to(&self, other: &NumButton) -> [Option<Path>; 2] { | ||||
|     #[inline(always)] | ||||
|     fn to(&self, other: &NumButton) -> [Option<Path>; 2] { | ||||
|         let start = self.pos(); | ||||
|         let end = other.pos(); | ||||
| 
 | ||||
|         let bx = if start.0 < end.0 { | ||||
|             DirButton::Right | ||||
|         } else { | ||||
|             DirButton::Left | ||||
|         }; | ||||
|         let dx = start.0.abs_diff(end.0); | ||||
|         let mut x = Path::new(); | ||||
|         let mut y = Path::new(); | ||||
| 
 | ||||
|         let by = if start.1 < end.1 { | ||||
|             DirButton::Down | ||||
|         if start.0 < end.0 { | ||||
|             (start.0..end.0).for_each(|_| x.push(DirButton::Right)); | ||||
|         } else { | ||||
|             DirButton::Up | ||||
|             (end.0..start.0).for_each(|_| x.push(DirButton::Left)); | ||||
|         }; | ||||
|         if start.1 < end.1 { | ||||
|             (start.1..end.1).for_each(|_| y.push(DirButton::Down)); | ||||
|         } else { | ||||
|             (end.1..start.1).for_each(|_| y.push(DirButton::Up)); | ||||
|         }; | ||||
|         let dy = start.1.abs_diff(end.1); | ||||
| 
 | ||||
|         if start.0 == 0 && end.1 == 3 { | ||||
|             let mut x = [None, None, None, None, None, None]; | ||||
|             (0..dx).for_each(|i| { | ||||
|                 x[i] = Some(bx); | ||||
|             }); | ||||
|             (0..dy).for_each(|i| { | ||||
|                 x[i + dx] = Some(by); | ||||
|             }); | ||||
|             x[dx + dy] = Some(DirButton::A); | ||||
|             [ | ||||
|                 Some(Path { | ||||
|                     steps: dx + dy + 1, | ||||
|                     path: x, | ||||
|                 }), | ||||
|                 None, | ||||
|             ] | ||||
|             x.append(&y); | ||||
|             x.push(DirButton::A); | ||||
|             [Some(x), None] | ||||
|         } else if end.0 == 0 && start.1 == 3 { | ||||
|             let mut y = [None, None, None, None, None, None]; | ||||
|             (0..dx).for_each(|i| { | ||||
|                 y[i + dy] = Some(bx); | ||||
|             }); | ||||
|             (0..dy).for_each(|i| { | ||||
|                 y[i] = Some(by); | ||||
|             }); | ||||
|             y[dx + dy] = Some(DirButton::A); | ||||
|             [ | ||||
|                 Some(Path { | ||||
|                     steps: dx + dy + 1, | ||||
|                     path: y, | ||||
|                 }), | ||||
|                 None, | ||||
|             ] | ||||
|             y.append(&x); | ||||
|             y.push(DirButton::A); | ||||
|             [Some(y), None] | ||||
|         } else { | ||||
|             let mut x = [None, None, None, None, None, None]; | ||||
|             let mut y = [None, None, None, None, None, None]; | ||||
|             (0..dx).for_each(|i| { | ||||
|                 x[i] = Some(bx); | ||||
|                 y[i + dy] = Some(bx); | ||||
|             }); | ||||
|             (0..dy).for_each(|i| { | ||||
|                 x[i + dx] = Some(by); | ||||
|                 y[i] = Some(by); | ||||
|             }); | ||||
|             x[dx + dy] = Some(DirButton::A); | ||||
|             y[dx + dy] = Some(DirButton::A); | ||||
|             [ | ||||
|                 Some(Path { | ||||
|                     steps: dx + dy + 1, | ||||
|                     path: x, | ||||
|                 }), | ||||
|                 Some(Path { | ||||
|                     steps: dx + dy + 1, | ||||
|                     path: y, | ||||
|                 }), | ||||
|             ] | ||||
|             let x_copy = x; | ||||
|             x.append(&y); | ||||
|             x.push(DirButton::A); | ||||
|             y.append(&x_copy); | ||||
|             y.push(DirButton::A); | ||||
|             [Some(x), Some(y)] | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| type Input = Vec<(DistanceSize, Vec<NumButton>)>; | ||||
| type Input = Vec<(usize, Vec<NumButton>)>; | ||||
| 
 | ||||
| #[aoc_generator(day21)] | ||||
| fn parse(input: &str) -> Input { | ||||
| @ -271,8 +221,7 @@ fn parse(input: &str) -> Input { | ||||
|         .collect() | ||||
| } | ||||
| 
 | ||||
| type DistanceSize = u64; | ||||
| type DistanceMatrix = [[DistanceSize; 5]; 5]; | ||||
| type DistanceMatrix = [[usize; 5]; 5]; | ||||
| 
 | ||||
| fn precompute_steps(steps: &mut [DistanceMatrix]) { | ||||
|     if steps.len() == 1 { | ||||
| @ -289,74 +238,106 @@ fn precompute_steps(steps: &mut [DistanceMatrix]) { | ||||
|         DirButton::A, | ||||
|     ]; | ||||
| 
 | ||||
|     buttons.iter().for_each(|start| { | ||||
|         buttons.iter().for_each(|end| { | ||||
|             steps[0][start.index()][end.index()] = start | ||||
|                 .paths_to(end) | ||||
|                 .iter() | ||||
|     buttons.iter().enumerate().for_each(|(i, start)| { | ||||
|         buttons[i + 1..].iter().for_each(|end| { | ||||
|             let (forward, reversed) = start | ||||
|                 .to(end) | ||||
|                 .into_iter() | ||||
|                 .flatten() | ||||
|                 .map(|path| { | ||||
|                     path.path | ||||
|                         .iter() | ||||
|                         .flatten() | ||||
|                         .fold((0, &DirButton::A), |acc, next| { | ||||
|                             (acc.0 + steps[1][acc.1.index()][next.index()], next) | ||||
|                         }) | ||||
|                         .0 | ||||
|                 }) | ||||
|                 .fold(DistanceSize::MAX, |acc, next| acc.min(next)); | ||||
|         }); | ||||
|     }); | ||||
| } | ||||
|                 .map(|mut path| { | ||||
|                     path.steps -= 1; | ||||
|                     let mut reversed = path.reversed(); | ||||
|                     let mut forward = path; | ||||
|                     reversed.push(DirButton::A); | ||||
|                     forward.push(DirButton::A); | ||||
| 
 | ||||
| fn shortest_dir_path(path: Path, steps: &mut [DistanceMatrix]) -> DistanceSize { | ||||
|     path.path | ||||
|         .iter() | ||||
|         .flatten() | ||||
|         .fold((0, &DirButton::A), |acc, next| { | ||||
|             (acc.0 + steps[0][acc.1.index()][next.index()], next) | ||||
|         }) | ||||
|         .0 | ||||
| } | ||||
| 
 | ||||
| fn shortest_path(path: &[NumButton], steps: &mut [DistanceMatrix]) -> DistanceSize { | ||||
|     path.iter() | ||||
|         .fold((0, &NumButton::A), |acc, end| { | ||||
|                     ( | ||||
|                 acc.0 | ||||
|                     + acc | ||||
|                         .1 | ||||
|                         .paths_to(end) | ||||
|                         forward | ||||
|                             .path | ||||
|                             .into_iter() | ||||
|                             .flatten() | ||||
|                             .fold((0, DirButton::A), |acc, next| { | ||||
|                                 let extension = steps[1][acc.1.index()][next.index()]; | ||||
| 
 | ||||
|                                 (acc.0 + extension, next) | ||||
|                             }) | ||||
|                             .0, | ||||
|                         reversed | ||||
|                             .path | ||||
|                             .into_iter() | ||||
|                             .flatten() | ||||
|                             .fold((0, DirButton::A), |acc, next| { | ||||
|                                 let extension = steps[1][acc.1.index()][next.index()]; | ||||
| 
 | ||||
|                                 (acc.0 + extension, next) | ||||
|                             }) | ||||
|                             .0, | ||||
|                     ) | ||||
|                 }) | ||||
|                 .fold((usize::MAX, usize::MAX), |acc, next| { | ||||
|                     (acc.0.min(next.0), acc.1.min(next.1)) | ||||
|                 }); | ||||
|             steps[0][start.index()][end.index()] = forward; | ||||
|             steps[0][end.index()][start.index()] = reversed; | ||||
|         }); | ||||
|     }); | ||||
| } | ||||
| 
 | ||||
| fn shortest_dir_path(path: Path, steps: &mut [DistanceMatrix]) -> usize { | ||||
|     path.path | ||||
|         .into_iter() | ||||
|         .flatten() | ||||
|         .fold((0, DirButton::A), |acc, next| { | ||||
|             let sum = acc.0; | ||||
|             let pos = acc.1; | ||||
| 
 | ||||
|             let extension = steps[0][pos.index()][next.index()]; | ||||
| 
 | ||||
|             (sum + extension, next) | ||||
|         }) | ||||
|         .0 | ||||
| } | ||||
| 
 | ||||
| fn shortest_path(path: Vec<NumButton>, steps: &mut [DistanceMatrix]) -> usize { | ||||
|     path.into_iter() | ||||
|         .fold((0, NumButton::A), |acc, end| { | ||||
|             let sum = acc.0; | ||||
|             let start = acc.1; | ||||
| 
 | ||||
|             let extension = start | ||||
|                 .to(&end) | ||||
|                 .into_iter() | ||||
|                 .flatten() | ||||
|                 .map(|path| shortest_dir_path(path, steps)) | ||||
|                         .fold(DistanceSize::MAX, |acc, next| acc.min(next)), | ||||
|                 end, | ||||
|             ) | ||||
|                 .min() | ||||
|                 .unwrap(); | ||||
|             (sum + extension, end) | ||||
|         }) | ||||
|         .0 | ||||
| } | ||||
| 
 | ||||
| #[aoc(day21, part1)] | ||||
| fn part1(input: &Input) -> DistanceSize { | ||||
| fn part1(input: &Input) -> usize { | ||||
|     let mut steps = vec![[[1; 5]; 5]; 3]; | ||||
| 
 | ||||
|     precompute_steps(&mut steps); | ||||
| 
 | ||||
|     input | ||||
|         .iter() | ||||
|         .cloned() | ||||
|         .map(|(num, path)| num * shortest_path(path, &mut steps)) | ||||
|         .sum() | ||||
| } | ||||
| 
 | ||||
| #[aoc(day21, part2)] | ||||
| fn part2(input: &Input) -> DistanceSize { | ||||
| fn part2(input: &Input) -> usize { | ||||
|     let mut steps = vec![[[1; 5]; 5]; 26]; | ||||
| 
 | ||||
|     precompute_steps(&mut steps); | ||||
| 
 | ||||
|     input | ||||
|         .iter() | ||||
|         .cloned() | ||||
|         .map(|(num, path)| num * shortest_path(path, &mut steps)) | ||||
|         .sum() | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user