Too tired to finish part 2 of day 3 of advent of code, but answer is not vaid yet
This commit is contained in:
@@ -68,7 +68,7 @@ fn main(){
|
|||||||
let mut pow = 1;
|
let mut pow = 1;
|
||||||
|
|
||||||
gamma_rate.reverse();
|
gamma_rate.reverse();
|
||||||
for b in gamma_rate {
|
for b in &gamma_rate {
|
||||||
match b {
|
match b {
|
||||||
true => gamma_rate_decimal += pow,
|
true => gamma_rate_decimal += pow,
|
||||||
false => (),
|
false => (),
|
||||||
@@ -79,4 +79,80 @@ fn main(){
|
|||||||
println!("Gamma rate in decimal is {}, epsilon rate in decimal is {}, yielding product of {}",
|
println!("Gamma rate in decimal is {}, epsilon rate in decimal is {}, yielding product of {}",
|
||||||
gamma_rate_decimal, pow - gamma_rate_decimal - 1,
|
gamma_rate_decimal, pow - gamma_rate_decimal - 1,
|
||||||
gamma_rate_decimal * (pow - gamma_rate_decimal - 1));
|
gamma_rate_decimal * (pow - gamma_rate_decimal - 1));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Now on to the second part of day 3
|
||||||
|
|
||||||
|
// Verify life support rating, by multiplying oxygen generator rating by co2 scrubber rating
|
||||||
|
// Oxygen generator rating is determined by finding the most common bit for the first bit
|
||||||
|
// Then, throw out all diagnostics line that do not have the same most common bit for the first position
|
||||||
|
// Repeat until only one line remains, this is the oxygen generator rating
|
||||||
|
// For the co2 scrubber rating, do the same but for the least common bit per position
|
||||||
|
|
||||||
|
gamma_rate.reverse();
|
||||||
|
let mut viable_oxygen = vec![true; line_numbers];
|
||||||
|
let mut viable_scrubber = vec![true; line_numbers];
|
||||||
|
|
||||||
|
let mut n_oxygen = line_numbers;
|
||||||
|
let mut n_scrubber = line_numbers;
|
||||||
|
|
||||||
|
for bit_n in 0 .. b_len {
|
||||||
|
for line_n in 0 .. line_numbers {
|
||||||
|
if viable_oxygen[line_n] && n_oxygen > 1 {
|
||||||
|
if diagnostics[line_n][bit_n] != gamma_rate[bit_n] {
|
||||||
|
n_oxygen -= 1;
|
||||||
|
viable_oxygen[line_n] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if viable_scrubber[line_n] && n_scrubber > 1 {
|
||||||
|
if diagnostics[line_n][bit_n] == gamma_rate[bit_n] {
|
||||||
|
n_scrubber -= 1;
|
||||||
|
viable_scrubber[line_n] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if n_oxygen == 1 && n_scrubber == 1 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut oxygen_ans = 0;
|
||||||
|
let mut scrubber_ans = 0;
|
||||||
|
|
||||||
|
for i in 0 .. line_numbers {
|
||||||
|
if viable_oxygen[i] {
|
||||||
|
oxygen_ans = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
if viable_scrubber[i] {
|
||||||
|
scrubber_ans = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diagnostics[oxygen_ans].reverse();
|
||||||
|
diagnostics[scrubber_ans].reverse();
|
||||||
|
|
||||||
|
let oxygen_dec = bit_to_decimal(&diagnostics[oxygen_ans]);
|
||||||
|
let scrubber_dec = bit_to_decimal(&diagnostics[scrubber_ans]);
|
||||||
|
|
||||||
|
println!("Oxygen generator rating is {}, co2 scrubber rating is {}, yields product of {}",
|
||||||
|
oxygen_dec, scrubber_dec, oxygen_dec * scrubber_dec);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bit_to_decimal(bits: &Vec::<bool>) -> u64 {
|
||||||
|
let mut pow = 1;
|
||||||
|
let mut result = 0;
|
||||||
|
|
||||||
|
for b in bits {
|
||||||
|
match b {
|
||||||
|
true => result += pow,
|
||||||
|
false => (),
|
||||||
|
}
|
||||||
|
pow *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user