Day 8 part 1

This commit is contained in:
Wesley Moore 2019-12-08 16:13:40 +11:00
parent 92d9e9fa10
commit 5336148b29
No known key found for this signature in database
GPG key ID: BF67766C0BC2D0EE
2 changed files with 27 additions and 0 deletions

1
2019/input/day8.txt Normal file

File diff suppressed because one or more lines are too long

26
2019/src/bin/day8.rs Normal file
View file

@ -0,0 +1,26 @@
use std::{fs, io};
const WIDTH: usize = 25;
const HEIGHT: usize = 6;
fn main() -> io::Result<()> {
let data = fs::read_to_string("input/day8.txt")?;
let image = data.trim().chars().collect::<Vec<_>>();
let layers = image.chunks(WIDTH * HEIGHT).collect::<Vec<_>>();
let layer = layers
.iter()
.min_by_key(|layer| count_digits(layer, '0'))
.unwrap();
let ones = count_digits(layer, '1');
let twos = count_digits(layer, '2');
println!("Part 1: {}", ones * twos);
Ok(())
}
fn count_digits(layer: &[char], digit: char) -> usize {
layer.iter().filter(|&&c| c == digit).count()
}