-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.rs
32 lines (27 loc) · 863 Bytes
/
main.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
#![feature(drain_filter)]
const WIDTH: usize = 12;
pub fn main() {
let nums = include_str!("../input.txt")
.lines()
.map(|l| u32::from_str_radix(l, 2).unwrap())
.collect::<Vec<_>>();
let oxy = (0..WIDTH)
.rev()
.scan(nums.clone(), |oxy, i| {
let one = oxy.iter().filter(|n| *n & 1 << i > 0).count() >= (oxy.len() + 1) / 2;
oxy.drain_filter(|n| (*n & 1 << i > 0) != one);
oxy.first().copied()
})
.last()
.unwrap();
let co2 = (0..WIDTH)
.rev()
.scan(nums, |co2, i| {
let one = co2.iter().filter(|n| *n & 1 << i > 0).count() >= (co2.len() + 1) / 2;
co2.drain_filter(|n| (*n & 1 << i > 0) == one);
co2.first().copied()
})
.last()
.unwrap();
println!("{}", oxy * co2);
}