|
| 1 | +use advent_of_code_data as aoc; |
| 2 | +use ube::{spatial::Point2, utils::pairwise_combinations}; |
| 3 | +use yuletide as yt; |
| 4 | + |
| 5 | +use linkme::distributed_slice; |
| 6 | + |
| 7 | +use crate::SOLVERS; |
| 8 | + |
| 9 | +#[distributed_slice(SOLVERS)] |
| 10 | +static SOLVER: yt::SolverAutoRegister = yt::SolverAutoRegister { |
| 11 | + modpath: std::module_path!(), |
| 12 | + part_one: yt::SolverPart { |
| 13 | + func: day_9_1, |
| 14 | + examples: &[yt::Example { |
| 15 | + input: "7,1\n11,1\n11,7\n9,7\n9,5\n2,5\n2,3\n7,3", |
| 16 | + expected: aoc::Answer::Int(50), |
| 17 | + }], |
| 18 | + }, |
| 19 | + part_two: yt::SolverPart { |
| 20 | + func: day_9_2, |
| 21 | + examples: &[/*yt::Example { |
| 22 | + input: "", |
| 23 | + expected: aoc::Answer::Int(0), |
| 24 | + }*/], |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +fn parse_tile_locations(input: &str) -> Vec<Point2> { |
| 29 | + input |
| 30 | + .lines() |
| 31 | + .map(|line| { |
| 32 | + let (x_str, y_str) = line.split_once(",").unwrap(); |
| 33 | + Point2 { |
| 34 | + x: x_str.parse().unwrap(), |
| 35 | + y: y_str.parse().unwrap(), |
| 36 | + } |
| 37 | + }) |
| 38 | + .collect::<Vec<_>>() |
| 39 | +} |
| 40 | + |
| 41 | +fn find_largest_rectangle(points: Vec<Point2>) -> usize { |
| 42 | + pairwise_combinations(&points) |
| 43 | + .map(|(a, b)| { |
| 44 | + let w = (b.x - a.x).abs() + 1; |
| 45 | + let h = (b.y - a.y).abs() + 1; |
| 46 | + (w * h) as usize |
| 47 | + }) |
| 48 | + .max() |
| 49 | + .unwrap() |
| 50 | +} |
| 51 | + |
| 52 | +pub fn day_9_1(args: &yt::SolverArgs) -> yt::Result<aoc::Answer> { |
| 53 | + Ok(find_largest_rectangle(parse_tile_locations(args.input)).into()) |
| 54 | +} |
| 55 | + |
| 56 | +pub fn day_9_2(_args: &yt::SolverArgs) -> yt::Result<aoc::Answer> { |
| 57 | + Err(yt::SolverError::NotFinished) |
| 58 | +} |
0 commit comments