-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
39 lines (33 loc) · 994 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
33
34
35
36
37
38
39
use std::io::{self, BufRead};
fn main() {
let stdin = io::stdin();
let lines = stdin.lock().lines();
let mut digit_cnt: Vec<i32> = Vec::new();
for _line in lines {
let line = _line.unwrap();
for (digit, i) in line.chars().rev().zip(0..) {
match digit_cnt.get(i) {
None => digit_cnt.push(0),
_ => (),
};
digit_cnt[i] += match digit {
'0' => -1,
'1' => 1,
_ => 0,
};
}
}
let gamma_str: String = digit_cnt
.iter()
.rev()
.map(|x| if x > &0 { '1' } else { '0' })
.collect();
let gamma = i32::from_str_radix(&gamma_str, 2).unwrap();
let epsilon_str: String = digit_cnt
.iter()
.rev()
.map(|x| if x < &0 { '1' } else { '0' })
.collect();
let epsilon = i32::from_str_radix(&epsilon_str, 2).unwrap();
println!("ans {}", gamma * epsilon);
}