fn merge_layers(layers: &[&[u8]]) -> Vec { let mut image = Vec::new(); image.resize(layers[0].len(), 2); for layer in layers { for (ndx, b) in layer.into_iter().enumerate() { let digit = *b - b'0'; if image[ndx] != 2 { continue; } image[ndx] = digit; } } image } fn split_layers(input: &[u8], image_size: usize) -> Vec<&[u8]> { assert_eq!(input.len() % image_size, 0); input.chunks(image_size).collect::>() } fn main() { let input = include_bytes!("input.txt"); let layers = split_layers(input, 6*25); let min0_product12 = layers.iter().map(|layer| { let mut count = [0, 0, 0]; for b in *layer { let digit = *b - b'0'; if digit < 3 { count[digit as usize] += 1; } } (count[0], count[1]*count[2]) }).min_by_key(|(zeroes, _product12)| *zeroes).unwrap().1; println!("Product of counts of 1 and 2 for the layer with min 0s: {}", min0_product12); let image = merge_layers(&layers); for row in image.chunks(25) { for cell in row { match cell { 1 => print!("#"), _ => print!(" "), } } println!(""); } } #[cfg(test)] mod day8test { use super::*; #[test] fn examples1() { // no runnable examples } #[test] fn examples2() { let layers = split_layers(b"0222112222120000", 4); assert_eq!(merge_layers(&layers), [0, 1, 1, 0]); } }