mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 18:29:55 +00:00
Day 8 part 2
This commit is contained in:
parent
5336148b29
commit
52fe8eb76f
1 changed files with 23 additions and 0 deletions
|
@ -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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue