Files
2026-12-02 14:41:24 -07:00

616 lines
13 KiB
Rust

use aoc_runner_derive::{aoc, aoc_generator};
use grid::{AsCoord2d, Coord2d, Grid};
use indicatif::{ParallelProgressIterator, ProgressIterator, ProgressStyle};
use itertools::Itertools;
use rayon::prelude::*;
use std::collections::{HashMap, VecDeque};
use std::{
cmp::{max, min},
fmt::Display,
};
#[aoc_generator(day9)]
fn parse(input: &str) -> Vec<Coord2d> {
input
.lines()
.map(|l| l.parse::<Coord2d>().unwrap())
.map(|c| Coord2d {
x: c.x + 1, // allow a buffer zone
y: c.y + 1,
})
.collect()
}
#[aoc(day9, part1)]
fn part1(input: &[Coord2d]) -> u64 {
input
.iter()
.tuple_combinations()
// .inspect(|(a, b)| println!("{a} vs {b} = {}", a.x().abs_diff(b.x()) * a.y().abs_diff(b.y())))
.map(|(a, b)| (a.x().abs_diff(b.x()) + 1) * (a.y().abs_diff(b.y()) + 1))
.sorted_unstable()
.next_back()
.unwrap()
}
fn build_grid(reds: &Vec<Coord2d>) -> Grid<u8> {
let width = reds.iter().map(|c| c.x()).max().unwrap() + 3;
let height = reds.iter().map(|c| c.y()).max().unwrap() + 3;
let mut grid = Grid::with_shape(width as usize, height as usize, b'.');
let mut prev = reds.last().unwrap();
for c in reds {
// mark c filled
grid.set(&c, b'#');
// build a line of green between it and the previous
if c.x() == prev.x() {
// vertical
for y in (min(c.y(), prev.y()) + 1)..max(c.y(), prev.y()) {
grid.set(&(c.x(), y), b'X');
}
} else if c.y() == prev.y() {
// horiztonal
for x in (min(c.x(), prev.x()) + 1)..max(c.x(), prev.x()) {
grid.set(&(x, c.y()), b'X');
}
} else {
panic!()
}
prev = c
}
grid
}
#[allow(unused)]
fn build_hashgrid(reds: &Vec<Coord2d>) -> HashMap<Coord2d, u8> {
let mut grid = HashMap::new();
let mut prev = reds.last().unwrap();
for c in reds {
// mark c filled
grid.insert(*c, b'#');
// build a line of green between it and the previous
if c.x() == prev.x() {
// vertical
for y in (min(c.y(), prev.y()) + 1)..max(c.y(), prev.y()) {
grid.insert(Coord2d { x: c.x(), y }, b'X');
}
} else if c.y() == prev.y() {
// horiztonal
for x in (min(c.x(), prev.x()) + 1)..max(c.x(), prev.x()) {
grid.insert(Coord2d { x, y: c.y() }, b'X');
}
} else {
panic!()
}
prev = c
}
grid
}
#[allow(unused)]
fn flood_fill(grid: &mut Grid<u8>, pos: Coord2d) {
if grid.get(&pos) != Some(&b'.') {
return;
}
let mut q = VecDeque::new();
q.push_back(pos);
while let Some(next) = q.pop_front() {
let (mut lx, mut rx) = (next.x, next.x);
while grid.get(&Coord2d { x: lx - 1, y: next.y }) == Some(&b'.') {
grid.set(&Coord2d { x: lx - 1, y: next.y }, b'X');
lx -= 1;
}
while grid.get(&Coord2d { x: rx, y: next.y }) == Some(&b'.') {
grid.set(&Coord2d { x: rx, y: next.y }, b'X');
rx += 1;
}
flood_scan(grid, lx, rx, next.y + 1, &mut q);
flood_scan(grid, lx, rx, next.y - 1, &mut q);
}
}
#[allow(unused)]
fn flood_scan(grid: &mut Grid<u8>, lx: i64, rx: i64, y: i64, q: &mut VecDeque<Coord2d>) {
let mut span_added = false;
for x in lx..rx {
if grid.get(&Coord2d { x, y }) != Some(&b'.') {
span_added = false
} else if !span_added {
q.push_back(Coord2d { x, y });
span_added = true
}
}
}
#[allow(unused)]
// #[aoc(day9, part2, Brute)]
fn part2(reds: &Vec<Coord2d>) -> u64 {
let mut grid = build_grid(reds);
// Find the first red starting at top left - 'inside' must be to its bottom right
let start = grid.find(&b'#').unwrap();
flood_fill(&mut grid, start + &(1, 1).to_coord());
// println!("{grid}");
let pairs = reds
.iter()
.tuple_combinations()
.sorted_unstable_by_key(|(a, b)| (a.x().abs_diff(b.x()) + 1) * (a.y().abs_diff(b.y()) + 1))
.rev()
.progress()
.collect_vec();
let (a, b) = pairs
.par_iter()
.progress_with_style(
ProgressStyle::with_template(
"[{elapsed_precise}/{eta_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {per_sec}",
)
.unwrap(),
)
.filter(|(a, b)| (a.x().abs_diff(b.x()) + 1) * (a.y().abs_diff(b.y()) + 1) < 2972065369) // FIXME: PROGRESS CAPTURE
.map(|(a, b)| {
for y in (min(a.y(), b.y()))..=max(a.y(), b.y()) {
for x in (min(a.x(), b.x()))..=max(a.x(), b.y()) {
if *grid.get(&(x, y)).unwrap() == b'.' {
return (false, a, b);
}
}
}
(true, a, b)
})
.find_map_first(|(good, a, b)| if good { Some((a, b)) } else { None })
.unwrap();
// println!("win: {a} {b}");
(a.x().abs_diff(b.x()) + 1) * (a.y().abs_diff(b.y()) + 1)
}
#[allow(unused)]
fn line_square_intersection<LT, ST>(&(l1, l2): &(&LT, &LT), &(sq1, sq2): &(&ST, &ST)) -> bool
where
LT: Clone + Display + AsCoord2d,
ST: Clone + Display + AsCoord2d,
{
let min_x = min(sq1.x(), sq2.x());
let max_x = max(sq1.x(), sq2.x());
let min_y = min(sq1.y(), sq2.y());
let max_y = max(sq1.y(), sq2.y());
// println!("({},{}) X [{},{}]", l1, l2, sq1, sq2);
// line is horizontal
if l1.y() == l2.y() {
// println!(" horizontal at y={}", l1.y());
// above, below, or touching square
if l1.y() <= min_y || l1.y() >= max_y {
// println!(" y out of range or touching");
false
// start inside
} else if min(l1.x(), l2.x()) >= min_x && min(l1.x(), l2.x()) <= max_x {
// println!(" start inside");
true
//end inside
} else if max(l1.x(), l2.x()) >= min_x && max(l1.x(), l2.x()) <= max_x {
// println!(" end inside");
true
} else {
// println!(" no overlap on x");
false
}
// line is vertical
} else {
// println!(" vertical at x={}", l1.x());
if l1.x() <= min_x || l1.x() >= max_x {
// println!(" x out of range or touching");
false
// start inside
} else if min(l1.y(), l2.y()) >= min_y && min(l1.y(), l2.y()) <= max_y {
// println!(" start inside");
true
//end inside
} else if max(l1.y(), l2.y()) >= min_y && max(l1.y(), l2.y()) <= max_y {
// println!(" end inside");
true
} else {
// println!(" no overlap on y");
false
}
}
}
trait Rectangle {
fn top_left(&self) -> Coord2d;
fn bottom_right(&self) -> Coord2d;
#[allow(unused)]
fn in_bounds<T: AsCoord2d>(&self, b: &T) -> bool {
b.x() >= self.top_left().x
&& b.x() <= self.bottom_right().x()
&& b.y() >= self.top_left().y()
&& b.y() <= self.bottom_right().y()
}
fn area(&self) -> u64 {
let a = self.top_left();
let b = self.bottom_right();
(a.x().abs_diff(b.x()) + 1) * (a.y().abs_diff(b.y()) + 1)
}
}
impl Rectangle for (&Coord2d, &Coord2d) {
fn top_left(&self) -> Coord2d {
Coord2d {
x: min(self.0.x, self.1.x),
y: min(self.0.y, self.1.y),
}
}
fn bottom_right(&self) -> Coord2d {
Coord2d {
x: max(self.0.x, self.1.x),
y: max(self.0.y, self.1.y),
}
}
}
// true = clockwise
#[allow(unused)]
fn outside_points(prev: &Coord2d, cur: &Coord2d, next: &Coord2d) -> Vec<Coord2d> {
let l1_diff = ((cur.x - prev.x).signum(), (cur.y - prev.y).signum());
let l2_diff = ((next.x - cur.x).signum(), (next.y - cur.y).signum());
match (l1_diff, l2_diff) {
// CW
((1, 0), (0, 1)) => vec![cur + (0, -1), cur + (1, -1), cur + (1, 0)], // x^ y^
((0, 1), (-1, 0)) => vec![cur + (1, 0), cur + (1, 1), cur + (0, 1)],
((-1, 0), (0, -1)) => vec![cur + (0, 1), cur + (-1, 1), cur + (-1, 0)], // xv yv
((0, -1), (1, 0)) => vec![cur + (-1, 0), cur + (-1, -1), cur + (0, -1)], // yv x^
// CCW
((0, 1), (1, 0)) => vec![cur + (1, -1)],
((1, 0), (0, -1)) => vec![cur + (-1, -1)],
((0, -1), (-1, 0)) => vec![cur + (-1, 1)],
((-1, 0), (0, 1)) => vec![cur + (1, 1)],
((0, _), (0, _)) | ((_, 0), (_, 0)) => vec![], // colinear
_ => panic!(
"unexpected line arrangement {:?} {:?} @ [({},{}), ({},{}), ({},{})",
l1_diff, l2_diff, prev.x, prev.y, cur.x, cur.y, next.x, next.y
),
}
}
#[aoc(day9, part2, Lines)]
fn part2_lines(reds: &[Coord2d]) -> u64 {
// let mut grid = build_hashgrid(reds);
// let mut grid = build_grid(reds);
// // println!("{grid}");
// let area: i64 = reds
// .iter()
// .circular_tuple_windows()
// .map(|(a, b)| (a.x * b.y) - (b.x * a.y))
// .sum();
// let outside_points: HashMap<&Coord2d, Vec<Coord2d>> = if area < 0 {
// reds.iter()
// .rev()
// .circular_tuple_windows()
// .map(|(a, b, c)| (b, outside_points(a, b, c)))
// .collect()
// } else {
// reds.iter()
// .circular_tuple_windows()
// .map(|(a, b, c)| (b, outside_points(a, b, c)))
// .collect()
// };
// for corner in reds {
// for p in &outside_points[corner] {
// if *grid.get(p).unwrap() == b'.' {
// grid.set(p, b'O');
// }
// }
// }
// println!("{grid}");
// still iterate over the possible rectangles in size order
for rect in reds
.iter()
.tuple_combinations()
.sorted_unstable_by_key(|rect: &(&Coord2d, &Coord2d)| rect.area())
.rev()
.progress_with_style(
ProgressStyle::with_template(
"[{elapsed_precise}/{eta_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {per_sec}",
)
.unwrap(),
)
{
let tl = rect.top_left();
let br = rect.bottom_right();
let invalid = reds.iter().circular_tuple_windows().any(|(a, b)| {
// 1. A polygon vertex lies strictly inside the rectangle
let node_inside = tl.x < a.x && a.x < br.x && tl.y < a.y && a.y < br.y;
// 2. A polygon edge slices all the way across the rectangle
let crosses_bounds = if a.x == b.x {
let (ey_min, ey_max) = (a.y.min(b.y), a.y.max(b.y));
(tl.x < a.x && a.x < br.x) && (ey_min <= tl.y && ey_max >= br.y)
} else {
let (ex_min, ex_max) = (a.x.min(b.x), a.x.max(b.x));
(tl.y < a.y && a.y < br.y) && (ex_min <= tl.x && ex_max >= br.x)
};
node_inside || crosses_bounds
});
if invalid {
continue;
}
// println!(
// "win! {},{} = {}",
// rect.top_left(),
// rect.bottom_right(),
// rect.area()
// );
// grid.set(&rect.top_left(), b'*');
// grid.set(&rect.bottom_right(), b'*');
// println!("{grid}");
return rect.area();
}
panic!()
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
const EXAMPLE: &str = "7,1
11,1
11,7
9,7
9,5
2,5
2,3
7,3";
#[test]
fn part1_example() {
assert_eq!(part1(&parse(EXAMPLE)), 50);
}
#[test]
fn part2_example() {
assert_eq!(part2(&parse(EXAMPLE)), 24);
}
#[rstest]
#[case(
"7,1
11,1
11,7
9,7
9,5
2,5
2,3
7,3",
24
)]
#[case(
"4,2
13,2
13,4
8,4
8,6
11,6
11,10
4,10",
40
)]
#[case(
"3,2
17,2
17,13
13,13
13,11
15,11
15,8
11,8
11,15
18,15
18,17
4,17
4,12
6,12
6,5
3,5",
66
)]
#[case(
"2,2
8,2
8,6
5,6
5,4
2,4",
21
)]
#[case(
"3,1
12,1
12,4
9,4
9,8
3,8",
56
)]
#[case(
"7,1
11,1
11,3
13,3
13,5
11,5
11,7
9,7
9,5
2,5
2,3
7,3",
36
)]
#[ignore = "not required for aoc input"]
#[case(
"1,2
8,2
8,4
6,4
6,5
9,5
9,8
1,8",
56
)]
#[case(
"1,1
4,1
4,5
7,5
7,1
10,1
10,8
1,8",
32
)]
#[case(
"1,1
5,1
5,6
7,6
7,1
11,1
11,9
1,9",
45
)]
#[ignore = "not required for aoc input"]
#[case(
"1,1
5,1
5,7
6,7
6,1
10,1
10,9
1,9",
90
)]
#[case(
"1,1
12,1
12,8
10,8
10,3
8,3
8,8
6,8
6,3
4,3
4,8
1,8",
32
)]
#[case(
"1,1
1,8
10,8
10,1
7,1
7,5
4,5
4,1",
32
)]
#[case(
"4,1
7,1
7,4
10,4
10,7
7,7
7,10
4,10
4,7
1,7
1,4
4,4",
40
)]
#[ignore = "not required for aoc input"]
#[case(
"1,1
12,1
12,10
1,10
1,8
10,8
10,3
1,3",
36
)]
#[case(
"1,1
10,1
10,6
4,6
4,8
10,8
10,12
1,12
1,7
7,7
7,4
1,4",
50
)]
#[ignore = "not required for aoc input"]
#[case(
"1,1
10,1
10,10
3,10
3,4
7,4
7,7
5,7
5,6
6,6
6,5
4,5
4,9
9,9
9,2
1,2",
20
)]
#[ignore = "not required for aoc input"]
#[case(
"1,1
5,1
5,3
7,3
7,4
2,4
2,7
7,7
7,8
3,8
3,11
1,11",
16
)]
fn part2_lines_example(#[case] input: &str, #[case] answer: u64) {
assert_eq!(part2_lines(&parse(input)), answer);
}
}