Removed more clones and copies
This commit is contained in:
parent
cfd13f68bf
commit
6a39bf7002
|
@ -1,56 +1,29 @@
|
||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
struct Path {
|
struct Path {
|
||||||
steps: usize,
|
steps: usize,
|
||||||
path: [Option<DirButton>; 6],
|
path: [Option<DirButton>; 6],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Path {
|
impl Path {
|
||||||
fn new() -> Self {
|
const fn from(buttons: &[DirButton]) -> Option<Self> {
|
||||||
Self {
|
let steps = buttons.len();
|
||||||
steps: 0,
|
let mut path = [None, None, None, None, None, None];
|
||||||
path: [None, None, None, None, None, None],
|
let mut i = 0;
|
||||||
|
while i < steps {
|
||||||
|
path[i] = Some(match buttons[i] {
|
||||||
|
DirButton::Up => DirButton::Up,
|
||||||
|
DirButton::Down => DirButton::Down,
|
||||||
|
DirButton::Left => DirButton::Left,
|
||||||
|
DirButton::Right => DirButton::Right,
|
||||||
|
DirButton::A => DirButton::A,
|
||||||
|
});
|
||||||
|
i += 1;
|
||||||
}
|
}
|
||||||
}
|
let steps = steps + 1;
|
||||||
|
path[i] = Some(DirButton::A);
|
||||||
fn push(&mut self, button: DirButton) {
|
Some(Self { steps, path })
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,17 +36,93 @@ enum DirButton {
|
||||||
A,
|
A,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DirButton {
|
const DIRBUTTON_PATHS: [[[Option<Path>; 2]; 5]; 5] = [
|
||||||
fn pos(&self) -> (usize, usize) {
|
// Up
|
||||||
match self {
|
[
|
||||||
DirButton::Up => (1, 0),
|
// Up
|
||||||
DirButton::Down => (1, 1),
|
[Path::from(&[]), None],
|
||||||
DirButton::Left => (0, 1),
|
// Down
|
||||||
DirButton::Right => (2, 1),
|
[Path::from(&[DirButton::Down]), None],
|
||||||
DirButton::A => (2, 0),
|
// 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 {
|
||||||
fn index(&self) -> usize {
|
fn index(&self) -> usize {
|
||||||
match self {
|
match self {
|
||||||
DirButton::Up => 0,
|
DirButton::Up => 0,
|
||||||
|
@ -84,40 +133,8 @@ impl DirButton {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to(&self, other: &DirButton) -> [Option<Path>; 2] {
|
fn to(&self, other: &DirButton) -> &[Option<Path>; 2] {
|
||||||
let start = self.pos();
|
&DIRBUTTON_PATHS[self.index()][other.index()]
|
||||||
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)]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,40 +170,79 @@ impl NumButton {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn to(&self, other: &NumButton) -> [Option<Path>; 2] {
|
fn to(&self, other: &NumButton) -> [Option<Path>; 2] {
|
||||||
let start = self.pos();
|
let start = self.pos();
|
||||||
let end = other.pos();
|
let end = other.pos();
|
||||||
|
|
||||||
let mut x = Path::new();
|
let bx = if start.0 < end.0 {
|
||||||
let mut y = Path::new();
|
DirButton::Right
|
||||||
|
} else {
|
||||||
|
DirButton::Left
|
||||||
|
};
|
||||||
|
let dx = start.0.abs_diff(end.0);
|
||||||
|
|
||||||
if start.0 < end.0 {
|
let by = if start.1 < end.1 {
|
||||||
(start.0..end.0).for_each(|_| x.push(DirButton::Right));
|
DirButton::Down
|
||||||
} else {
|
} else {
|
||||||
(end.0..start.0).for_each(|_| x.push(DirButton::Left));
|
DirButton::Up
|
||||||
};
|
|
||||||
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 {
|
if start.0 == 0 && end.1 == 3 {
|
||||||
x.append(&y);
|
let mut x = [None, None, None, None, None, None];
|
||||||
x.push(DirButton::A);
|
(0..dx).for_each(|i| {
|
||||||
[Some(x), None]
|
x[i] = Some(bx.clone());
|
||||||
|
});
|
||||||
|
(0..dy).for_each(|i| {
|
||||||
|
x[i + dx] = Some(by.clone());
|
||||||
|
});
|
||||||
|
x[dx + dy] = Some(DirButton::A);
|
||||||
|
[
|
||||||
|
Some(Path {
|
||||||
|
steps: dx + dy + 1,
|
||||||
|
path: x,
|
||||||
|
}),
|
||||||
|
None,
|
||||||
|
]
|
||||||
} else if end.0 == 0 && start.1 == 3 {
|
} else if end.0 == 0 && start.1 == 3 {
|
||||||
y.append(&x);
|
let mut y = [None, None, None, None, None, None];
|
||||||
y.push(DirButton::A);
|
(0..dx).for_each(|i| {
|
||||||
[Some(y), None]
|
y[i + dy] = Some(bx.clone());
|
||||||
|
});
|
||||||
|
(0..dy).for_each(|i| {
|
||||||
|
y[i] = Some(by.clone());
|
||||||
|
});
|
||||||
|
y[dx + dy] = Some(DirButton::A);
|
||||||
|
[
|
||||||
|
Some(Path {
|
||||||
|
steps: dx + dy + 1,
|
||||||
|
path: y,
|
||||||
|
}),
|
||||||
|
None,
|
||||||
|
]
|
||||||
} else {
|
} else {
|
||||||
let x_copy = x;
|
let mut x = [None, None, None, None, None, None];
|
||||||
x.append(&y);
|
let mut y = [None, None, None, None, None, None];
|
||||||
x.push(DirButton::A);
|
(0..dx).for_each(|i| {
|
||||||
y.append(&x_copy);
|
x[i] = Some(bx.clone());
|
||||||
y.push(DirButton::A);
|
y[i + dy] = Some(bx.clone());
|
||||||
[Some(x), Some(y)]
|
});
|
||||||
|
(0..dy).for_each(|i| {
|
||||||
|
x[i + dx] = Some(by.clone());
|
||||||
|
y[i] = Some(by.clone());
|
||||||
|
});
|
||||||
|
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,
|
||||||
|
}),
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -238,47 +294,22 @@ fn precompute_steps(steps: &mut [DistanceMatrix]) {
|
||||||
DirButton::A,
|
DirButton::A,
|
||||||
];
|
];
|
||||||
|
|
||||||
buttons.iter().enumerate().for_each(|(i, start)| {
|
buttons.iter().for_each(|start| {
|
||||||
buttons[i + 1..].iter().for_each(|end| {
|
buttons.iter().for_each(|end| {
|
||||||
let (forward, reversed) = start
|
steps[0][start.index()][end.index()] = start
|
||||||
.to(end)
|
.to(end)
|
||||||
.into_iter()
|
.iter()
|
||||||
.flatten()
|
.flatten()
|
||||||
.map(|mut path| {
|
.map(|path| {
|
||||||
path.steps -= 1;
|
path.path
|
||||||
let mut reversed = path.reversed();
|
.iter()
|
||||||
let mut forward = path;
|
.flatten()
|
||||||
reversed.push(DirButton::A);
|
.fold((0, &DirButton::A), |acc, next| {
|
||||||
forward.push(DirButton::A);
|
(acc.0 + steps[1][acc.1.index()][next.index()], next)
|
||||||
|
})
|
||||||
(
|
.0
|
||||||
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| {
|
.fold(usize::MAX, |acc, next| acc.min(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;
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue