-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday01.rs
59 lines (52 loc) · 1.78 KB
/
day01.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
//! # Trebuchet?!
//!
//! The input can contain overlapping digits such as "twone", so we only remove a letter at a time
//! until the starting or ending digits are found.
use crate::util::parse::*;
/// Use the index of each digit as its implicit value.
const DIGITS: [&[u8]; 9] =
[b"one", b"two", b"three", b"four", b"five", b"six", b"seven", b"eight", b"nine"];
pub fn parse(input: &str) -> Vec<&str> {
input.lines().collect()
}
pub fn part1(input: &[&str]) -> u32 {
input
.iter()
.map(|line| {
let first = line.bytes().find(u8::is_ascii_digit).unwrap().to_decimal();
let last = line.bytes().rfind(u8::is_ascii_digit).unwrap().to_decimal();
(10 * first + last) as u32
})
.sum()
}
pub fn part2(input: &[&str]) -> usize {
input
.iter()
.map(|line| {
let mut line = line.as_bytes();
let first = 'outer: loop {
if line[0].is_ascii_digit() {
break line[0].to_decimal() as usize;
}
for (value, digit) in DIGITS.iter().enumerate() {
if line.starts_with(digit) {
break 'outer value + 1;
}
}
line = &line[1..];
};
let last = 'outer: loop {
if line[line.len() - 1].is_ascii_digit() {
break line[line.len() - 1].to_decimal() as usize;
}
for (value, digit) in DIGITS.iter().enumerate() {
if line.ends_with(digit) {
break 'outer value + 1;
}
}
line = &line[..line.len() - 1];
};
10 * first + last
})
.sum()
}