-
Notifications
You must be signed in to change notification settings - Fork 0
/
quest17.rs
67 lines (53 loc) · 1.58 KB
/
quest17.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
use crate::util::grid::*;
use crate::util::heap::*;
use crate::util::point::*;
use std::collections::HashSet;
pub fn part1(notes: &str) -> u64 {
size(parse(notes))
}
pub fn part2(notes: &str) -> u64 {
size(parse(notes))
}
pub fn part3(notes: &str) -> u64 {
let mut constellations: Vec<Vec<Point>> = Vec::new();
for star in parse(notes) {
let (near, far): (Vec<_>, Vec<_>) = constellations
.into_iter()
.partition(|c| c.iter().any(|&next| next.manhattan(star) < 6));
let mut merged = Vec::from([star]);
merged.extend(near.iter().flatten());
constellations = far;
constellations.push(merged);
}
let mut sizes: Vec<_> = constellations.into_iter().map(size).collect();
sizes.sort_unstable();
sizes.iter().rev().take(3).product()
}
fn parse(notes: &str) -> Vec<Point> {
let grid = Grid::parse(notes);
let mut stars = Vec::new();
for y in 0..grid.height {
for x in 0..grid.width {
let point = Point::new(x, y);
if grid[point] == b'*' {
stars.push(point);
}
}
}
stars
}
fn size(stars: Vec<Point>) -> u64 {
let mut heap = MinHeap::from([(0, stars[0])]);
let mut todo: HashSet<_> = stars.into_iter().collect();
let mut total = 0;
while !todo.is_empty() {
let (distance, next) = heap.pop().unwrap();
if todo.remove(&next) {
total += 1 + distance;
for &star in &todo {
heap.push(next.manhattan(star), star);
}
}
}
total as u64
}