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 std::collections::HashMap;
static DAY: u8 = 14;
fn main() {
let input = advent::read_lines(DAY);
println!("{DAY}a: {}", sand_before_abyss(&input));
println!("{DAY}b: {}", sand_to_rest(&input));
}
#[derive(Debug)]
enum Material {
Rock,
Sand,
}
fn parse_map(input: &[String]) -> HashMap<(isize,isize),Material> {
let mut map = HashMap::new();
for line in input {
let mut path = Vec::new();
for coord in line.split(" -> ") {
let (x,y) = coord.split_once(',').expect("coordinate needs to contain ,");
let x = x.parse::<isize>().expect("x coord should be number");
let y = y.parse::<isize>().expect("y coord should be number");
path.push((x,y));
}
for pos in path.windows(2) {
let (x1, x2) = if pos[0].0 < pos[1].0 {
(pos[0].0, pos[1].0)
} else {
(pos[1].0, pos[0].0)
};
let (y1, y2) = if pos[0].1 < pos[1].1 {
(pos[0].1, pos[1].1)
} else {
(pos[1].1, pos[0].1)
};
for x in x1 ..= x2 {
for y in y1 ..= y2 {
map.insert((x,y), Material::Rock);
}
}
}
}
map
}
fn next_sand_pos(map: &mut HashMap<(isize,isize),Material>, pos: (isize,isize)) -> Option<(isize,isize)> {
let next_positions = [
(pos.0, pos.1 + 1),
(pos.0 - 1, pos.1 + 1),
(pos.0 + 1, pos.1 + 1),
];
next_positions.iter().find(|next_pos| !map.contains_key(next_pos)).copied()
}
fn sand_above_abyss(map: &mut HashMap<(isize,isize),Material>, above: isize) -> bool {
let mut pos = (500,0);
while pos.1 <= above {
match next_sand_pos(map, pos) {
Some(next_pos) => pos = next_pos,
None => {
map.insert(pos, Material::Sand);
break;
}
}
}
pos.1 <= above
}
fn sand_at_source(map: &mut HashMap<(isize,isize),Material>, floor: isize) -> bool {
let mut pos = (500,0);
loop {
match next_sand_pos(map, pos) {
Some(next_pos) => {
if next_pos.1 == floor {
map.insert(pos, Material::Sand);
break;
}
pos = next_pos;
},
None => {
map.insert(pos, Material::Sand);
break;
}
}
}
pos == (500,0)
}
fn sand_before_abyss(input: &[String]) -> usize {
let mut map = parse_map(input);
let lowest_rock = *map.keys().max_by_key(|(_,y)| y).expect("map should have a rock");
let mut sand = 0;
while sand_above_abyss(&mut map, lowest_rock.1) {
sand += 1;
}
sand
}
fn sand_to_rest(input: &[String]) -> usize {
let mut map = parse_map(input);
let floor = map.keys().max_by_key(|(_,y)| y).expect("map should have a rock").1 + 2;
let mut sand = 0;
while !sand_at_source(&mut map, floor) {
sand += 1;
}
sand + 1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let input = [
"498,4 -> 498,6 -> 496,6",
"503,4 -> 502,4 -> 502,9 -> 494,9",
].iter().map(|&x| String::from(x)).collect::<Vec<_>>();
assert_eq!(sand_before_abyss(&input), 24);
assert_eq!(sand_to_rest(&input), 93);
}
}
|