This repository has been archived by the owner on Jan 6, 2024. It is now read-only.
/
01.rs
55 lines (45 loc) · 1.41 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
advent_of_code::solution!(1);
const LUT: [&str; 9] = [
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
];
pub fn part_one(input: &str) -> Option<u32> {
Some(input.lines().map(parse_line_1).sum())
}
pub fn part_two(input: &str) -> Option<u32> {
Some(input.lines().map(parse_line_2).sum())
}
fn parse_line_1(line: &str) -> u32 {
let first = line.chars().find_map(|c| c.to_digit(10));
let last = line.chars().rev().find_map(|c| c.to_digit(10));
10 * first.unwrap() + last.unwrap()
}
fn parse_line_2(line: &str) -> u32 {
let first = find_pattern(0..line.len(), line);
let last = find_pattern((0..line.len()).rev(), line);
10 * first + last
}
fn find_pattern(mut it: impl Iterator<Item = usize>, line: &str) -> u32 {
it.find_map(|i| compare_slice(&line[i..])).unwrap()
}
fn compare_slice(slice: &str) -> Option<u32> {
LUT.iter()
.enumerate()
.find(|(_, pattern)| slice.starts_with(*pattern))
.map(|(i, _)| i as u32 + 1)
.or_else(|| slice.chars().next().unwrap().to_digit(10))
}
#[cfg(test)]
mod tests {
use super::*;
use advent_of_code::template::*;
#[test]
fn test_part_one() {
let result = part_one(&read_example_part(DAY, 1));
assert_eq!(result, Some(142));
}
#[test]
fn test_part_two() {
let result = part_two(&read_example_part(DAY, 2));
assert_eq!(result, Some(281));
}
}