day08 clean up with itertools' chunks
This commit is contained in:
@@ -3,15 +3,14 @@ use itertools::Itertools;
|
||||
#[allow(dead_code)]
|
||||
pub fn run() {
|
||||
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()
|
||||
.chars()
|
||||
.map(|c| c.to_digit(10).unwrap() as u8)
|
||||
.chunks(size)
|
||||
.into_iter()
|
||||
.map(|chunk| chunk.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);
|
||||
task2(&images);
|
||||
}
|
||||
@@ -31,16 +30,15 @@ fn task2(images: &Vec<Vec<u8>>) {
|
||||
let result: Vec<u8> = (0..n_pixels)
|
||||
.map(|pix| images.iter().find(|layer| layer[pix] != 2).unwrap()[pix])
|
||||
.collect();
|
||||
for row in result.chunks(25) {
|
||||
for row in result.iter().chunks(25).into_iter() {
|
||||
println!(
|
||||
"{}",
|
||||
row.iter()
|
||||
.map(|it| if *it == 0 {
|
||||
format!(" ")
|
||||
} else {
|
||||
format!("{}", it)
|
||||
})
|
||||
.join("")
|
||||
row.map(|it| if *it == 0 {
|
||||
format!(" ")
|
||||
} else {
|
||||
format!("{}", it)
|
||||
})
|
||||
.join("")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user