Add solution for "Day 9: Movie Theater", part 1
This commit is contained in:
@@ -26,6 +26,10 @@ This project does not contain the puzzle or example inputs as per the [copyright
|
||||
|
||||
:mag_right: Puzzle: <https://adventofcode.com/2025/day/8>, :white_check_mark: Solver: [`Playground`](src/solvers/playground.rs)
|
||||
|
||||
### Day 9: Movie Theater
|
||||
|
||||
:mag_right: Puzzle: <https://adventofcode.com/2025/day/9>, :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.
|
||||
|
||||
@@ -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<u64> = 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);
|
||||
|
||||
|
||||
@@ -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 {});
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<R: BufRead>(reader: R) -> Vec<Point2> {
|
||||
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<Point2>) -> 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<R: BufRead>(&self, reader: R) -> (u64, u64) {
|
||||
let red_tiles = Self::red_tiles(reader);
|
||||
(Self::largest_rectangle(&red_tiles), 0)
|
||||
}
|
||||
}
|
||||
@@ -45,3 +45,11 @@ fn playground() {
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn movie_theater() {
|
||||
assert_eq!(
|
||||
Ok((50, 0)),
|
||||
solvers::run_solver(solvers::movie_theater::MovieTheater {}, &EXAMPLE_PATHS)
|
||||
);
|
||||
}
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user