-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.rs
109 lines (96 loc) · 2.61 KB
/
day2.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
// vi: set shiftwidth=4 tabstop=4 expandtab:
use common::input::check_answer;
use common::input::collect_lines;
use common::input::get_answers;
use common::input::get_file_content;
use std::collections::HashMap;
use std::time::Instant;
const INPUT_FILEPATH: &str = "../resources/year2016_day2_input.txt";
const ANSWERS_FILEPATH: &str = "../resources/year2016_day2_answer.txt";
type InputContent = Vec<String>;
fn get_input_from_str(string: &str) -> InputContent {
collect_lines(string)
}
type Keypad = Vec<String>;
type KeypadGraph = HashMap<(char /*Position*/, char /*Direction*/), char /*New position*/>;
fn build_keypad_graph(keypad: &Keypad) -> KeypadGraph {
let mut graph = KeypadGraph::new();
for s in keypad {
for (a, b) in s.chars().zip(s.chars().skip(1)) {
if a != ' ' && b != ' ' {
graph.insert((a, 'R'), b);
graph.insert((b, 'L'), a);
}
}
}
for (s1, s2) in keypad.iter().zip(keypad.iter().skip(1)) {
for (a, b) in s1.chars().zip(s2.chars()) {
if a != ' ' && b != ' ' {
graph.insert((a, 'D'), b);
graph.insert((b, 'U'), a);
}
}
}
graph
}
fn get_code(keypad: &Keypad, instructions: &InputContent) -> String {
let g = build_keypad_graph(keypad);
let mut pos = '5';
let mut positions = Vec::new();
for instruction in instructions {
for c in instruction.chars() {
if let Some(new_position) = g.get(&(pos, c)) {
pos = *new_position;
}
}
positions.push(pos);
}
positions.iter().collect()
}
fn part1(instructions: &InputContent) -> String {
let keypad = collect_lines(
"
123
456
789",
);
get_code(&keypad, instructions)
}
fn part2(instructions: &InputContent) -> String {
let keypad = collect_lines(
"
1
234
56789
ABC
D ",
);
get_code(&keypad, instructions)
}
fn main() {
let before = Instant::now();
let data = get_input_from_str(&get_file_content(INPUT_FILEPATH));
let (ans, ans2) = get_answers(ANSWERS_FILEPATH);
let solved = true;
let res = part1(&data);
check_answer(&res, ans, solved);
let res2 = part2(&data);
check_answer(&res2, ans2, solved);
println!("Elapsed time: {:.2?}", before.elapsed());
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE: &str = "ULL
RRDDD
LURDL
UUUUD";
#[test]
fn test_part1() {
assert_eq!(part1(&get_input_from_str(EXAMPLE)), "1985");
}
#[test]
fn test_part2() {
assert_eq!(part2(&get_input_from_str(EXAMPLE)), "5DB3");
}
}