forked from fspoettel/advent-of-code-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.rs
132 lines (109 loc) · 3.25 KB
/
14.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
use advent_of_code::{Direction, Point, SparseGrid};
use itertools::Itertools;
use std::cmp;
type Grid = SparseGrid<PointType>;
type Input<'a> = (Grid, isize);
#[derive(Clone, Eq, PartialEq)]
pub enum PointType {
Wall,
Sand,
}
static SOURCE: Point = Point { x: 500, y: 0 };
fn parse(input: &str) -> Input {
input
.lines()
.fold((SparseGrid::default(), 0_isize), |mut acc, l| {
l.split(" -> ")
.filter_map(|segment| {
let (x, y) = segment.split_once(',')?;
Some(Point {
x: x.parse().ok()?,
y: y.parse().ok()?,
})
})
.tuple_windows()
.for_each(|(a, b)| {
for point in a.line_to(&b) {
acc.1 = cmp::max(acc.1, point.y);
acc.0.insert(point, PointType::Wall);
}
});
acc
})
}
fn next_pos(grid: &Grid, point: &Point) -> Option<Point> {
let south = point.get_neighbour(&Direction::South, 1);
if grid.get(&south).is_none() {
return Some(south);
}
let south_east = point.get_neighbour(&Direction::SouthEast, 1);
if grid.get(&south_east).is_none() {
return Some(south_east);
}
let south_west = point.get_neighbour(&Direction::SouthWest, 1);
if grid.get(&south_west).is_none() {
return Some(south_west);
}
None
}
fn count_sand(grid: &Grid) -> usize {
grid.points
.values()
.filter(|x| **x == PointType::Sand)
.count()
}
pub fn part_one((mut grid, floor): Input) -> Option<usize> {
let mut current_path = vec![SOURCE.clone()];
loop {
let current_sand = ¤t_path[current_path.len() - 1];
match next_pos(&grid, current_sand) {
Some(next_sand) => {
if current_sand.y >= floor {
break;
} else {
current_path.push(next_sand);
}
}
None => {
grid.insert(current_sand.clone(), PointType::Sand);
current_path.pop();
}
}
}
Some(count_sand(&grid))
}
pub fn part_two((mut grid, floor): Input) -> Option<usize> {
let floor = floor + 2;
let mut current_path = vec![SOURCE.clone()];
loop {
let current_sand = ¤t_path[current_path.len() - 1];
if let Some(next_sand) = next_pos(&grid, current_sand) {
if next_sand.y < floor {
current_path.push(next_sand);
continue;
}
}
grid.insert(current_sand.clone(), PointType::Sand);
if current_sand == &SOURCE {
break;
} else {
current_path.pop();
}
}
Some(count_sand(&grid))
}
advent_of_code::main!(14);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(parse(&advent_of_code::template::read_file("examples", 14)));
assert_eq!(result, Some(24));
}
#[test]
fn test_part_two() {
let result = part_two(parse(&advent_of_code::template::read_file("examples", 14)));
assert_eq!(result, Some(93));
}
}