-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
155 lines (138 loc) · 3.44 KB
/
main.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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> u32 {
let mut kp = Keypad::default();
for line in data.lines() {
kp.process_line(line);
}
kp.code
}
fn part_two(data: &str) -> String {
let mut kp = Keypad2::default();
for line in data.lines() {
kp.process_line(line);
}
kp.code
}
struct Keypad2 {
row: u8,
col: u8,
code: String,
}
impl Default for Keypad2 {
fn default() -> Self {
Self {
row: 3,
col: 1,
code: String::new(),
}
}
}
impl Keypad2 {
fn next(&mut self, instruction: char) {
match (self.row, self.col, instruction) {
// No-ops first
(3, 1, 'U' | 'D' | 'L') // 5
| (2, 2, 'U' | 'L') // 2
| (1, 3, 'U' | 'L' | 'R') // 1
| (2, 4, 'U' | 'R') // 4
| (3, 5, 'U' | 'D' | 'R') // 9
| (4, 4, 'D' | 'R') // C
| (5, 3, 'D' | 'L' | 'R') // D
| (4, 2, 'D' | 'L') => (), // A
(_, _, 'U') => self.row -= 1,
(_, _, 'D') => self.row += 1,
(_, _, 'L') => self.col -= 1,
(_, _, 'R') => self.col += 1,
_ => unreachable!(),
}
}
fn process_line(&mut self, line: &str) {
for instruction in line.chars() {
self.next(instruction);
}
self.code.push(self.number());
}
fn number(&self) -> char {
match (self.row, self.col) {
(1, 3) => '1',
(2, 2) => '2',
(2, 3) => '3',
(2, 4) => '4',
(3, 1) => '5',
(3, 2) => '6',
(3, 3) => '7',
(3, 4) => '8',
(3, 5) => '9',
(4, 2) => 'A',
(4, 3) => 'B',
(4, 4) => 'C',
(5, 3) => 'D',
_ => unreachable!(),
}
}
}
struct Keypad {
row: u8,
col: u8,
code: u32,
}
impl Default for Keypad {
fn default() -> Self {
Self {
row: 2,
col: 2,
code: 0,
}
}
}
impl Keypad {
fn next(&mut self, instruction: char) {
match instruction {
'U' => self.row = std::cmp::max(self.row - 1, 1),
'D' => self.row = std::cmp::min(self.row + 1, 3),
'L' => self.col = std::cmp::max(self.col - 1, 1),
'R' => self.col = std::cmp::min(self.col + 1, 3),
_ => unreachable!(),
}
}
fn process_line(&mut self, line: &str) {
for instruction in line.chars() {
self.next(instruction);
}
self.code *= 10;
self.code += self.number();
}
fn number(&self) -> u32 {
match (self.row, self.col) {
(1, 1) => 1,
(1, 2) => 2,
(1, 3) => 3,
(2, 1) => 4,
(2, 2) => 5,
(2, 3) => 6,
(3, 1) => 7,
(3, 2) => 8,
(3, 3) => 9,
_ => unreachable!(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(1985, part_one(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!("5DB3", part_two(data));
}
}