-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
37 lines (32 loc) · 906 Bytes
/
lib.rs
File metadata and controls
37 lines (32 loc) · 906 Bytes
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
use common::Answer;
fn chars_to_number(first: char, second: char) -> u32 {
first.to_digit(10).unwrap() * 10 + second.to_digit(10).unwrap()
}
fn clean_input(s: &str) -> String {
s.replace("zero", "z0o")
.replace("one", "o1e")
.replace("two", "t2o")
.replace("three", "t3e")
.replace("four", "f4r")
.replace("five", "f5e")
.replace("six", "s6x")
.replace("seven", "s7n")
.replace("eight", "e8t")
.replace("nine", "n9e")
}
pub fn step1(s: &str) -> Answer {
s.lines()
.map(|l| {
(
l.chars().find(|c| c.is_numeric()).unwrap(),
l.chars().rev().find(|c| c.is_numeric()).unwrap(),
)
})
.map(|(c1, c2)| chars_to_number(c1, c2))
.sum::<u32>()
.into()
}
pub fn step2(s: &str) -> Answer {
let s = clean_input(s);
step1(&s)
}