From 52fe8eb76f72992dcea759e3fa88f90f092c67b0 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Sun, 8 Dec 2019 16:23:51 +1100 Subject: [PATCH] Day 8 part 2 --- 2019/src/bin/day8.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/2019/src/bin/day8.rs b/2019/src/bin/day8.rs index 30dc87c..cfb8c4e 100644 --- a/2019/src/bin/day8.rs +++ b/2019/src/bin/day8.rs @@ -18,9 +18,32 @@ fn main() -> io::Result<()> { println!("Part 1: {}", ones * twos); + let picture = composite(&layers); + + for i in 0..(WIDTH * HEIGHT) { + if i % WIDTH == 0 { + println!(); + } + let pixel = if picture[i] == '1' { '█' } else { ' ' }; + print!("{}", pixel); + } + println!(); + Ok(()) } fn count_digits(layer: &[char], digit: char) -> usize { layer.iter().filter(|&&c| c == digit).count() } + +fn composite(layers: &Vec<&[char]>) -> Vec { + let mut image = Vec::with_capacity(WIDTH * HEIGHT); + // For each pixel go top to bottom finding the first non-transparent colour and add that to the + // final image. + for i in 0..(WIDTH * HEIGHT) { + let layer = layers.iter().find(|layer| layer[i] != '2').unwrap(); + image.push(layer[i]); + } + + image +}