-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
141 lines (127 loc) · 3.72 KB
/
main.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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
#[allow(clippy::large_stack_frames)]
pub fn main() {
let data = include_str!("input.txt");
let (parts, gears) = find_parts::<140>(data.as_bytes());
println!("Part 1: {parts}");
println!("Part 2: {}", part_two(gears));
}
fn part_two<const R: usize>(gears: [[GearParts; R]; R]) -> u32 {
gears.iter().flatten().map(GearParts::gear_ratio).sum()
}
fn find_parts<const R: usize>(data: &[u8]) -> (u32, [[GearParts; R]; R]) {
let mut acc = 0;
let mut gears = [[GearParts::None; R]; R];
let mut part = 0;
let mut start = None;
for (idx, ch) in data.iter().enumerate() {
match (start, ch.is_ascii_digit()) {
(Some(_), true) => {
// In a number sting & continuing
part *= 10;
part += u32::from(ch - b'0');
}
(Some(s), false) => {
// In a number sting & ended
if has_symbol_adjacent::<R>(data, s, idx - 1) {
acc += part;
check_for_gears(&mut gears, data, s, idx - 1, part);
}
part = 0;
start = None;
}
(None, true) => {
// Not in a number but just started
part = u32::from(ch - b'0');
start = Some(idx);
}
(None, false) => {} // Not in a number & not started one
}
}
(acc, gears)
}
fn has_symbol_adjacent<const R: usize>(data: &[u8], start: usize, end: usize) -> bool {
(start > 0 && is_symbol(data, start - 1))
|| (end < data.len() && is_symbol(data, end + 1))
|| (start > R && ((start - R - 2)..=(end - R)).any(|i| is_symbol(data, i)))
|| (start + R < data.len() && ((start + R)..=(end + R + 2)).any(|i| is_symbol(data, i)))
}
const fn is_symbol(data: &[u8], idx: usize) -> bool {
let val = data[idx];
!(val.is_ascii_digit() || val == b'.' || val == b'\n')
}
fn check_for_gears<const R: usize>(
gears: &mut [[GearParts; R]; R],
data: &[u8],
start: usize,
end: usize,
part: u32,
) {
if start > 0 {
check_for_gear(gears, data, start - 1, part);
}
if end < data.len() {
check_for_gear(gears, data, end + 1, part);
}
if start > R {
for idx in (start - R - 2)..=(end - R) {
check_for_gear(gears, data, idx, part);
}
}
if start + R < data.len() {
for idx in (start + R)..=(end + R + 2) {
check_for_gear(gears, data, idx, part);
}
}
}
fn check_for_gear<const R: usize>(
gears: &mut [[GearParts; R]; R],
data: &[u8],
idx: usize,
part: u32,
) {
if data[idx] == b'*' {
let row = idx % (R + 1);
let col = idx / (R + 1);
gears[row][col].add(part);
}
}
#[derive(Clone, Copy)]
enum GearParts {
None,
One(u32),
Two((u32, u32)),
Overflow,
}
impl GearParts {
fn add(&mut self, val: u32) {
match *self {
Self::None => *self = Self::One(val),
Self::One(prev) => *self = Self::Two((prev, val)),
Self::Two(_) => *self = Self::Overflow,
Self::Overflow => {}
}
}
const fn gear_ratio(&self) -> u32 {
match *self {
Self::Two((a, b)) => a * b,
_ => 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
let (parts, _) = find_parts::<10>(data.as_bytes());
assert_eq!(4361, parts);
}
#[test]
fn two() {
let data = include_str!("test.txt");
let (_, gears) = find_parts::<10>(data.as_bytes());
assert_eq!(467_835, part_two::<10>(gears));
}
}