-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01.rs
More file actions
62 lines (55 loc) · 1.65 KB
/
01.rs
File metadata and controls
62 lines (55 loc) · 1.65 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
advent_of_code::solution!(1);
pub fn part_one(input: &str) -> Option<u32> {
let result = input
.lines()
.filter_map(|lll| {
let first = lll.chars().find(char::is_ascii_digit);
let second = lll.chars().rfind(char::is_ascii_digit);
Some(first?.to_digit(10)? * 10 + second?.to_digit(10)?)
})
.sum();
Some(result)
}
pub fn part_two(input: &str) -> Option<u32> {
let result = input
.lines()
.filter(|line| !line.is_empty())
.map(|line| {
line.to_string()
.replace("one", "one1one")
.replace("two", "two2two")
.replace("three", "three3three")
.replace("four", "four4four")
.replace("five", "five5five")
.replace("six", "six6six")
.replace("seven", "seven7seven")
.replace("eight", "eight8eight")
.replace("nine", "nine9nine")
})
.map(|line| {
line.chars()
.filter_map(|c| c.to_digit(10))
.collect::<Vec<u32>>()
})
.map(|vec| 10 * vec.first().unwrap() + vec.last().unwrap())
.sum();
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(142u32));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file_part(
"examples", DAY, 2,
));
assert_eq!(result, Some(281));
}
}