day8 tests

This commit is contained in:
Stefan Bühler 2019-12-09 09:13:06 +01:00
parent e4e5d1c092
commit 5abf4a2916

View File

@ -1,10 +1,25 @@
fn merge_layers(layers: &[&[u8]]) -> Vec<u8> {
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::<Vec<_>>()
}
fn main() { fn main() {
let input = include_str!("input.txt").as_bytes(); let input = include_bytes!("input.txt");
let layers = split_layers(input, 6*25);
const LAYER_SIZE: usize = 6*25;
assert_eq!(input.len() % LAYER_SIZE, 0);
let layers = input.chunks(LAYER_SIZE).collect::<Vec<_>>();
let min0_product12 = layers.iter().map(|layer| { let min0_product12 = layers.iter().map(|layer| {
let mut count = [0, 0, 0]; let mut count = [0, 0, 0];
for b in *layer { for b in *layer {
@ -17,14 +32,7 @@ fn main() {
}).min_by_key(|(zeroes, _product12)| *zeroes).unwrap().1; }).min_by_key(|(zeroes, _product12)| *zeroes).unwrap().1;
println!("Product of counts of 1 and 2 for the layer with min 0s: {}", min0_product12); println!("Product of counts of 1 and 2 for the layer with min 0s: {}", min0_product12);
let mut image = [2u8; LAYER_SIZE]; let image = merge_layers(&layers);
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;
}
}
for row in image.chunks(25) { for row in image.chunks(25) {
for cell in row { for cell in row {
@ -36,3 +44,19 @@ fn main() {
println!(""); 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]);
}
}