AoC/aoc_2024/src/day21.rs

293 lines
7.3 KiB
Rust
Raw Normal View History

2024-12-21 12:03:49 +00:00
use aoc_runner_derive::{aoc, aoc_generator};
2024-12-21 20:16:07 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Path<T> {
steps: usize,
2024-12-21 20:16:07 +00:00
path: [Option<T>; 6],
}
2024-12-21 20:16:07 +00:00
impl<T: Copy> Path<T> {
fn new() -> Self {
Self {
steps: 0,
path: [None, None, None, None, None, None],
}
}
2024-12-21 20:16:07 +00:00
fn push(&mut self, button: T) {
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());
}
}
}
2024-12-21 20:16:07 +00:00
impl<T> IntoIterator for Path<T> {
type Item = T;
type IntoIter = std::iter::Flatten<std::array::IntoIter<Option<T>, 6>>;
fn into_iter(self) -> Self::IntoIter {
self.path.into_iter().flatten()
}
}
2024-12-21 12:03:49 +00:00
2024-12-21 20:16:07 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DirButton {
2024-12-21 12:03:49 +00:00
Up,
Down,
Left,
Right,
A,
}
2024-12-21 20:16:07 +00:00
impl DirButton {
fn pos(&self) -> (usize, usize) {
2024-12-21 12:03:49 +00:00
match self {
2024-12-21 20:16:07 +00:00
DirButton::Up => (1, 0),
DirButton::Down => (1, 1),
DirButton::Left => (0, 1),
DirButton::Right => (2, 1),
DirButton::A => (2, 0),
2024-12-21 12:03:49 +00:00
}
}
2024-12-21 20:16:07 +00:00
fn index(&self) -> usize {
match self {
DirButton::Up => 0,
DirButton::Down => 1,
DirButton::Left => 2,
DirButton::Right => 3,
DirButton::A => 4,
}
}
fn to(&self, other: &DirButton) -> [Option<Path<DirButton>>; 2] {
2024-12-21 12:03:49 +00:00
let start = self.pos();
let end = other.pos();
let mut x = Path::new();
let mut y = Path::new();
if start.0 < end.0 {
2024-12-21 20:16:07 +00:00
(start.0..end.0).for_each(|_| x.push(DirButton::Right));
2024-12-21 12:03:49 +00:00
} else {
2024-12-21 20:16:07 +00:00
(end.0..start.0).for_each(|_| x.push(DirButton::Left));
2024-12-21 12:03:49 +00:00
};
if start.1 < end.1 {
2024-12-21 20:16:07 +00:00
(start.1..end.1).for_each(|_| y.push(DirButton::Down));
2024-12-21 12:03:49 +00:00
} else {
2024-12-21 20:16:07 +00:00
(end.1..start.1).for_each(|_| y.push(DirButton::Up));
2024-12-21 12:03:49 +00:00
};
if start.0 == 0 && end.1 == 0 {
x.append(&y);
2024-12-21 20:16:07 +00:00
x.push(DirButton::A);
[Some(x), None]
} else if end.0 == 0 && start.1 == 0 {
y.append(&x);
2024-12-21 20:16:07 +00:00
y.push(DirButton::A);
[Some(y), None]
2024-12-21 12:03:49 +00:00
} else {
2024-12-21 20:16:07 +00:00
let x_copy = x;
x.append(&y);
2024-12-21 20:16:07 +00:00
x.push(DirButton::A);
y.append(&x_copy);
y.push(DirButton::A);
[Some(x), Some(y)]
2024-12-21 12:03:49 +00:00
}
}
}
2024-12-21 20:16:07 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum NumButton {
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Zero,
2024-12-21 12:03:49 +00:00
A,
}
2024-12-21 20:16:07 +00:00
impl NumButton {
fn pos(&self) -> (usize, usize) {
2024-12-21 12:03:49 +00:00
match self {
2024-12-21 20:16:07 +00:00
NumButton::One => (0, 2),
NumButton::Two => (1, 2),
NumButton::Three => (2, 2),
NumButton::Four => (0, 1),
NumButton::Five => (1, 1),
NumButton::Six => (2, 1),
NumButton::Seven => (0, 0),
NumButton::Eight => (1, 0),
NumButton::Nine => (2, 0),
NumButton::Zero => (1, 3),
NumButton::A => (2, 3),
2024-12-21 12:03:49 +00:00
}
}
2024-12-21 20:16:07 +00:00
fn to(&self, other: &NumButton) -> [Option<Path<DirButton>>; 2] {
2024-12-21 12:03:49 +00:00
let start = self.pos();
let end = other.pos();
let mut x = Path::new();
let mut y = Path::new();
if start.0 < end.0 {
2024-12-21 20:16:07 +00:00
(start.0..end.0).for_each(|_| x.push(DirButton::Right));
2024-12-21 12:03:49 +00:00
} else {
2024-12-21 20:16:07 +00:00
(end.0..start.0).for_each(|_| x.push(DirButton::Left));
2024-12-21 12:03:49 +00:00
};
if start.1 < end.1 {
2024-12-21 20:16:07 +00:00
(start.1..end.1).for_each(|_| y.push(DirButton::Down));
2024-12-21 12:03:49 +00:00
} else {
2024-12-21 20:16:07 +00:00
(end.1..start.1).for_each(|_| y.push(DirButton::Up));
2024-12-21 12:03:49 +00:00
};
if start.0 == 0 && end.1 == 3 {
x.append(&y);
2024-12-21 20:16:07 +00:00
x.push(DirButton::A);
[Some(x), None]
} else if end.0 == 0 && start.1 == 3 {
y.append(&x);
2024-12-21 20:16:07 +00:00
y.push(DirButton::A);
[Some(y), None]
2024-12-21 12:03:49 +00:00
} else {
let x_copy = x;
x.append(&y);
2024-12-21 20:16:07 +00:00
x.push(DirButton::A);
y.append(&x_copy);
2024-12-21 20:16:07 +00:00
y.push(DirButton::A);
[Some(x), Some(y)]
2024-12-21 12:03:49 +00:00
}
}
}
2024-12-21 20:16:07 +00:00
type Input = Vec<(usize, Vec<NumButton>)>;
2024-12-21 12:03:49 +00:00
#[aoc_generator(day21)]
fn parse(input: &str) -> Input {
input
.lines()
.map(|line| {
2024-12-21 20:16:07 +00:00
(
line[0..3].parse().unwrap(),
line.chars()
.map(|c| match c {
'1' => NumButton::One,
'2' => NumButton::Two,
'3' => NumButton::Three,
'4' => NumButton::Four,
'5' => NumButton::Five,
'6' => NumButton::Six,
'7' => NumButton::Seven,
'8' => NumButton::Eight,
'9' => NumButton::Nine,
'0' => NumButton::Zero,
'A' => NumButton::A,
_ => unreachable!(),
})
.collect(),
)
2024-12-21 12:03:49 +00:00
})
.collect()
}
2024-12-21 20:16:07 +00:00
type DistanceMatrix = [[usize; 5]; 5];
2024-12-21 20:16:07 +00:00
fn calculate_steps(start: DirButton, end: DirButton, steps: &mut [DistanceMatrix]) -> usize {
if steps.is_empty() {
return 1;
}
2024-12-21 20:16:07 +00:00
if steps[0][start.index()][end.index()] == 0 {
let extension = start
.to(&end)
.into_iter()
.flatten()
.map(|path| shortest_dir_path(path, &mut steps[1..]))
.min()
.unwrap();
steps[0][start.index()][end.index()] = extension;
}
2024-12-21 20:16:07 +00:00
steps[0][start.index()][end.index()]
}
2024-12-21 20:16:07 +00:00
fn shortest_dir_path(path: Path<DirButton>, steps: &mut [DistanceMatrix]) -> usize {
path.path
.into_iter()
.flatten()
.fold((0, DirButton::A), |acc, next| {
let sum = acc.0;
let pos = acc.1;
2024-12-21 20:16:07 +00:00
let extension = calculate_steps(pos, next, steps);
2024-12-21 20:16:07 +00:00
(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))
.min()
.unwrap();
(sum + extension, end)
})
.0
}
2024-12-21 12:03:49 +00:00
#[aoc(day21, part1)]
fn part1(input: &Input) -> usize {
2024-12-21 20:16:07 +00:00
let mut steps = vec![[[0; 5]; 5]; 3 - 1];
2024-12-21 12:03:49 +00:00
input
.iter()
.cloned()
2024-12-21 20:16:07 +00:00
.map(|(num, path)| num * shortest_path(path, &mut steps))
2024-12-21 12:03:49 +00:00
.sum()
}
#[aoc(day21, part2)]
fn part2(input: &Input) -> usize {
2024-12-21 20:16:07 +00:00
let mut steps = vec![[[0; 5]; 5]; 26 - 1];
input
.iter()
.cloned()
2024-12-21 20:16:07 +00:00
.map(|(num, path)| num * shortest_path(path, &mut steps))
.sum()
}
2024-12-21 12:03:49 +00:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part1_example() {
assert_eq!(part1(&parse("179A")), 68 * 179);
2024-12-21 12:03:49 +00:00
assert_eq!(part1(&parse("379A")), 64 * 379);
assert_eq!(part1(&parse("456A")), 64 * 456);
assert_eq!(part1(&parse("980A")), 60 * 980);
assert_eq!(part1(&parse("029A")), 68 * 29);
}
}