-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday21.rs
More file actions
184 lines (156 loc) · 5.29 KB
/
day21.rs
File metadata and controls
184 lines (156 loc) · 5.29 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use std::{collections::HashMap, hash::Hash};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Point {
row: usize,
col: usize,
}
impl Point {
// Only consider sequences with a single turn
pub fn sequences_to(&self, other: &Point, reverse_row_axis: bool) -> Vec<String> {
let delta_row =
(other.row as isize - self.row as isize) * if reverse_row_axis { -1 } else { 1 };
let symbol = if delta_row < 0 { "v" } else { "^" };
let vertical_part = (0..delta_row.abs()).fold("".to_string(), |res, _| res + symbol);
let delta_col = other.col as isize - self.col as isize;
let symbol = if delta_col < 0 { "<" } else { ">" };
let horizontal_part = (0..delta_col.abs()).fold("".to_string(), |res, _| res + symbol);
if self.col == 0 && other.row == 0 {
return vec![horizontal_part + &vertical_part + &"A"];
}
if self.row == 0 && other.col == 0 {
return vec![vertical_part + &horizontal_part + &"A"];
}
if self.row == other.row || self.col == other.col {
return vec![horizontal_part + &vertical_part + &"A"];
}
vec![
horizontal_part.clone() + &vertical_part + &"A",
vertical_part + &horizontal_part + &"A",
]
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Keypad {
keys: HashMap<char, Point>,
reverse_row_axis: bool,
}
impl Keypad {
pub fn numpad() -> Self {
let mut keys = HashMap::new();
keys.insert('0', Point { row: 0, col: 1 });
keys.insert('A', Point { row: 0, col: 2 });
keys.insert('1', Point { row: 1, col: 0 });
keys.insert('2', Point { row: 1, col: 1 });
keys.insert('3', Point { row: 1, col: 2 });
keys.insert('4', Point { row: 2, col: 0 });
keys.insert('5', Point { row: 2, col: 1 });
keys.insert('6', Point { row: 2, col: 2 });
keys.insert('7', Point { row: 3, col: 0 });
keys.insert('8', Point { row: 3, col: 1 });
keys.insert('9', Point { row: 3, col: 2 });
Keypad {
keys,
reverse_row_axis: false,
}
}
pub fn arrowpad() -> Self {
let mut keys = HashMap::new();
keys.insert('^', Point { row: 0, col: 1 });
keys.insert('A', Point { row: 0, col: 2 });
keys.insert('<', Point { row: 1, col: 0 });
keys.insert('v', Point { row: 1, col: 1 });
keys.insert('>', Point { row: 1, col: 2 });
Keypad {
keys,
reverse_row_axis: true,
}
}
pub fn generate_sequences(&self) -> HashMap<String, Vec<String>> {
let mut sequences = HashMap::new();
for (key, point) in self.keys.iter() {
for (other_key, other_point) in self.keys.iter() {
let seqs = point.sequences_to(other_point, self.reverse_row_axis);
let pair = format!("{}{}", key, other_key);
sequences.insert(pair, seqs);
}
}
sequences
}
}
fn count_levels(
current_sequence: &String,
depth: usize,
pair_to_paths_map: &HashMap<String, Vec<String>>,
memo: &mut HashMap<(String, usize), usize>,
) -> usize {
if depth == 0 {
return current_sequence.len();
}
match memo.get(&(current_sequence.clone(), depth)) {
Some(count) => return count.clone(),
None => {}
}
let (_, count) =
current_sequence
.chars()
.fold(('A', 0_usize), |(last_char, mut count), char| {
let pattern = format!("{}{}", last_char, char);
count += pair_to_paths_map
.get(&pattern)
.unwrap()
.iter()
.map(|next_part| count_levels(&next_part, depth - 1, pair_to_paths_map, memo))
.min()
.unwrap();
(char, count)
});
memo.insert((current_sequence.clone(), depth), count);
count
}
fn task(input: &str, n_robots: usize) -> String {
let numpad = Keypad::numpad();
let arrowpad = Keypad::arrowpad();
let mut pair_to_paths_map = numpad.generate_sequences();
arrowpad.generate_sequences().iter().for_each(|(k, v)| {
pair_to_paths_map.insert(k.clone(), v.clone());
});
let mut memo = HashMap::new();
input
.lines()
.fold(0, |acc, line| {
let min_len = count_levels(&line.to_string(), n_robots, &pair_to_paths_map, &mut memo);
acc + min_len * line[..line.len() - 1].parse::<usize>().unwrap()
})
.to_string()
}
pub fn task01(input: &str) -> String {
task(input, 3)
}
pub fn task02(input: &str) -> String {
task(input, 26)
}
#[cfg(test)]
mod tests {
use super::super::fs_utils::{read_example, read_input};
use super::*;
#[test]
fn test_task01() {
let input = read_example(21, 1);
assert_eq!(task01(&input), "126384");
}
#[test]
fn run_task01() {
let input = read_input(21);
assert_eq!(task01(&input), "215374");
}
#[test]
fn test_task02() {
let input = read_example(21, 1);
assert_eq!(task02(&input), "154115708116294");
}
#[test]
fn run_task02() {
let input = read_input(21);
assert_eq!(task02(&input), "260586897262600");
}
}