generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.rs
83 lines (78 loc) · 2.14 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
advent_of_code::solution!(1);
pub fn part_one(input: &str) -> Option<u32> {
Some(
input
.lines()
.filter_map(|line| {
let mut nums = line.chars().filter_map(|ch| ch.to_digit(10));
let first = nums.next();
let last = nums.next_back().or(first);
first.zip(last).map(|(a, b)| a * 10 + b)
})
.sum(),
)
}
pub fn part_two(input: &str) -> Option<u32> {
let digit_strs = [
("one", 1u32),
("two", 2),
("three", 3),
("four", 4),
("five", 5),
("six", 6),
("seven", 7),
("eight", 8),
("nine", 9),
("1", 1),
("2", 2),
("3", 3),
("4", 4),
("5", 5),
("6", 6),
("7", 7),
("8", 8),
("9", 9),
];
Some(
input
.lines()
.filter_map(|line| {
let first = (0..line.len()).find_map(|start| {
digit_strs.iter().find_map(|&(digit_str, val)| {
if line[start..].starts_with(digit_str) {
Some(val)
} else {
None
}
})
});
let last = (0..line.len()).rev().find_map(|end| {
digit_strs.iter().find_map(|&(digit_str, val)| {
if line[..=end].ends_with(digit_str) {
Some(val)
} else {
None
}
})
});
first.zip(last).map(|(a, b)| a * 10 + b)
})
.sum(),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
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));
}
}