-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
84 lines (69 loc) · 2.2 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::io::{self, BufRead};
fn get_digit_count(report: &Vec<String>, digit_index: usize) -> i32 {
let mut count = 0;
for line in report {
count += match line.chars().nth(digit_index).unwrap() {
'0' => -1,
'1' => 1,
_ => 0,
};
}
count
}
fn filter_report(report: &Vec<String>, nth: usize, char: char) -> Vec<String> {
let filterd: Vec<String> = report
.iter()
.cloned()
.filter(|line| line.chars().nth(nth).unwrap() == char)
.collect();
filterd
}
fn main() {
let stdin = io::stdin();
let lines = stdin.lock().lines();
let mut report: Vec<String> = Vec::new();
for _line in lines {
report.push(_line.unwrap());
}
let line_len = report.first().unwrap().len();
let mut oxygen_generator_rating_report = report.clone();
for i in 0..line_len {
let digit_cnt = get_digit_count(&oxygen_generator_rating_report, i);
let filter_char = if digit_cnt == 0 {
'1'
} else if digit_cnt > 0 {
'1'
} else {
'0'
};
oxygen_generator_rating_report =
filter_report(&oxygen_generator_rating_report, i, filter_char);
if oxygen_generator_rating_report.len() < 2 {
break;
}
}
let mut co2_scrubber_rating_report = report.clone();
for i in 0..line_len {
let digit_cnt = get_digit_count(&co2_scrubber_rating_report, i);
let filter_char = if digit_cnt == 0 {
'0'
} else if digit_cnt > 0 {
'0'
} else {
'1'
};
co2_scrubber_rating_report = filter_report(&co2_scrubber_rating_report, i, filter_char);
if co2_scrubber_rating_report.len() < 2 {
break;
}
}
println!(
"{:?} {:?}",
oxygen_generator_rating_report, co2_scrubber_rating_report
);
let oxygen_generator_rating =
i32::from_str_radix(oxygen_generator_rating_report.first().unwrap(), 2).unwrap();
let co2_scrubber_rating =
i32::from_str_radix(co2_scrubber_rating_report.first().unwrap(), 2).unwrap();
println!("ans {}", oxygen_generator_rating * co2_scrubber_rating);
}