More fast

This commit is contained in:
Jan-Bulthuis 2024-12-22 01:50:48 +01:00
parent 6a39bf7002
commit 40b79950bd
1 changed files with 15 additions and 17 deletions

View File

@ -138,7 +138,7 @@ impl DirButton {
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
enum NumButton { enum NumButton {
One, One,
Two, Two,
@ -191,10 +191,10 @@ impl NumButton {
if start.0 == 0 && end.1 == 3 { if start.0 == 0 && end.1 == 3 {
let mut x = [None, None, None, None, None, None]; let mut x = [None, None, None, None, None, None];
(0..dx).for_each(|i| { (0..dx).for_each(|i| {
x[i] = Some(bx.clone()); x[i] = Some(bx);
}); });
(0..dy).for_each(|i| { (0..dy).for_each(|i| {
x[i + dx] = Some(by.clone()); x[i + dx] = Some(by);
}); });
x[dx + dy] = Some(DirButton::A); x[dx + dy] = Some(DirButton::A);
[ [
@ -207,10 +207,10 @@ impl NumButton {
} else if end.0 == 0 && start.1 == 3 { } else if end.0 == 0 && start.1 == 3 {
let mut y = [None, None, None, None, None, None]; let mut y = [None, None, None, None, None, None];
(0..dx).for_each(|i| { (0..dx).for_each(|i| {
y[i + dy] = Some(bx.clone()); y[i + dy] = Some(bx);
}); });
(0..dy).for_each(|i| { (0..dy).for_each(|i| {
y[i] = Some(by.clone()); y[i] = Some(by);
}); });
y[dx + dy] = Some(DirButton::A); y[dx + dy] = Some(DirButton::A);
[ [
@ -224,12 +224,12 @@ impl NumButton {
let mut x = [None, None, None, None, None, None]; let mut x = [None, None, None, None, None, None];
let mut y = [None, None, None, None, None, None]; let mut y = [None, None, None, None, None, None];
(0..dx).for_each(|i| { (0..dx).for_each(|i| {
x[i] = Some(bx.clone()); x[i] = Some(bx);
y[i + dy] = Some(bx.clone()); y[i + dy] = Some(bx);
}); });
(0..dy).for_each(|i| { (0..dy).for_each(|i| {
x[i + dx] = Some(by.clone()); x[i + dx] = Some(by);
y[i] = Some(by.clone()); y[i] = Some(by);
}); });
x[dx + dy] = Some(DirButton::A); x[dx + dy] = Some(DirButton::A);
y[dx + dy] = Some(DirButton::A); y[dx + dy] = Some(DirButton::A);
@ -316,9 +316,9 @@ fn precompute_steps(steps: &mut [DistanceMatrix]) {
fn shortest_dir_path(path: Path, steps: &mut [DistanceMatrix]) -> usize { fn shortest_dir_path(path: Path, steps: &mut [DistanceMatrix]) -> usize {
path.path path.path
.into_iter() .iter()
.flatten() .flatten()
.fold((0, DirButton::A), |acc, next| { .fold((0, &DirButton::A), |acc, next| {
let sum = acc.0; let sum = acc.0;
let pos = acc.1; let pos = acc.1;
@ -329,14 +329,14 @@ fn shortest_dir_path(path: Path, steps: &mut [DistanceMatrix]) -> usize {
.0 .0
} }
fn shortest_path(path: Vec<NumButton>, steps: &mut [DistanceMatrix]) -> usize { fn shortest_path(path: &[NumButton], steps: &mut [DistanceMatrix]) -> usize {
path.into_iter() path.iter()
.fold((0, NumButton::A), |acc, end| { .fold((0, &NumButton::A), |acc, end| {
let sum = acc.0; let sum = acc.0;
let start = acc.1; let start = acc.1;
let extension = start let extension = start
.to(&end) .to(end)
.into_iter() .into_iter()
.flatten() .flatten()
.map(|path| shortest_dir_path(path, steps)) .map(|path| shortest_dir_path(path, steps))
@ -355,7 +355,6 @@ fn part1(input: &Input) -> usize {
input input
.iter() .iter()
.cloned()
.map(|(num, path)| num * shortest_path(path, &mut steps)) .map(|(num, path)| num * shortest_path(path, &mut steps))
.sum() .sum()
} }
@ -368,7 +367,6 @@ fn part2(input: &Input) -> usize {
input input
.iter() .iter()
.cloned()
.map(|(num, path)| num * shortest_path(path, &mut steps)) .map(|(num, path)| num * shortest_path(path, &mut steps))
.sum() .sum()
} }