-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.rs
152 lines (132 loc) · 3.88 KB
/
day14.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
//! [Day 14: Regolith Reservoir](https://adventofcode.com/2022/day/14)
use std::collections::HashSet;
#[derive(Eq, Hash, PartialEq, Debug)]
struct Coord {
x: u32,
y: u32,
}
struct Puzzle {
wall: HashSet<Coord>,
floor: u32,
sand: usize,
}
impl Puzzle {
fn new() -> Self {
Self {
wall: HashSet::new(),
floor: 0,
sand: 0,
}
}
/// Loads data from input (one line)
fn configure(&mut self, path: &str) {
let data = std::fs::read_to_string(path).unwrap();
let lines = data.split('\n').collect::<Vec<_>>();
for line in lines {
if line.is_empty() {
continue;
}
let path = line
.split(" -> ")
.map(|p| {
let mut xy = p.split(',');
let x = xy.next().unwrap().parse::<u32>().unwrap();
let y = xy.next().unwrap().parse::<u32>().unwrap();
Coord { x, y }
})
.collect::<Vec<_>>();
for i in 0..(path.len() - 1) {
let p1 = &path[i];
let p2 = &path[i + 1];
if p1.x == p2.x {
let y1 = p1.y.min(p2.y);
let y2 = p1.y.max(p2.y);
for y in y1..=y2 {
self.wall.insert(Coord { x: p1.x, y });
}
} else if p1.y == p2.y {
let x1 = p1.x.min(p2.x);
let x2 = p1.x.max(p2.x);
for x in x1..=x2 {
self.wall.insert(Coord { x, y: p1.y });
}
} else {
panic!("{p1:?} {p2:?} diagonal");
}
}
}
self.floor = self.wall.iter().map(|p| p.y).max().unwrap() + 2;
}
fn fall(&mut self, part2: bool) -> bool {
let start = Coord { x: 500, y: 0 };
let mut x = start.x;
let mut y = start.y;
loop {
if y + 1 >= self.floor {
if part2 {
break;
} else {
// sand is beyond the lowest rock
return false;
}
}
if self.is_empty(x, y + 1) {
// fall vertically
y += 1;
} else if self.is_empty(x - 1, y + 1) {
// fall diagonally to the left
y += 1;
x -= 1;
} else if self.is_empty(x + 1, y + 1) {
// fall diagonally to the right
y += 1;
x += 1;
} else {
// sand is blocked
break;
}
}
self.wall.insert(Coord { x, y });
// if part1, always return true (sand cannot be at the starting point)
// if part2, return false if the sand is at the starting point
!(x == start.x && y == start.y)
}
fn is_empty(&self, x: u32, y: u32) -> bool {
!self.wall.contains(&Coord { x, y })
}
// Solves part one
fn part1(&mut self) -> usize {
for i in 0..10000 {
if !self.fall(false) {
self.sand = i;
break;
}
}
self.sand
}
// Solve part two
fn part2(&mut self) -> usize {
for i in 1..100_000 {
if !self.fall(true) {
self.sand += i;
break;
}
}
self.sand
}
}
/// main function
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
puzzle.configure(&args.path);
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
}
#[test]
fn test01() {
let mut puzzle = Puzzle::new();
puzzle.configure("test.txt");
assert_eq!(puzzle.part1(), 24);
assert_eq!(puzzle.part2(), 93);
}