-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03.rs
More file actions
138 lines (119 loc) · 3.62 KB
/
03.rs
File metadata and controls
138 lines (119 loc) · 3.62 KB
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
advent_of_code::solution!(3);
use grid::Grid;
use itertools::Itertools;
use regex::Regex;
#[derive(Clone, PartialEq)]
struct Point {
x: usize,
y: usize,
}
#[derive(Clone)]
struct Part {
location: Point,
part_type: char,
}
fn create_grid(input: &str) -> Grid<char> {
// Create a grid and fill it with our input
let mut grid: Grid<char> = Grid::new(0, 0);
for l in input.lines() {
grid.push_row(l.chars().collect_vec());
}
grid
}
fn parse_part(location: (usize, usize), part_type: &char) -> Option<Part> {
match part_type {
// Filter out any dots or numbers, keeping all the parts
'0'..='9' | '.' => None,
_ => Some(Part {
part_type: *part_type,
location: Point {
x: location.0,
y: location.1,
},
}),
}
}
fn find_adjacent_points(point: &Point) -> Vec<Point> {
let mut adjacent_points: Vec<Point> = vec![];
// Loop around the point generating a vec
// There are no parts on the edge of the schematic so we do not worry about over/underflowing
for x in (point.x - 1)..=point.x + 1 {
for y in (point.y - 1)..=(point.y + 1) {
adjacent_points.push(Point { x, y });
}
}
adjacent_points
}
fn discover_numbers(part: &Part, grid: &Grid<char>) -> Vec<u32> {
let adjacent_points = find_adjacent_points(&part.location);
let re = Regex::new(r"\d+").unwrap();
let mut matches: Vec<u32> = vec![];
for x in (part.location.x - 1)..=part.location.x + 1 {
let row = grid.iter_row(x).collect::<String>();
for m in re.find_iter(&row) {
let match_range = m.start()..m.end();
for y in match_range {
if adjacent_points.contains(&Point { x, y }) {
//Parse the match and push the result into the part
matches.push(m.as_str().parse::<u32>().unwrap());
// Move onto the next regex match if a gear is touching
break;
}
}
}
}
matches
}
fn get_parts_list(grid: Grid<char>) -> Vec<(Part, Vec<u32>)> {
grid.indexed_iter()
.filter_map(|(location, part_type)| parse_part(location, part_type))
.map(|part| {
// Discover the matches for the part
let matches = discover_numbers(&part, &grid);
(part, matches)
})
.collect_vec()
}
pub fn part_one(input: &str) -> Option<u32> {
let grid = create_grid(input);
let parts: Vec<(Part, Vec<u32>)> = get_parts_list(grid);
Some(
parts
.iter()
.map(|(_, matches)| matches.iter().sum::<u32>())
.sum::<u32>(),
)
}
pub fn part_two(input: &str) -> Option<u32> {
let grid = create_grid(input);
let parts: Vec<(Part, Vec<u32>)> = get_parts_list(grid);
Some(
parts
.iter()
.filter_map(|(part, matches)| match part.part_type {
'*' => {
if matches.len() == 2 {
Some(matches.iter().product::<u32>())
} else {
None
}
}
_ => None,
})
.sum::<u32>(),
)
}
#[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));
}
}