Skip to content

Commit

Permalink
Refactor and add panic if reduced readings ends up more than 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tjheslin1 committed Dec 3, 2021
1 parent aea0769 commit fae0046
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions 2021/day3/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
pub fn diagnostics_report(readings: &[&str]) -> (isize, isize) {
let most_common =
column_bits(readings)
.iter()
.fold((String::from("0"), String::from("0")), |acc, column| {
if most_common_bit(column, '0') == '0' {
(acc.0 + "0", acc.1 + "1")
} else {
(acc.0 + "1", acc.1 + "0")
}
});
let initialised_readings = (String::from("0"), String::from("0"));

let most_common = column_bits(readings)
.iter()
.fold(initialised_readings, |acc, column| {
if most_common_bit(column, '0') == '0' {
(acc.0 + "0", acc.1 + "1")
} else {
(acc.0 + "1", acc.1 + "0")
}
});

match most_common {
(b_gamma, b_epsilon) => {
Expand Down Expand Up @@ -55,6 +56,7 @@ fn find_by_bit_criteria(readings: &[&str], most_common: bool, on_match: char) ->

match reduced_readings[..] {
[binary] => binary_to_decimal(binary),
[_, _] => panic!("Unable to reduce down to a single binary value!"),
_ => 0,
}
}
Expand Down

0 comments on commit fae0046

Please sign in to comment.