generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.rs
101 lines (83 loc) · 2.1 KB
/
01.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
advent_of_code::solution!(1);
use std::collections::HashMap;
fn parse_data(input: &str) -> Vec<&str> {
input.lines().collect()
}
fn part_x(data: Vec<&str>, digit_map: HashMap<&str, u8>) -> u32 {
let mut result = 0;
for line in data.into_iter() {
let mut tmp_result = vec![];
for i in 0..line.len() {
let digit = digit_map
.iter()
.find(|(key, _)| line[i..].starts_with(*key));
if let Some((_, value)) = digit {
tmp_result.push(value)
}
}
let mut tmp_result_iter = tmp_result.into_iter();
let first = *tmp_result_iter.next().unwrap();
let last = *tmp_result_iter.next_back().unwrap_or(&first);
result += first as u32 * 10 + last as u32;
}
result
}
pub fn part_one(input: &str) -> Option<u32> {
let data = parse_data(input);
let digit_map = HashMap::from([
("1", 1),
("2", 2),
("3", 3),
("4", 4),
("5", 5),
("6", 6),
("7", 7),
("8", 8),
("9", 9),
]);
let result = part_x(data, digit_map);
Some(result)
}
pub fn part_two(input: &str) -> Option<u32> {
let data = parse_data(input);
let digit_map = HashMap::from([
("1", 1),
("one", 1),
("2", 2),
("two", 2),
("3", 3),
("three", 3),
("4", 4),
("four", 4),
("5", 5),
("five", 5),
("6", 6),
("six", 6),
("7", 7),
("seven", 7),
("8", 8),
("eight", 8),
("9", 9),
("nine", 9),
]);
let result = part_x(data, digit_map);
Some(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file_part(
"examples", DAY, 1,
));
assert_eq!(result, Some(142));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file_part(
"examples", DAY, 2,
));
assert_eq!(result, Some(281));
}
}