|
| 1 | +//! [Day 9: Movie Theater](https://adventofcode.com/2025/day/9) |
| 2 | +
|
| 3 | +// Nota: not a good solution 😡 But it solves the puzzle... |
| 4 | + |
| 5 | +use geo::algorithm::contains::Contains; |
| 6 | +use geo::{Point, Polygon, Rect}; |
| 7 | +use itertools::Itertools; |
| 8 | + |
| 9 | +struct Puzzle { |
| 10 | + points: Vec<Point>, |
| 11 | +} |
| 12 | + |
| 13 | +impl Puzzle { |
| 14 | + /// Initialize from the puzzle input. |
| 15 | + fn new(data: &str) -> Self { |
| 16 | + Self { |
| 17 | + points: data |
| 18 | + .lines() |
| 19 | + .map(|line| { |
| 20 | + let (x, y) = line |
| 21 | + .split(',') |
| 22 | + .map(|x| x.parse::<f64>().unwrap()) |
| 23 | + .collect_tuple() |
| 24 | + .unwrap(); |
| 25 | + |
| 26 | + Point::new(x, y) |
| 27 | + }) |
| 28 | + .collect(), |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /// Solve part one. |
| 33 | + fn part1(&self) -> f64 { |
| 34 | + let mut max_area = 0.; |
| 35 | + |
| 36 | + for (i, p1) in self.points.iter().enumerate() { |
| 37 | + for (j, p2) in self.points.iter().enumerate() { |
| 38 | + if i > j { |
| 39 | + let xmin = p1.x().min(p2.x()); |
| 40 | + let xmax = p1.x().max(p2.x()); |
| 41 | + let ymin = p1.y().min(p2.y()); |
| 42 | + let ymax = p1.y().max(p2.y()); |
| 43 | + |
| 44 | + let area = (xmax + 1. - xmin) * (ymax + 1. - ymin); |
| 45 | + if area > max_area { |
| 46 | + max_area = area; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + max_area |
| 53 | + } |
| 54 | + |
| 55 | + /// Solve part two. |
| 56 | + fn part2(&self) -> f64 { |
| 57 | + let poly = Polygon::new(self.points.clone().into(), vec![]); |
| 58 | + |
| 59 | + let mut max_area = 0.; |
| 60 | + |
| 61 | + for (i, p1) in self.points.iter().enumerate() { |
| 62 | + for (j, p2) in self.points.iter().enumerate() { |
| 63 | + if i > j { |
| 64 | + let xmin = p1.x().min(p2.x()); |
| 65 | + let xmax = p1.x().max(p2.x()); |
| 66 | + let ymin = p1.y().min(p2.y()); |
| 67 | + let ymax = p1.y().max(p2.y()); |
| 68 | + |
| 69 | + let rect = Rect::new((xmin, ymin), (xmax, ymax)); |
| 70 | + |
| 71 | + if poly.contains(&rect) { |
| 72 | + let area = (xmax + 1. - xmin) * (ymax + 1. - ymin); |
| 73 | + if area > max_area { |
| 74 | + max_area = area; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + max_area |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// # Panics |
| 86 | +#[must_use] |
| 87 | +pub fn solve(data: &str) -> (f64, f64) { |
| 88 | + let puzzle = Puzzle::new(data); |
| 89 | + (puzzle.part1(), puzzle.part2()) |
| 90 | +} |
| 91 | + |
| 92 | +pub fn main() { |
| 93 | + let args = aoc::parse_args(); |
| 94 | + args.run(solve); |
| 95 | +} |
| 96 | + |
| 97 | +#[cfg(test)] |
| 98 | +mod test { |
| 99 | + use super::*; |
| 100 | + |
| 101 | + const TEST_INPUT: &str = include_str!("test.txt"); |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn part1() { |
| 105 | + let puzzle = Puzzle::new(TEST_INPUT); |
| 106 | + assert_eq!(puzzle.part1(), 50.); |
| 107 | + } |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn part2() { |
| 111 | + let puzzle = Puzzle::new(TEST_INPUT); |
| 112 | + assert_eq!(puzzle.part2(), 24.); |
| 113 | + } |
| 114 | +} |
0 commit comments