From fde63d90906f465241a3839cd49c643083dcfd93 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Mon, 27 Dec 2021 17:28:55 +0200 Subject: [PATCH] feat: solve day 13 part 2 --- src/day13.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 2 files changed, 42 insertions(+) diff --git a/src/day13.rs b/src/day13.rs index 62ee48d..1f0d47e 100644 --- a/src/day13.rs +++ b/src/day13.rs @@ -83,6 +83,47 @@ pub fn part1(input: &InputData) -> usize { folded_dots.len() as usize } +fn determine_dot_bounds(dots: &HashSet) -> (u32, u32, u32, u32) { + let mut min_x = u32::MAX; + let mut min_y = u32::MAX; + let mut max_x = u32::MIN; + let mut max_y = u32::MIN; + + for dot in dots { + min_x = min_x.min(dot.0); + min_y = min_y.min(dot.1); + max_x = max_x.max(dot.0); + max_y = max_y.max(dot.1); + } + + return (min_x, min_y, max_x, max_y); +} + +fn render_dots(dots: &HashSet) { + let (min_x, min_y, max_x, max_y) = determine_dot_bounds(dots); + for y in min_y..=max_y { + for x in min_x..=max_x { + if dots.contains(&Dot(x, y)) { + print!("#"); + } else { + print!("."); + } + } + print!("\n"); + } +} + +pub fn part2(input: &InputData) { + let mut folded_dots = HashSet::new(); + for dot in &input.dots { + folded_dots.insert(dot.clone()); + } + for fold in &input.folds { + folded_dots = perform_fold(&folded_dots, fold); + } + render_dots(&folded_dots); +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index dc885c3..73e0257 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,6 +52,7 @@ fn run(day: i32, part: i32, input_filename: &str) { "12.1" => println!("{}", day12::part1(&day12::parse_input(&contents))), "12.2" => println!("{}", day12::part2(&day12::parse_input(&contents))), "13.1" => println!("{}", day13::part1(&day13::parse_input(&contents))), + "13.2" => day13::part2(&day13::parse_input(&contents)), _ => println!("Day {} part {} not found", day, part) } }