|
| 1 | +use crate::util::grid::*; |
| 2 | +use crate::util::iter::*; |
| 3 | +use crate::util::parse::*; |
| 4 | +use crate::util::point::*; |
| 5 | +use std::collections::{BTreeMap, BTreeSet, VecDeque}; |
| 6 | + |
| 7 | +type Tile = [u64; 2]; |
| 8 | + |
| 9 | +pub fn parse(input: &str) -> Vec<Tile> { |
| 10 | + input.iter_unsigned::<u64>().chunk::<2>().collect() |
| 11 | +} |
| 12 | + |
| 13 | +pub fn part1(tiles: &[Tile]) -> u64 { |
| 14 | + let mut area = 0; |
| 15 | + |
| 16 | + for (i, &[x1, y1]) in tiles.iter().enumerate() { |
| 17 | + for &[x2, y2] in tiles.iter().skip(i + 1) { |
| 18 | + let dx = x1.abs_diff(x2) + 1; |
| 19 | + let dy = y1.abs_diff(y2) + 1; |
| 20 | + area = area.max(dx * dy); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + area |
| 25 | +} |
| 26 | + |
| 27 | +pub fn part2(tiles: &[Tile]) -> u64 { |
| 28 | + let mut xs: BTreeSet<_> = tiles.iter().map(|&[x, _]| x).collect(); |
| 29 | + xs.insert(0); |
| 30 | + xs.insert(u64::MAX); |
| 31 | + let shrink_x: BTreeMap<_, _> = xs.into_iter().enumerate().map(|(i, x)| (x, i as i32)).collect(); |
| 32 | + |
| 33 | + let mut ys: BTreeSet<_> = tiles.iter().map(|&[_, y]| y).collect(); |
| 34 | + ys.insert(0); |
| 35 | + ys.insert(u64::MAX); |
| 36 | + let shrink_y: BTreeMap<_, _> = ys.into_iter().enumerate().map(|(i, y)| (y, i as i32)).collect(); |
| 37 | + |
| 38 | + let mut grid = Grid::new(shrink_x.len() as i32, shrink_y.len() as i32, b'X'); |
| 39 | + |
| 40 | + for i in 0..tiles.len() { |
| 41 | + let [x1, y1] = tiles[i]; |
| 42 | + let [x2, y2] = tiles[(i + 1) % tiles.len()]; |
| 43 | + |
| 44 | + let x1 = shrink_x[&x1]; |
| 45 | + let x2 = shrink_x[&x2]; |
| 46 | + let y1 = shrink_y[&y1]; |
| 47 | + let y2 = shrink_y[&y2]; |
| 48 | + |
| 49 | + for x in x1.min(x2)..=x1.max(x2) { |
| 50 | + for y in y1.min(y2)..=y1.max(y2) { |
| 51 | + grid[Point::new(x, y)] = b'#'; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + let mut todo = VecDeque::new(); |
| 57 | + todo.push_back(ORIGIN); |
| 58 | + |
| 59 | + while let Some(point) = todo.pop_front() { |
| 60 | + for next in ORTHOGONAL.map(|o| point + o) { |
| 61 | + if grid.contains(next) && grid[next] == b'X' { |
| 62 | + grid[next] = b'.'; |
| 63 | + todo.push_back(next); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + let mut area = 0; |
| 69 | + |
| 70 | + for i in 0..tiles.len() { |
| 71 | + 'outer: for j in i + 1..tiles.len() { |
| 72 | + let [x1, y1] = tiles[i]; |
| 73 | + let [x2, y2] = tiles[j]; |
| 74 | + |
| 75 | + let x1 = shrink_x[&x1]; |
| 76 | + let x2 = shrink_x[&x2]; |
| 77 | + let y1 = shrink_y[&y1]; |
| 78 | + let y2 = shrink_y[&y2]; |
| 79 | + |
| 80 | + for x in x1.min(x2)..=x1.max(x2) { |
| 81 | + for y in y1.min(y2)..=y1.max(y2) { |
| 82 | + if grid[Point::new(x, y)] == b'.' { |
| 83 | + continue 'outer; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + let [x1, y1] = tiles[i]; |
| 89 | + let [x2, y2] = tiles[j]; |
| 90 | + let dx = x1.abs_diff(x2) + 1; |
| 91 | + let dy = y1.abs_diff(y2) + 1; |
| 92 | + area = area.max(dx * dy); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + area |
| 97 | +} |
0 commit comments