From e71d4e85f41e711c4c65dc9f768be295e87ce6bd Mon Sep 17 00:00:00 2001 From: Thom Dickson Date: Wed, 15 Dec 2021 15:33:17 -0500 Subject: [PATCH] Finish day03 rust rewrite --- rust-rewrite/day03/src/main.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/rust-rewrite/day03/src/main.rs b/rust-rewrite/day03/src/main.rs index 8a1a7e9..1aaefd9 100644 --- a/rust-rewrite/day03/src/main.rs +++ b/rust-rewrite/day03/src/main.rs @@ -6,6 +6,7 @@ fn main() { println!("=== PART 1 ==="); part1(&data); println!("=== PART 2 ==="); + part2(&data); } fn ingest_data(input: String) -> Vec { @@ -53,3 +54,24 @@ fn part1(data: &Vec) { println!("Epsilon: {}", epsilon_i); println!("Power consumption: {}", gamma_i * epsilon_i); } + +fn part2(data: &Vec) { + 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); +}