-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_21.rs
156 lines (132 loc) Β· 3.46 KB
/
day_21.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::collections::HashMap;
use std::iter::once;
use std::iter::repeat;
use common::{solution, Answer};
use nd_vec::{vector, Vec2};
solution!("Keypad Conundrum", 21);
const NUMPAD: [Vec2<usize>; 11] = [
vector!(1, 3),
vector!(0, 2),
vector!(1, 2),
vector!(2, 2),
vector!(0, 1),
vector!(1, 1),
vector!(2, 1),
vector!(0, 0),
vector!(1, 0),
vector!(2, 0),
vector!(2, 3),
];
const DIRPAD: [Vec2<usize>; 5] = [
vector!(1, 0),
vector!(1, 1),
vector!(0, 1),
vector!(2, 1),
vector!(2, 0),
];
fn part_a(input: &str) -> Answer {
solve(input, 2).into()
}
fn part_b(input: &str) -> Answer {
solve(input, 25).into()
}
fn solve(input: &str, dirpad_count: u8) -> u64 {
let mut sum = 0;
for case in input.lines() {
let buttons = case
.chars()
.map(|x| x.to_digit(10).unwrap_or(10) as u8)
.collect::<Vec<_>>();
let mut out = 0;
let mut pos = 10;
for &next in buttons.iter() {
let paths = get_paths(pos as usize, next as usize, &NUMPAD);
out += paths
.into_iter()
.map(|p| solve_dirpad(&mut HashMap::new(), p, dirpad_count))
.min()
.unwrap_or(0);
pos = next;
}
let number = case[..case.len() - 1].parse().unwrap_or(1);
sum += out * number;
}
sum
}
fn solve_dirpad(
memo: &mut HashMap<(Vec<Button>, u8), u64>,
sequence: Vec<Button>,
depth: u8,
) -> u64 {
if depth == 0 {
return sequence.len() as u64;
}
if let Some(&cache) = memo.get(&(sequence.clone(), depth)) {
return cache;
}
let mut out = 0;
let mut pos = Button::Activate;
for &next in sequence.iter() {
let paths = get_paths(pos as usize, next as usize, &DIRPAD);
out += paths
.into_iter()
.map(|p| solve_dirpad(memo, p, depth - 1))
.min()
.unwrap_or(0);
pos = next;
}
memo.insert((sequence, depth), out);
out
}
fn get_paths(start: usize, end: usize, keys: &[Vec2<usize>]) -> Vec<Vec<Button>> {
let (start, end) = (keys[start], keys[end]);
if start == end {
return vec![vec![Button::Activate]];
}
let dy = start.y().abs_diff(end.y());
let dx = start.x().abs_diff(end.x());
let py = repeat([Button::Down, Button::Up][(end.y() < start.y()) as usize]).take(dy);
let px = repeat([Button::Right, Button::Left][(end.x() < start.x()) as usize]).take(dx);
fn out(iter: impl Iterator<Item = Button>) -> Vec<Button> {
iter.chain(once(Button::Activate)).collect()
}
if dy == 0 {
vec![out(px)]
} else if dx == 0 {
vec![out(py)]
} else if !keys.contains(&vector!(start.x(), end.y())) {
vec![out(px.chain(py))]
} else if !keys.contains(&vector!(end.x(), start.y())) {
vec![out(py.chain(px))]
} else {
vec![out(px.clone().chain(py.clone())), out(py.chain(px))]
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[repr(u8)]
enum Button {
Up = 0,
Down = 1,
Left = 2,
Right = 3,
Activate = 4,
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
029A
980A
179A
456A
379A
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 126384.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 154115708116294_u64.into());
}
}