generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.rs
148 lines (123 loc) · 3.8 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
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
advent_of_code::solution!(1);
pub fn part_one(input: &str) -> Option<u32> {
let mut sum = 0;
for line in input.lines() {
let first_number = line
.chars()
.find(|&c| c.is_ascii_digit())
.map(|c| c.to_digit(10).unwrap());
let last_number = line
.chars()
.rev()
.find(|&c| c.is_ascii_digit())
.map(|c| c.to_digit(10).unwrap());
if let (Some(first), Some(last)) = (first_number, last_number) {
let number = first * 10 + last;
sum += number;
}
}
Some(sum)
}
pub fn part_two(input: &str) -> Option<u32> {
let number_strings = [
(1, "one"),
(2, "two"),
(3, "three"),
(4, "four"),
(5, "five"),
(6, "six"),
(7, "seven"),
(8, "eight"),
(9, "nine"),
];
let mut sum = 0;
for line in input.lines() {
let string_positions = get_string_positions(line, &number_strings);
let mut first_position = get_first_numeric_position(line);
let mut last_position = get_last_numeric_position(line);
for (position_first, position_last) in string_positions {
if let Some(position) = position_first {
if let Some(ref first) = first_position {
if position.index < first.index {
let position = Some(position);
first_position = position;
}
} else {
first_position = Some(position);
}
}
if let Some(position) = position_last {
if let Some(ref last) = last_position {
if position.index > last.index {
last_position = Some(position);
}
} else {
last_position = Some(position);
}
}
}
if let (Some(first), Some(last)) = (first_position, last_position) {
let number = first.number * 10 + last.number;
sum += number;
}
}
Some(sum)
}
fn get_string_positions<const N: usize>(
line: &str,
number_strings: &[(u32, &str); N],
) -> [(Option<NumberPosition>, Option<NumberPosition>); N] {
number_strings.map(|(number, number_string)| {
let position_first = line
.find(number_string)
.map(|index| NumberPosition { number, index });
let position_last = line
.rfind(number_string)
.map(|index| NumberPosition { number, index });
(position_first, position_last)
})
}
fn get_first_numeric_position(line: &str) -> Option<NumberPosition> {
for (index, character) in line.chars().enumerate() {
if character.is_ascii_digit() {
return Some(NumberPosition {
number: character.to_digit(10).unwrap(),
index,
});
}
}
None
}
fn get_last_numeric_position(line: &str) -> Option<NumberPosition> {
for (index, character) in line.chars().rev().enumerate() {
if character.is_ascii_digit() {
return Some(NumberPosition {
number: character.to_digit(10).unwrap(),
index: line.len() - 1 - index,
});
}
}
None
}
struct NumberPosition {
number: u32,
index: usize,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file_part(
"examples", DAY, 1,
));
assert_eq!(result, Some(142));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file_part(
"examples", DAY, 2,
));
assert_eq!(result, Some(281));
}
}