-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.rs
54 lines (45 loc) · 1.35 KB
/
03.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
#![feature(test, extract_if)]
type Input = Vec<String>;
fn setup(input: &str) -> Input {
input.lines().map(|s| s.to_string()).collect()
}
fn count(input: &Input, i: usize, chr: &char) -> usize {
input
.iter()
.filter(|s| s.chars().nth(i).unwrap() == *chr)
.count()
}
fn most_common(input: &Input, i: usize) -> char {
"01".chars().max_by_key(|c| count(input, i, c)).unwrap()
}
fn least_common(input: &Input, i: usize) -> char {
"01".chars().min_by_key(|c| count(input, i, c)).unwrap()
}
fn part1(input: &Input) -> String {
let mut most = 0;
let mut least = 0;
for i in 0..input[0].len() {
most = most << 1 | (most_common(input, i) as u32 - '0' as u32);
least = least << 1 | (least_common(input, i) as u32 - '0' as u32);
}
(most * least).to_string()
}
fn find(input: &Input, x: bool) -> isize {
let mut out = input.clone();
for i in 0..input[0].len() {
let mx = if x {
least_common(&out, i)
} else {
most_common(&out, i)
};
out.extract_if(|x| x.chars().nth(i).unwrap() != mx).count();
if out.len() == 1 {
return isize::from_str_radix(out[0].as_str(), 2).unwrap();
}
}
panic!();
}
fn part2(input: &Input) -> String {
(find(input, true) * find(input, false)).to_string()
}
aoc::main!(2021, 3, ex: 1);