Day 8 part 2

This commit is contained in:
Wesley Moore 2019-12-08 16:23:51 +11:00
parent 5336148b29
commit 52fe8eb76f
No known key found for this signature in database
GPG key ID: BF67766C0BC2D0EE

View file

@ -18,9 +18,32 @@ fn main() -> io::Result<()> {
println!("Part 1: {}", ones * twos); 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(()) Ok(())
} }
fn count_digits(layer: &[char], digit: char) -> usize { fn count_digits(layer: &[char], digit: char) -> usize {
layer.iter().filter(|&&c| c == digit).count() layer.iter().filter(|&&c| c == digit).count()
} }
fn composite(layers: &Vec<&[char]>) -> Vec<char> {
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
}