|
1 |
| -#![allow(clippy::needless_range_loop)] |
2 |
| - |
3 | 1 | use crate::util::iter::*;
|
4 | 2 | use crate::util::parse::*;
|
5 |
| -use std::collections::VecDeque; |
6 | 3 |
|
7 |
| -pub struct Input { |
8 |
| - up: Vec<Vec<usize>>, |
9 |
| - down: Vec<Vec<usize>>, |
10 |
| -} |
| 4 | +type Input = (usize, usize); |
11 | 5 |
|
12 | 6 | pub fn parse(input: &str) -> Input {
|
13 | 7 | let mut bricks: Vec<_> = input.iter_unsigned::<usize>().chunk::<6>().collect();
|
14 |
| - let mut heights = [[0; 10]; 10]; |
15 |
| - let mut indices = [[usize::MAX; 10]; 10]; |
16 |
| - let mut up = vec![Vec::new(); bricks.len()]; |
17 |
| - let mut down = vec![Vec::new(); bricks.len()]; |
| 8 | + let mut heights = [0; 100]; |
| 9 | + let mut indices = [usize::MAX; 100]; |
| 10 | + |
| 11 | + let mut safe = vec![true; bricks.len()]; |
| 12 | + let mut dominator: Vec<(usize, usize)> = Vec::with_capacity(bricks.len()); |
18 | 13 |
|
19 | 14 | // Sort ascending by lowest z coordinate.
|
20 | 15 | bricks.sort_unstable_by_key(|b| b[2]);
|
21 | 16 |
|
22 | 17 | for (i, &[x1, y1, z1, x2, y2, z2]) in bricks.iter().enumerate() {
|
| 18 | + let start = 10 * y1 + x1; |
| 19 | + let end = 10 * y2 + x2; |
| 20 | + let step = if y2 > y1 { 10 } else { 1 }; |
23 | 21 | let height = z2 - z1 + 1;
|
| 22 | + |
24 | 23 | let mut top = 0;
|
25 | 24 | let mut previous = usize::MAX;
|
| 25 | + let mut underneath = 0; |
| 26 | + let mut parent = 0; |
| 27 | + let mut depth = 0; |
26 | 28 |
|
27 |
| - for x in x1..=x2 { |
28 |
| - for y in y1..=y2 { |
29 |
| - top = top.max(heights[x][y]); |
30 |
| - } |
| 29 | + for j in (start..=end).step_by(step) { |
| 30 | + top = top.max(heights[j]); |
31 | 31 | }
|
32 | 32 |
|
33 |
| - for x in x1..=x2 { |
34 |
| - for y in y1..=y2 { |
35 |
| - if heights[x][y] == top { |
36 |
| - let index = indices[x][y]; |
37 |
| - if index != previous { |
38 |
| - up[index].push(i); |
39 |
| - down[i].push(index); |
40 |
| - previous = index; |
| 33 | + for j in (start..=end).step_by(step) { |
| 34 | + if heights[j] == top { |
| 35 | + let index = indices[j]; |
| 36 | + if index != previous { |
| 37 | + previous = index; |
| 38 | + underneath += 1; |
| 39 | + |
| 40 | + if underneath == 1 { |
| 41 | + (parent, depth) = dominator[previous]; |
| 42 | + } else { |
| 43 | + // Find common ancestor |
| 44 | + let (mut a, mut b) = (parent, depth); |
| 45 | + let (mut x, mut y) = dominator[previous]; |
| 46 | + |
| 47 | + while b > y { |
| 48 | + (a, b) = dominator[a]; |
| 49 | + } |
| 50 | + while y > b { |
| 51 | + (x, y) = dominator[x]; |
| 52 | + } |
| 53 | + while a != x { |
| 54 | + (a, b) = dominator[a]; |
| 55 | + (x, _) = dominator[x]; |
| 56 | + } |
| 57 | + |
| 58 | + (parent, depth) = (a, b); |
41 | 59 | }
|
42 | 60 | }
|
43 |
| - |
44 |
| - heights[x][y] = top + height; |
45 |
| - indices[x][y] = i; |
46 | 61 | }
|
| 62 | + |
| 63 | + heights[j] = top + height; |
| 64 | + indices[j] = i; |
47 | 65 | }
|
| 66 | + |
| 67 | + if underneath == 1 { |
| 68 | + safe[previous] = false; |
| 69 | + parent = previous; |
| 70 | + depth = dominator[previous].1 + 1; |
| 71 | + } |
| 72 | + |
| 73 | + dominator.push((parent, depth)); |
48 | 74 | }
|
49 | 75 |
|
50 |
| - Input { up, down } |
| 76 | + let part_one = safe.iter().filter(|&&b| b).count(); |
| 77 | + let part_two = dominator.iter().map(|(_, d)| d).sum(); |
| 78 | + (part_one, part_two) |
51 | 79 | }
|
52 | 80 |
|
53 | 81 | pub fn part1(input: &Input) -> usize {
|
54 |
| - let Input { down, .. } = input; |
55 |
| - let mut safe = vec![true; down.len()]; |
56 |
| - |
57 |
| - for underneath in down { |
58 |
| - if underneath.len() == 1 { |
59 |
| - safe[underneath[0]] = false; |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| - safe.iter().filter(|&&b| b).count() |
| 82 | + input.0 |
64 | 83 | }
|
65 | 84 |
|
66 | 85 | pub fn part2(input: &Input) -> usize {
|
67 |
| - let Input { up, down } = input; |
68 |
| - let mut safe = vec![true; down.len()]; |
69 |
| - |
70 |
| - for underneath in down { |
71 |
| - if underneath.len() == 1 { |
72 |
| - safe[underneath[0]] = false; |
73 |
| - } |
74 |
| - } |
75 |
| - |
76 |
| - let mut result = 0; |
77 |
| - let mut todo = VecDeque::new(); |
78 |
| - let mut removed = vec![usize::MAX; down.len()]; |
79 |
| - |
80 |
| - for (start, &safe) in safe.iter().enumerate() { |
81 |
| - if safe { |
82 |
| - continue; |
83 |
| - } |
84 |
| - |
85 |
| - todo.push_back(start); |
86 |
| - removed[start] = start; |
87 |
| - |
88 |
| - while let Some(current) = todo.pop_front() { |
89 |
| - for &next in &up[current] { |
90 |
| - if removed[next] != start && down[next].iter().all(|&i| removed[i] == start) { |
91 |
| - result += 1; |
92 |
| - removed[next] = start; |
93 |
| - todo.push_back(next); |
94 |
| - } |
95 |
| - } |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - result |
| 86 | + input.1 |
100 | 87 | }
|
0 commit comments