generated from sindrekjr/AdventOfCodeBase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d14.rs
176 lines (146 loc) · 4.33 KB
/
d14.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::{collections::HashSet, iter};
use crate::core::{Part, Solution};
use super::coor::Position;
pub fn solve(part: Part, input: String) -> String {
match part {
Part::P1 => Day14::solve_part_one(input),
Part::P2 => Day14::solve_part_two(input),
}
}
impl Position<usize> {
fn from(pos_str: &str) -> Self {
let (x, y) = pos_str.split_once(',').unwrap();
Self {
x: x.parse::<usize>()
.expect(&format!("unable to parse x: {}", x)),
y: y.parse::<usize>()
.expect(&format!("unable to parse y: {}", y)),
}
}
fn path_to(&self, other: &Position<usize>) -> HashSet<Position<usize>> {
if self.x == other.x {
let x = self.x;
let range = if self.y < other.y {
self.y + 1..other.y + 1
} else {
other.y..self.y + 1
};
iter::once(self.clone())
.chain(range.map(|y| Position { x, y }))
.collect::<HashSet<Position<usize>>>()
} else {
let y = self.y;
let range = if self.x < other.x {
self.x + 1..other.x + 1
} else {
other.x..self.x + 1
};
iter::once(self.clone())
.chain(range.map(|x| Position { x, y }))
.collect::<HashSet<Position<usize>>>()
}
}
fn down(&self) -> Self {
Self {
x: self.x,
y: self.y + 1,
}
}
fn down_left(&self) -> Self {
Self {
x: self.x - 1,
y: self.y + 1,
}
}
fn down_right(&self) -> Self {
Self {
x: self.x + 1,
y: self.y + 1,
}
}
}
const ENTRY: Position<usize> = Position { x: 500, y: 0 };
struct Day14;
impl Solution for Day14 {
fn solve_part_one(input: String) -> String {
let (abyssal_perimeter, mut set) = parse_rocks(&input);
let mut sands = 0;
loop {
let mut finished = false;
let mut sand = ENTRY;
loop {
if sand.y >= abyssal_perimeter {
finished = true;
break;
}
if !set.contains(&sand.down()) {
sand = sand.down();
} else if !set.contains(&sand.down_left()) {
sand = sand.down_left();
} else if !set.contains(&sand.down_right()) {
sand = sand.down_right();
} else {
set.insert(sand);
sands += 1;
break;
}
}
if finished {
break;
}
}
sands.to_string()
}
fn solve_part_two(input: String) -> String {
let (lowest, mut set) = parse_rocks(&input);
let floor = lowest + 2;
let mut sands = 0;
loop {
let mut sand = ENTRY;
loop {
if sand.y + 1 == floor {
set.insert(sand);
sands += 1;
break;
}
if !set.contains(&sand.down()) {
sand = sand.down();
} else if !set.contains(&sand.down_left()) {
sand = sand.down_left();
} else if !set.contains(&sand.down_right()) {
sand = sand.down_right();
} else {
set.insert(sand);
sands += 1;
break;
}
}
if set.contains(&ENTRY) {
break;
}
}
sands.to_string()
}
}
fn parse_rocks(input: &str) -> (usize, HashSet<Position<usize>>) {
let mut lowest: usize = 0;
let mut set = HashSet::new();
for path in input.lines().map(|line| {
line.split(" -> ")
.map(Position::from)
.collect::<Vec<Position<usize>>>()
}) {
for pos in path.windows(2).map(|path| {
if path[0].y > lowest {
lowest = path[0].y;
}
if path[1].y > lowest {
lowest = path[1].y;
}
path[0].path_to(&path[1])
}) {
set.extend(pos);
}
}
(lowest, set)
}