generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.rs
120 lines (113 loc) · 4.28 KB
/
03.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
use std::collections::HashMap;
advent_of_code::solution!(3);
pub fn part_one(input: &str) -> Option<u32> {
let mut part_sum = 0;
let mut cur_num: Option<u32> = None;
let mut cur_start: Option<usize> = None;
let chars: Vec<Vec<char>> = input
.lines()
.map(|line| line.trim().chars().collect())
.collect();
for (row, line) in chars.iter().enumerate() {
for (col, ch) in line.iter().copied().enumerate() {
let mut numeric = false;
if let Some(digit) = ch.to_digit(10) {
numeric = true;
cur_num = Some(cur_num.unwrap_or(0) * 10 + digit);
if cur_start.is_none() {
cur_start = Some(col);
}
}
if !numeric || col == line.len() - 1 {
if let Some((start, number)) = cur_start.zip(cur_num) {
let end = if numeric { col } else { col - 1 };
let mut symbol: Option<char> = None;
for i in row.saturating_sub(1)..=row.saturating_add(1) {
for j in start.saturating_sub(1)..=end.saturating_add(1) {
if let Some(&adj_ch) = chars.get(i).and_then(|l| l.get(j)) {
if !adj_ch.is_numeric() && adj_ch != '.' {
symbol = Some(adj_ch);
}
}
}
}
if symbol.is_some() {
part_sum += number;
}
}
cur_start = None;
cur_num = None;
}
}
}
Some(part_sum)
}
pub fn part_two(input: &str) -> Option<u32> {
let mut gears: HashMap<(usize, usize), Gear> = HashMap::new();
let mut cur_num: Option<u32> = None;
let mut cur_start: Option<usize> = None;
let chars: Vec<Vec<char>> = input
.lines()
.map(|line| line.trim().chars().collect())
.collect();
// TODO: scan for gears instead, and only parse numbers when gears are found?
for (row, line) in chars.iter().enumerate() {
for (col, ch) in line.iter().copied().enumerate() {
let mut numeric = false;
if let Some(digit) = ch.to_digit(10) {
numeric = true;
cur_num = Some(cur_num.unwrap_or(0) * 10 + digit);
if cur_start.is_none() {
cur_start = Some(col);
}
}
if !numeric || col == line.len() - 1 {
if let Some((start, number)) = cur_start.zip(cur_num) {
let end = if numeric { col } else { col - 1 };
let mut counted_gear_part = false;
for i in row.saturating_sub(1)..=row.saturating_add(1) {
for j in start.saturating_sub(1)..=end.saturating_add(1) {
if let Some(&adj_ch) = chars.get(i).and_then(|l| l.get(j)) {
if adj_ch == '*' && !counted_gear_part {
let gear = gears.entry((i, j)).or_insert(Gear {
ratio: 1,
adjacent_parts: 0,
});
gear.ratio *= number;
gear.adjacent_parts += 1;
counted_gear_part = true;
}
}
}
}
}
cur_start = None;
cur_num = None;
}
}
}
Some(
gears
.into_values()
.filter_map(|gear| (gear.adjacent_parts == 2).then_some(gear.ratio))
.sum(),
)
}
struct Gear {
ratio: u32,
adjacent_parts: usize,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(4361));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(467835));
}
}