mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-11-09 09:22:33 +00:00
Day 8 part 1
This commit is contained in:
parent
92d9e9fa10
commit
5336148b29
2 changed files with 27 additions and 0 deletions
1
2019/input/day8.txt
Normal file
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
26
2019/src/bin/day8.rs
Normal 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()
|
||||
}
|
Loading…
Reference in a new issue