This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.rs
161 lines (130 loc) · 3.76 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
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
advent_of_code::solution!(3);
#[derive(PartialEq)]
enum Coord {
Num(u32),
Symbol(char),
Void,
}
impl Coord {
fn is_num(&self) -> bool {
match self {
Coord::Num(_) => true,
_ => false,
}
}
fn is_symbol(&self) -> bool {
match self {
Coord::Symbol(_) => true,
_ => false,
}
}
}
fn parse(input: &str) -> Vec<Vec<Coord>> {
input.lines().map(|l| l.chars().map(|c| match c {
'.' => Coord::Void,
c if c.is_ascii_digit() => Coord::Num(c.to_digit(10).unwrap()),
c => Coord::Symbol(c)
}).collect()).collect()
}
fn is_part(plot: &Vec<Vec<Coord>>, x: usize, y: usize) -> bool {
if let Coord::Num(_) = plot[y][x] {
for i in -1..=1 {
for j in -1..=1 {
if y.saturating_add_signed(i) < plot.len() && x.saturating_add_signed(j) < plot[y].len() {
if plot[y.saturating_add_signed(i)][x.saturating_add_signed(j)].is_symbol() {
return true;
}
}
}
}
}
false
}
fn parse_whole_number(plot: &Vec<Vec<Coord>>, x: usize, y: usize) -> u32 {
let mut x = x;
while x > 0 && plot[y][x].is_num() {
x -= 1;
}
if !plot[y][x].is_num() {
x += 1
}
let mut result: u32 = 0;
while x < plot[y].len() {
if let Coord::Num(n) = plot[y][x] {
result = result*10 + n;
} else { break; }
x += 1;
}
result
}
pub fn part_one(input: &str) -> Option<u32> {
let input = parse(input);
let mut result: u32 = 0;
for y in 0..input.len() {
let mut flag = false;
for x in 0..input[y].len() {
if flag {
match input[y][x] {
Coord::Num(_) => continue,
_ => flag = false,
}
}
if is_part(&input, x, y) {
flag = true;
result += parse_whole_number(&input, x, y);
}
}
}
Some(result)
}
fn gear_ratio(plot: &Vec<Vec<Coord>>, x: usize, y: usize) -> u32 {
let mut nums: Vec<u32> = vec![];
for i in [-1, 1].iter() {
if plot[y][x.saturating_add_signed(*i)].is_num() {
nums.push(parse_whole_number(plot, x.saturating_add_signed(*i), y));
}
}
for i in [-1, 1] {
if plot[y.saturating_add_signed(i)][x].is_num() {
nums.push(parse_whole_number(plot, x, y.saturating_add_signed(i)));
} else {
if plot[y.saturating_add_signed(i)][x.saturating_add_signed(i)].is_num() {
nums.push(parse_whole_number(plot, x.saturating_add_signed(i), y.saturating_add_signed(i)));
}
if plot[y.saturating_add_signed(i)][x.saturating_add_signed(-i)].is_num() {
nums.push(parse_whole_number(plot, x.saturating_add_signed(-i), y.saturating_add_signed(i)));
}
}
}
if nums.len() == 2 {
nums[0] * nums[1]
} else {
0
}
}
pub fn part_two(input: &str) -> Option<u32> {
let input = parse(input);
let mut result: u32 = 0;
for y in 0..input.len() {
for x in 0..input[y].len() {
if let Coord::Symbol('*') = input[y][x] {
result += gear_ratio(&input, x, y);
}
}
}
Some(result)
}
#[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));
}
}