1
0

feat: solve day 13 part 2

This commit is contained in:
Rokas Puzonas 2021-12-27 17:28:55 +02:00
parent 01ff1cddfe
commit fde63d9090
2 changed files with 42 additions and 0 deletions

View File

@ -83,6 +83,47 @@ pub fn part1(input: &InputData) -> usize {
folded_dots.len() as usize
}
fn determine_dot_bounds(dots: &HashSet<Dot>) -> (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<Dot>) {
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::*;

View File

@ -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)
}
}