-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.rs
172 lines (153 loc) · 4.66 KB
/
day1.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use aoc_runner_derive::aoc;
#[aoc(day1, part1)]
#[must_use]
pub fn part1(input: &str) -> u32 {
input
.lines()
.map(|line| {
let mut digits = line.chars().filter_map(|c| c.to_digit(10));
let first_digit = digits.next().unwrap();
let last_digit = digits.next_back().unwrap_or(first_digit);
10 * first_digit + last_digit
})
.sum()
}
#[aoc(day1, part2)]
#[must_use]
pub fn part2(input: &str) -> u32 {
input
.lines()
.map(|line| {
let (first_digit, last_digit) = utils::Digit::find_first_and_last(line);
10 * (first_digit as u32) + (last_digit as u32)
})
.sum()
}
mod utils {
use strum::{EnumIter, IntoEnumIterator};
#[repr(u32)]
#[derive(Clone, Copy, EnumIter)]
/// Represents a digit from 1 to 9.
pub enum Digit {
One = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
}
impl Digit {
/// Finds the first and last instances of `Digit` in a string.
/// Digit can be either spelled out or in numeric form.
///
/// # Returns
/// (Digit, Digit) in the form of (first digit, last digit).
pub fn find_first_and_last(input: &str) -> (Self, Self) {
let mut first_index = usize::MAX;
let mut last_index = usize::MIN;
let mut first_digit = Self::One;
let mut last_digit = Self::One;
for digit in Self::iter() {
let digit_spelling = digit.as_spelling();
let digit_char = digit.as_char();
// Get index to the first instance of the digit
let digit_first_index = {
match (input.find(digit_spelling), input.find(digit_char)) {
(Some(s), Some(c)) => s.min(c),
(Some(s), None) => s,
(None, Some(c)) => c,
(None, None) => continue,
}
};
// Get index to the last instance of the digit
let digit_last_index = {
match (input.rfind(digit_spelling), input.rfind(digit_char)) {
(Some(s), Some(c)) => s.max(c),
(Some(s), None) => s,
(None, Some(c)) => c,
_ => unreachable!(),
}
};
// Update the first and last indices
if digit_first_index <= first_index {
first_index = digit_first_index;
first_digit = digit;
}
if digit_last_index >= last_index {
last_index = digit_last_index;
last_digit = digit;
}
}
(first_digit, last_digit)
}
/// Returns the spelling of the digit.
fn as_spelling(self) -> &'static str {
match self {
Self::One => "one",
Self::Two => "two",
Self::Three => "three",
Self::Four => "four",
Self::Five => "five",
Self::Six => "six",
Self::Seven => "seven",
Self::Eight => "eight",
Self::Nine => "nine",
}
}
/// Returns the digit as a char.
fn as_char(self) -> char {
match self {
Self::One => '1',
Self::Two => '2',
Self::Three => '3',
Self::Four => '4',
Self::Five => '5',
Self::Six => '6',
Self::Seven => '7',
Self::Eight => '8',
Self::Nine => '9',
}
}
}
impl From<Digit> for u32 {
fn from(value: Digit) -> Self {
value as u32
}
}
impl From<&Digit> for u32 {
fn from(value: &Digit) -> Self {
(*value).into()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
#[test]
pub fn part1_example() {
const SAMPLE: &str = indoc! {"
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
"};
assert_eq!(part1(SAMPLE), 142);
}
#[test]
pub fn part2_example() {
const SAMPLE: &str = indoc! {"
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
"};
assert_eq!(part2(SAMPLE), 281);
}
}