-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd01.rs
More file actions
85 lines (74 loc) · 2.28 KB
/
d01.rs
File metadata and controls
85 lines (74 loc) · 2.28 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use lazy_static::lazy_static;
use regex::Regex;
use crate::Day;
pub struct Day01 {}
impl Day for Day01 {
fn year(&self) -> u16 {
2023
}
fn day(&self) -> u8 {
1
}
fn part_one(&self) -> String {
self.read_default_input()
.lines()
.map(|line| line.chars().filter(|c| c.is_ascii_digit()).collect())
.map(|digits: Vec<char>| {
let digits = if digits.len() == 1 {
vec![*digits.first().unwrap(), *digits.first().unwrap()]
} else if digits.len() > 2 {
vec![*digits.first().unwrap(), *digits.last().unwrap()]
} else {
digits
};
digits.iter().collect::<String>()
})
.map(|digit| digit.parse::<i64>())
.map(|result| result.unwrap())
.sum::<i64>()
.to_string()
}
fn part_two(&self) -> String {
self.read_default_input()
.lines()
.map(|line| {
let first = first_digit_char(line);
let last = first_digit_char(&line.chars().rev().collect::<String>());
[first, last].iter().collect::<String>()
})
.map(|digit| digit.parse::<i64>().unwrap())
.sum::<i64>()
.to_string()
}
}
fn first_digit_char(str: &str) -> char {
DIGIT_PATTERN
.find(str)
.map(|match_item| match_item.as_str())
.map(|digit_str| {
if digit_str.len() == 1 {
digit_str.chars().next().unwrap()
} else {
digit_str_to_char(digit_str)
}
})
.unwrap()
}
lazy_static! {
static ref DIGIT_PATTERN: Regex =
Regex::new(r"\d|one|eno|two|owt|three|eerht|four|ruof|five|evif|six|xis|seven|neves|eight|thgie|nine|enin").unwrap();
}
fn digit_str_to_char(digit_str: &str) -> char {
match digit_str {
"one" | "eno" => '1',
"two" | "owt" => '2',
"three" | "eerht" => '3',
"four" | "ruof" => '4',
"five" | "evif" => '5',
"six" | "xis" => '6',
"seven" | "neves" => '7',
"eight" | "thgie" => '8',
"nine" | "enin" => '9',
_ => panic!("invalid digit literal"),
}
}