day08 clean up with itertools' chunks

This commit is contained in:
Johannes
2019-12-08 10:26:20 +01:00
parent 67ab190265
commit 226ac1149b

View File

@@ -3,15 +3,14 @@ use itertools::Itertools;
#[allow(dead_code)] #[allow(dead_code)]
pub fn run() { pub fn run() {
let size = 25 * 6; let size = 25 * 6;
let raw: Vec<u8> = std::fs::read_to_string("input/day08.txt") let images: Vec<Vec<u8>> = std::fs::read_to_string("input/day08.txt")
.unwrap() .unwrap()
.chars() .chars()
.map(|c| c.to_digit(10).unwrap() as u8) .map(|c| c.to_digit(10).unwrap() as u8)
.chunks(size)
.into_iter()
.map(|chunk| chunk.collect())
.collect(); .collect();
let mut images = Vec::<Vec<u8>>::new();
for chunk in raw.chunks(size) {
images.push(chunk.iter().map(|it| *it).collect());
}
task1(&images); task1(&images);
task2(&images); task2(&images);
} }
@@ -31,16 +30,15 @@ fn task2(images: &Vec<Vec<u8>>) {
let result: Vec<u8> = (0..n_pixels) let result: Vec<u8> = (0..n_pixels)
.map(|pix| images.iter().find(|layer| layer[pix] != 2).unwrap()[pix]) .map(|pix| images.iter().find(|layer| layer[pix] != 2).unwrap()[pix])
.collect(); .collect();
for row in result.chunks(25) { for row in result.iter().chunks(25).into_iter() {
println!( println!(
"{}", "{}",
row.iter() row.map(|it| if *it == 0 {
.map(|it| if *it == 0 { format!(" ")
format!(" ") } else {
} else { format!("{}", it)
format!("{}", it) })
}) .join("")
.join("")
); );
} }
} }