Finish day03 rust rewrite

This commit is contained in:
Thom Dickson 2021-12-15 15:33:17 -05:00
parent d53b80fb4a
commit e71d4e85f4
Signed by: boots
GPG Key ID: 40BE2AF8EBF8D2BB
1 changed files with 22 additions and 0 deletions

View File

@ -6,6 +6,7 @@ fn main() {
println!("=== PART 1 ===");
part1(&data);
println!("=== PART 2 ===");
part2(&data);
}
fn ingest_data(input: String) -> Vec<String> {
@ -53,3 +54,24 @@ fn part1(data: &Vec<String>) {
println!("Epsilon: {}", epsilon_i);
println!("Power consumption: {}", gamma_i * epsilon_i);
}
fn part2(data: &Vec<String>) {
let mut data_a = data.clone();
let mut data_b = data.clone();
for bit in 0..data_a[0].len() {
let cb = common_bit(&data_a, bit, false);
data_a.retain(|x| x.chars().nth(bit).unwrap() == cb);
}
for bit in 0..data_b[0].len() {
let cb = common_bit(&data_b, bit, true);
data_b.retain(|x| x.chars().nth(bit).unwrap() == cb);
if data_b.len() == 1 {
break;
}
}
let oxygen = usize::from_str_radix(&data_a[0], 2).unwrap();
let co2 = usize::from_str_radix(&data_b[0], 2).unwrap();
println!("Oxygen: {}", oxygen);
println!("CO2: {}", co2);
println!("Life support rating: {}", oxygen * co2);
}