From 695f08f99f1af839ebb1a3eafba4af64de4ce184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Wed, 10 Dec 2025 23:52:19 +0100 Subject: [PATCH] Add solution for "Day 9: Movie Theater", part 1 --- README.md | 4 ++++ src/common/geometry.rs | 14 ++++++++++++ src/main.rs | 1 + src/solvers.rs | 1 + src/solvers/movie_theater.rs | 44 ++++++++++++++++++++++++++++++++++++ tests/examples.rs | 8 +++++++ tests/full_data.rs | 8 +++++++ 7 files changed, 80 insertions(+) create mode 100644 src/solvers/movie_theater.rs diff --git a/README.md b/README.md index f75d8f3..c193e50 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,10 @@ This project does not contain the puzzle or example inputs as per the [copyright :mag_right: Puzzle: , :white_check_mark: Solver: [`Playground`](src/solvers/playground.rs) +### Day 9: Movie Theater + +:mag_right: Puzzle: , :white_check_mark: Solver: [`MovieTheater`](src/solvers/movie_theater.rs) + ## Tests The package contains unit tests for each solver to help troubleshoot issues and prevent regressions. These tests cover the solutions for provided examples and full data inputs. The solutions used within the tests are user-specific. diff --git a/src/common/geometry.rs b/src/common/geometry.rs index f4dadcf..46a3c92 100644 --- a/src/common/geometry.rs +++ b/src/common/geometry.rs @@ -1,3 +1,17 @@ +#[derive(Hash, PartialEq, Eq, Debug)] +pub struct Point2(pub u64, pub u64); + +impl Point2 { + pub fn from_line(line: &str, separator: char) -> Self { + let p: Vec = line.split(separator).map(|s| s.parse().unwrap()).collect(); + Self(p[0], p[1]) + } + + pub fn rect_area(&self, other: &Self) -> u64 { + (self.0.abs_diff(other.0) + 1) * (self.1.abs_diff(other.1) + 1) + } +} + #[derive(Hash, PartialEq, Eq, Debug)] pub struct Point3(pub u64, pub u64, pub u64); diff --git a/src/main.rs b/src/main.rs index 170399e..4023c30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,4 +10,5 @@ fn main() { solvers::run(solvers::trash_compactor::TrashCompactor {}); // Laboratories solvers::run(solvers::playground::Playground::new(None)); + solvers::run(solvers::movie_theater::MovieTheater {}); } diff --git a/src/solvers.rs b/src/solvers.rs index 01918bd..df6d6db 100644 --- a/src/solvers.rs +++ b/src/solvers.rs @@ -6,6 +6,7 @@ use std::path; use std::path::Path; pub mod cafeteria; +pub mod movie_theater; pub mod playground; pub mod printing_department; pub mod trash_compactor; diff --git a/src/solvers/movie_theater.rs b/src/solvers/movie_theater.rs new file mode 100644 index 0000000..8f8df77 --- /dev/null +++ b/src/solvers/movie_theater.rs @@ -0,0 +1,44 @@ +use crate::common::geometry::Point2; +use crate::solvers::Solver; +use std::io::BufRead; + +pub struct MovieTheater {} + +impl MovieTheater { + const POINT_SPLIT_CHAR: char = ','; + + // Collects all red tile coordinates. + fn red_tiles(reader: R) -> Vec { + let mut red_tiles = Vec::new(); + for line in reader.lines().map_while(Result::ok) { + red_tiles.push(Point2::from_line(&line, Self::POINT_SPLIT_CHAR)); + } + red_tiles + } + + // Maps the closest pairs of junction boxes to their distances, up to the maximum number of + // connections. + fn largest_rectangle(red_tiles: &Vec) -> u64 { + let mut max = 0; + for i in 0..red_tiles.len() { + let (_, right) = red_tiles.split_at(i + 1); + for b in right { + let a = red_tiles[i].rect_area(b); + if max < a { + max = a; + } + } + } + max + } +} + +impl Solver for MovieTheater { + const PUZZLE_INDEX: u8 = 9; + const PUZZLE_NAME: &'static str = "Movie Theater"; + + fn process_data(&self, reader: R) -> (u64, u64) { + let red_tiles = Self::red_tiles(reader); + (Self::largest_rectangle(&red_tiles), 0) + } +} diff --git a/tests/examples.rs b/tests/examples.rs index 67f6116..8e1574f 100644 --- a/tests/examples.rs +++ b/tests/examples.rs @@ -45,3 +45,11 @@ fn playground() { ) ); } + +#[test] +fn movie_theater() { + assert_eq!( + Ok((50, 0)), + solvers::run_solver(solvers::movie_theater::MovieTheater {}, &EXAMPLE_PATHS) + ); +} \ No newline at end of file diff --git a/tests/full_data.rs b/tests/full_data.rs index 085f642..6f068ad 100644 --- a/tests/full_data.rs +++ b/tests/full_data.rs @@ -40,3 +40,11 @@ fn playground() { ) ); } + +#[test] +fn movie_theater() { + assert_eq!( + Ok((4781235324, 0)), + solvers::run_solver(solvers::movie_theater::MovieTheater {}, &solvers::DATA_PATHS) + ); +}