-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathd8.rs
47 lines (39 loc) · 956 Bytes
/
d8.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
fn main() {
let nums: Vec<u32> = include_str!("./input.txt")
.chars()
.map(|i| i.to_digit(10).unwrap())
.collect();
const W: usize = 25;
const H: usize = 6;
const SIZE: usize = W * H;
// p1
let best = nums
.chunks(SIZE)
.min_by_key(|&layer| layer.iter().filter(|&x| *x == 0).count())
.unwrap();
let count_1s = best.iter().filter(|&x| *x == 1).count();
let count_2s = best.iter().filter(|&x| *x == 2).count();
println!("Part 1: {}", count_1s * count_2s);
// p2
let mut img = vec![2u32; SIZE];
nums.chunks(SIZE).rev().for_each(|c| {
c.iter().enumerate().for_each(|(i, x)| {
if *x != 2 {
img[i] = *x;
}
})
});
show(img.chunks_exact(W).collect());
}
fn show(chunks: Vec<&[u32]>) {
chunks.iter().for_each(|l| {
let line: String = l
.iter()
.map(|p| match p {
1 => '#',
_ => ' ',
})
.collect();
println!("{}", line);
})
}