|
| 1 | +use crate::util::grid::*; |
| 2 | +use crate::util::hash::*; |
| 3 | +use crate::util::iter::*; |
| 4 | +use crate::util::parse::*; |
| 5 | +use crate::util::point::*; |
| 6 | +use std::collections::VecDeque; |
| 7 | + |
| 8 | +const OUTSIDE: i64 = 0; |
| 9 | +const INSIDE: i64 = 1; |
| 10 | +const UNKNOWN: i64 = 2; |
| 11 | + |
| 12 | +type Tile = [u64; 2]; |
| 13 | + |
| 14 | +pub fn parse(input: &str) -> Vec<Tile> { |
| 15 | + input.iter_unsigned::<u64>().chunk::<2>().collect() |
| 16 | +} |
| 17 | + |
| 18 | +pub fn part1(tiles: &[Tile]) -> u64 { |
| 19 | + let mut area = 0; |
| 20 | + |
| 21 | + for (i, &[x1, y1]) in tiles.iter().enumerate() { |
| 22 | + for &[x2, y2] in tiles.iter().skip(i + 1) { |
| 23 | + let dx = x1.abs_diff(x2) + 1; |
| 24 | + let dy = y1.abs_diff(y2) + 1; |
| 25 | + area = area.max(dx * dy); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + area |
| 30 | +} |
| 31 | + |
| 32 | +pub fn part2(tiles: &[Tile]) -> u64 { |
| 33 | + let size = tiles.len(); |
| 34 | + let shrink_x = shrink(tiles, 0); |
| 35 | + let shrink_y = shrink(tiles, 1); |
| 36 | + let shrunk: Vec<_> = tiles.iter().map(|&[x, y]| (shrink_x[&x], shrink_y[&y])).collect(); |
| 37 | + |
| 38 | + let mut area = 0; |
| 39 | + let mut todo = VecDeque::from([ORIGIN]); |
| 40 | + let mut grid = Grid::new(shrink_x.len() as i32, shrink_y.len() as i32, UNKNOWN); |
| 41 | + |
| 42 | + for i in 0..size { |
| 43 | + let (x1, y1, x2, y2) = minmax(shrunk[i], shrunk[(i + 1) % size]); |
| 44 | + |
| 45 | + for x in x1..x2 + 1 { |
| 46 | + for y in y1..y2 + 1 { |
| 47 | + grid[Point::new(x, y)] = INSIDE; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + while let Some(point) = todo.pop_front() { |
| 53 | + for next in ORTHOGONAL.map(|o| point + o) { |
| 54 | + if grid.contains(next) && grid[next] == UNKNOWN { |
| 55 | + grid[next] = OUTSIDE; |
| 56 | + todo.push_back(next); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + for y in 1..grid.height { |
| 62 | + for x in 1..grid.width { |
| 63 | + let point = Point::new(x, y); |
| 64 | + let value = i64::from(grid[point] != OUTSIDE); |
| 65 | + grid[point] = value + grid[point + UP] + grid[point + LEFT] - grid[point + UP + LEFT]; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + for i in 0..size { |
| 70 | + for j in i + 1..size { |
| 71 | + let (x1, y1, x2, y2) = minmax(shrunk[i], shrunk[j]); |
| 72 | + |
| 73 | + let expected = (x2 - x1 + 1) as i64 * (y2 - y1 + 1) as i64; |
| 74 | + let actual = grid[Point::new(x2, y2)] |
| 75 | + - grid[Point::new(x1 - 1, y2)] |
| 76 | + - grid[Point::new(x2, y1 - 1)] |
| 77 | + + grid[Point::new(x1 - 1, y1 - 1)]; |
| 78 | + |
| 79 | + if expected == actual { |
| 80 | + let [x1, y1] = tiles[i]; |
| 81 | + let [x2, y2] = tiles[j]; |
| 82 | + let dx = x1.abs_diff(x2) + 1; |
| 83 | + let dy = y1.abs_diff(y2) + 1; |
| 84 | + area = area.max(dx * dy); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + area |
| 90 | +} |
| 91 | + |
| 92 | +fn shrink(tiles: &[Tile], index: usize) -> FastMap<u64, i32> { |
| 93 | + let mut ns: Vec<_> = tiles.iter().map(|tile| tile[index]).collect(); |
| 94 | + ns.push(0); |
| 95 | + ns.push(u64::MAX); |
| 96 | + ns.sort_unstable(); |
| 97 | + ns.dedup(); |
| 98 | + ns.iter().enumerate().map(|(i, &n)| (n, i as i32)).collect() |
| 99 | +} |
| 100 | + |
| 101 | +#[inline] |
| 102 | +fn minmax((x1, y1): (i32, i32), (x2, y2): (i32, i32)) -> (i32, i32, i32, i32) { |
| 103 | + (x1.min(x2), y1.min(y2), x1.max(x2), y1.max(y2)) |
| 104 | +} |
0 commit comments