-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
94 lines (81 loc) · 2.46 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
85
86
87
88
89
90
91
92
93
94
fn count_digits(numbers: &Vec<&str>, pos: usize) -> (usize, usize) {
let chars = numbers
.iter()
.map(|line| line[pos..pos + 1].to_string())
.collect::<Vec<_>>();
(
chars.iter().filter(|&c| c == "0").count(),
chars.iter().filter(|&c| c == "1").count(),
)
}
fn part1(input: &str) -> u32 {
let lines = input.split('\n').collect::<Vec<_>>();
let len = lines[0].len();
let digit_counts = (0..len)
.map(|i| count_digits(&lines, i))
.collect::<Vec<_>>();
let epsilon = u32::from_str_radix(
digit_counts
.iter()
.map(|(z, o)| if o > z { '1' } else { '0' })
.collect::<String>()
.as_str(),
2,
)
.unwrap();
let gamma = u32::from_str_radix(
digit_counts
.iter()
.map(|(z, o)| if o > z { '0' } else { '1' })
.collect::<String>()
.as_str(),
2,
)
.unwrap();
gamma * epsilon
}
fn part2(input: &str) -> u32 {
let lines = input.split('\n').collect::<Vec<_>>();
let len = lines[0].len();
let mut o2_gen_candidates = lines.clone();
for i in 0..len {
let (z, o) = count_digits(&o2_gen_candidates.clone(), i);
let ch = if z > o { "0" } else { "1" };
o2_gen_candidates = o2_gen_candidates
.iter()
.filter(|&c| c[i..i + 1] == *ch)
.map(|v| v.clone())
.collect::<Vec<&str>>();
if o2_gen_candidates.len() == 1 {
break;
}
}
let o2_gen_rating = u32::from_str_radix(o2_gen_candidates[0], 2).unwrap();
let mut co2_scrub_candidates = lines.clone();
for i in 0..len {
let (z, o) = count_digits(&co2_scrub_candidates.clone(), i);
let ch = if z > o { "1" } else { "0" };
co2_scrub_candidates = co2_scrub_candidates
.iter()
.filter(|&c| c[i..i + 1] == *ch)
.map(|v| v.clone())
.collect::<Vec<&str>>();
if co2_scrub_candidates.len() == 1 {
break;
}
}
let co2_scrub_rating = u32::from_str_radix(co2_scrub_candidates[0], 2).unwrap();
o2_gen_rating * co2_scrub_rating
}
fn main() {
println!("Part 1: {}", part1(include_str!("in.txt")));
println!("Part 2: {}", part2(include_str!("in.txt")));
}
#[test]
fn test_part1() {
assert_eq!(part1(include_str!("test.txt")), 198);
}
#[test]
fn test_part2() {
assert_eq!(part2(include_str!("test.txt")), 230);
}