-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.rs
128 lines (105 loc) · 2.88 KB
/
day3.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! [Day 3: Binary Diagnostic](https://adventofcode.com/2021/day/3)
/// main function
fn main() {
let data: Vec<_> = aoc::load_input_data_vec(3);
println!("{}", part1(&data));
println!("{}", part2(&data));
}
/// step 2
fn part2(data: &[String]) -> isize {
let nb_bits = data[0].len();
// println!("nb_bits: {}", nb_bits);
// oxygen generator rating
let mut dioxygen_rate = 0;
let mut dioxygen_start = String::new();
for bit in 0..nb_bits {
let mut one = 0;
let mut nb = 0;
for value in data {
if value.starts_with(&dioxygen_start) {
let c = value.chars().nth(bit).unwrap();
if c == '1' {
one += 1;
dioxygen_rate = isize::from_str_radix(value, 2).unwrap();
}
nb += 1;
}
}
if one >= nb - one {
dioxygen_start.push('1');
} else {
dioxygen_start.push('0');
}
}
// CO2 scrubber rating
let mut co2_rate = 0;
let mut co2_start = String::new();
for bit in 0..nb_bits {
let mut one = 0;
let mut nb = 0;
for value in data {
if value.starts_with(&co2_start) {
let c = value.chars().nth(bit).unwrap();
if c == '1' {
one += 1;
} else {
co2_rate = isize::from_str_radix(value, 2).unwrap();
}
nb += 1;
}
}
if one >= nb - one {
co2_start.push('0');
} else {
co2_start.push('1');
}
}
dioxygen_rate * co2_rate
}
/// step 1: compute `gamma_rate` * `espilon_rate`
fn part1(data: &[String]) -> i32 {
let mut gamma_rate = 0;
let mut freq_list: [i32; 12] = [0; 12];
let mut nb = 0;
let width = data.first().unwrap().len();
let mask = (1 << width) - 1;
for bits in data {
for (i, bit) in bits.chars().enumerate() {
assert!(i < width);
if bit == '1' {
freq_list[i] += 1_i32;
}
}
nb += 1;
}
for freq in freq_list.iter().take(width) {
gamma_rate *= 2;
if *freq >= nb / 2 {
gamma_rate += 1;
}
}
let espilon_rate = mask - gamma_rate;
gamma_rate * espilon_rate
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_part1() {
let data = load_data("test.txt".into());
assert_eq!(part1(&data), 198);
}
#[test]
fn test_part2() {
let data = load_data("test.txt".into());
assert_eq!(part2(&data), 230);
}
/// load data from file
fn load_data(filename: std::path::PathBuf) -> Vec<String> {
std::fs::read_to_string(filename)
.unwrap()
.lines()
.map(String::from)
.collect()
}
}