Add solution for "Day 9: Movie Theater", part 1
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user