diff --git a/2021/day03_binary_diagnostic/Cargo.toml b/2021/day03_binary_diagnostic/Cargo.toml new file mode 100644 index 0000000..46f91db --- /dev/null +++ b/2021/day03_binary_diagnostic/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day03_binary_diagnostic" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2021/day03_binary_diagnostic/challenge.txt b/2021/day03_binary_diagnostic/challenge.txt new file mode 100644 index 0000000..edcbff8 --- /dev/null +++ b/2021/day03_binary_diagnostic/challenge.txt @@ -0,0 +1,81 @@ +The submarine has been making some odd creaking noises, so you ask it to produce a diagnostic report just in case. + +The diagnostic report (your puzzle input) consists of a list of binary numbers which, when decoded properly, can tell you many useful things about the conditions of the submarine. The first parameter to check is the *power consumption*. + +You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the *gamma rate* and the *epsilon rate*). The power consumption can then be found by multiplying the gamma rate by the epsilon rate. + +Each bit in the gamma rate can be determined by finding the *most common bit in the corresponding position* of all numbers in the diagnostic report. For example, given the following diagnostic report: + +``` +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 + +``` + +Considering only the first bit of each number, there are five `0` bits and seven `1` bits. Since the most common bit is `1`, the first bit of the gamma rate is `1`. + +The most common second bit of the numbers in the diagnostic report is `0`, so the second bit of the gamma rate is `0`. + +The most common value of the third, fourth, and fifth bits are `1`, `1`, and `0`, respectively, and so the final three bits of the gamma rate are `110`. + +So, the gamma rate is the binary number `10110`, or `*22*` in decimal. + +The epsilon rate is calculated in a similar way; rather than use the most common bit, the least common bit from each position is used. So, the epsilon rate is `01001`, or `*9*` in decimal. Multiplying the gamma rate (`22`) by the epsilon rate (`9`) produces the power consumption, `*198*`. + +Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. *What is the power consumption of the submarine?* (Be sure to represent your answer in decimal, not binary.) + +Your puzzle answer was `2498354`. + +\--- Part Two --- +---------- + +Next, you should verify the *life support rating*, which can be determined by multiplying the *oxygen generator rating* by the *CO2 scrubber rating*. + +Both the oxygen generator rating and the CO2 scrubber rating are values that can be found in your diagnostic report - finding them is the tricky part. Both values are located using a similar process that involves filtering out values until only one remains. Before searching for either rating value, start with the full list of binary numbers from your diagnostic report and *consider just the first bit* of those numbers. Then: + +* Keep only numbers selected by the *bit criteria* for the type of rating value for which you are searching. Discard numbers which do not match the bit criteria. +* If you only have one number left, stop; this is the rating value for which you are searching. +* Otherwise, repeat the process, considering the next bit to the right. + +The *bit criteria* depends on which type of rating value you want to find: + +* To find *oxygen generator rating*, determine the *most common* value (`0` or `1`) in the current bit position, and keep only numbers with that bit in that position. If `0` and `1` are equally common, keep values with a `*1*` in the position being considered. +* To find *CO2 scrubber rating*, determine the *least common* value (`0` or `1`) in the current bit position, and keep only numbers with that bit in that position. If `0` and `1` are equally common, keep values with a `*0*` in the position being considered. + +For example, to determine the *oxygen generator rating* value using the same example diagnostic report from above: + +* Start with all 12 numbers and consider only the first bit of each number. There are more `1` bits (7) than `0` bits (5), so keep only the 7 numbers with a `1` in the first position: `11110`, `10110`, `10111`, `10101`, `11100`, `10000`, and `11001`. +* Then, consider the second bit of the 7 remaining numbers: there are more `0` bits (4) than `1` bits (3), so keep only the 4 numbers with a `0` in the second position: `10110`, `10111`, `10101`, and `10000`. +* In the third position, three of the four numbers have a `1`, so keep those three: `10110`, `10111`, and `10101`. +* In the fourth position, two of the three numbers have a `1`, so keep those two: `10110` and `10111`. +* In the fifth position, there are an equal number of `0` bits and `1` bits (one each). So, to find the *oxygen generator rating*, keep the number with a `1` in that position: `10111`. +* As there is only one number left, stop; the *oxygen generator rating* is `10111`, or `*23*` in decimal. + +Then, to determine the *CO2 scrubber rating* value from the same example above: + +* Start again with all 12 numbers and consider only the first bit of each number. There are fewer `0` bits (5) than `1` bits (7), so keep only the 5 numbers with a `0` in the first position: `00100`, `01111`, `00111`, `00010`, and `01010`. +* Then, consider the second bit of the 5 remaining numbers: there are fewer `1` bits (2) than `0` bits (3), so keep only the 2 numbers with a `1` in the second position: `01111` and `01010`. +* In the third position, there are an equal number of `0` bits and `1` bits (one each). So, to find the *CO2 scrubber rating*, keep the number with a `0` in that position: `01010`. +* As there is only one number left, stop; the *CO2 scrubber rating* is `01010`, or `*10*` in decimal. + +Finally, to find the life support rating, multiply the oxygen generator rating (`23`) by the CO2 scrubber rating (`10`) to get `*230*`. + +Use the binary numbers in your diagnostic report to calculate the oxygen generator rating and CO2 scrubber rating, then multiply them together. *What is the life support rating of the submarine?* (Be sure to represent your answer in decimal, not binary.) + +Your puzzle answer was `3277956`. + +Both parts of this puzzle are complete! They provide two gold stars: \*\* + +At this point, you should [return to your Advent calendar](/2021) and try another puzzle. + +If you still want to see it, you can [get your puzzle input](3/input). \ No newline at end of file diff --git a/2021/day03_binary_diagnostic/src/lib.rs b/2021/day03_binary_diagnostic/src/lib.rs new file mode 100644 index 0000000..f574391 --- /dev/null +++ b/2021/day03_binary_diagnostic/src/lib.rs @@ -0,0 +1,83 @@ +use core::fmt::Display; + +#[derive(Debug, PartialEq, Eq)] +pub enum ParseError { + ParseIntError(char), +} + +impl Display for ParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::ParseIntError(e) => write!(f, "Unable to parse into integer {e}"), + } + } +} + +pub fn run(input: &str) -> Result<(usize, usize), ParseError> { + let bits: Vec<_> = input.lines().map(|line| line.chars().map(|i| i.to_digit(10).ok_or(ParseError::ParseIntError(i))).collect::, _>>()).collect::, _>>()?; + let mut ones = vec![0; bits[0].len()]; + bits.iter().for_each(|number| number.iter().enumerate().for_each(|(idx, bit)| ones[idx] += bit)); + let half = bits.len() as u32/2; + let mut gamma = 0; + let mut epsilon = 0; + ones.iter().for_each(|bit_count| { + gamma *= 2; + epsilon *= 2; + if bit_count > &half { + gamma += 1; + } else { + epsilon += 1; + } + }); + let first = gamma as usize * epsilon as usize; + + let bit_count = bits.len(); + let mut oxygen_rating = bits.clone(); + for idx in 0..bit_count { + if oxygen_rating.len() > 1 { + if oxygen_rating.iter().map(|bits| bits[idx]).sum::() >= (oxygen_rating.len() as u32 + 1) / 2 { + oxygen_rating.retain(|bits| bits[idx] == 1); + } else { + oxygen_rating.retain(|bits| bits[idx] == 0); + } + } + } + let oxygen_rating = oxygen_rating[0].iter().fold(0, |acc, bit| 2 * acc + *bit); + + let mut co2_rating = bits.clone(); + for idx in 0..bit_count { + if co2_rating.len() > 1 { + if co2_rating.iter().map(|bits| bits[idx]).sum::() >= (co2_rating.len() as u32 + 1) / 2 { + co2_rating.retain(|bits| bits[idx] == 0); + } else { + co2_rating.retain(|bits| bits[idx] == 1); + } + } + } + let co2_rating = co2_rating[0].iter().fold(0, |acc, bit| 2 * acc + *bit); + + let second = oxygen_rating as usize * co2_rating as usize; + Ok((first, second)) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::read_to_string; + + fn read_file(name: &str) -> String { + read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string() + } + + #[test] + fn test_sample() { + let sample_input = read_file("tests/sample_input"); + assert_eq!(run(&sample_input), Ok((198, 230))); + } + + #[test] + fn test_challenge() { + let challenge_input = read_file("tests/challenge_input"); + assert_eq!(run(&challenge_input), Ok((2498354, 3277956))); + } +} diff --git a/2021/day03_binary_diagnostic/tests/challenge_input b/2021/day03_binary_diagnostic/tests/challenge_input new file mode 100644 index 0000000..d85f4c9 --- /dev/null +++ b/2021/day03_binary_diagnostic/tests/challenge_input @@ -0,0 +1,1000 @@ +101010011010 +101111111001 +100100100000 +000001010010 +100100110011 +000101100110 +101011001001 +100001111101 +011001001101 +010000110101 +001111101111 +010110111000 +000010001100 +011101111011 +111111100111 +010011101011 +010110001000 +001111001010 +000110101011 +011001110101 +001110001111 +101100001101 +010111110010 +110000000000 +001101110110 +111111101011 +110011110110 +100011110110 +010111000001 +111010100110 +100001011101 +110010110110 +111101010111 +000011110001 +110101010000 +111101101000 +000001111110 +100101000011 +100101001010 +111001001011 +010110011110 +111111010111 +111000000111 +101000101100 +000000101100 +111010001010 +000000001110 +011001111101 +000000111010 +010001000001 +110101100000 +101010100001 +100011011010 +110101000010 +100110011100 +110100110110 +010000011101 +111111101000 +001000110111 +000000001001 +111000001011 +101000000111 +011010010001 +000100001011 +100100101100 +100010101110 +111000011100 +011100000001 +010111101110 +101000010000 +100101110100 +101110000101 +100001111001 +110101001101 +110100111001 +011001100100 +111101110010 +010010111010 +011100110011 +100001100011 +100001111011 +010011001010 +011000001110 +000010101000 +011111001010 +101100000011 +010011011010 +000101110100 +101111110011 +011110011011 +000101101001 +110101111000 +100001100001 +001000110000 +100101011001 +001010100000 +110001110110 +000111010000 +000001111010 +100101010000 +101000001111 +111100011011 +001010001011 +010100110101 +011110011110 +101100101111 +011110110111 +000100011110 +111101100000 +100001111110 +101000100110 +101001000111 +001100001010 +110001011011 +010001010110 +001010110110 +010101100011 +100001001000 +011111000100 +010001011111 +101100111001 +100101101110 +010111111100 +011001011100 +000110010000 +101010110010 +010001111110 +001011100000 +011110101000 +110000110101 +101101000100 +101101111111 +111010111110 +000100110000 +101111001110 +110111100101 +100011011100 +111010011010 +101010111110 +001000011100 +000110110100 +000110110111 +100110101111 +101101010001 +011011000010 +110011010011 +101010101001 +010000001000 +011111100000 +110111111111 +011010110101 +101101011001 +101001011001 +100010010101 +101100100101 +010100100111 +111100100100 +001011001110 +010111011100 +110100010111 +000110001111 +010010110011 +101110111101 +011100001101 +101100000001 +110111000111 +100100100111 +001100011011 +100110101100 +110110101011 +101101100011 +101110111110 +111101010001 +011000100100 +000110001100 +111000111101 +001011101011 +101111001111 +000010010100 +111101001100 +110000011010 +001011111000 +001000101111 +000000111110 +000111110111 +000100101011 +001011011110 +011000100101 +111010101111 +010100001110 +101101011101 +000101000100 +110010100001 +001001011110 +001011010000 +110111101101 +010011110001 +111000111011 +100101011110 +111100100110 +010100000000 +100100011001 +000001001111 +101111111011 +101100010101 +111100011111 +111010110101 +100010010011 +101011011000 +101000011100 +010100100010 +101001111100 +010000110000 +111100010111 +011000101000 +110100110100 +110001010110 +110110110010 +111100010010 +000010101100 +011110101111 +100011000001 +000000110101 +111111100000 +110110010011 +000000010111 +011001001010 +001011001010 +110110110001 +110110110011 +010010000101 +101001001001 +001010000001 +100010001101 +011101101101 +100000010110 +001100110110 +000110110110 +110001110100 +111100000101 +110101101100 +100100101101 +101111001100 +100010011001 +000011111001 +010110101101 +111111110101 +011101010101 +011011010001 +111101110000 +010101011110 +011000111000 +101111101101 +111011110111 +111111001101 +101110101111 +101000001101 +101111101000 +111101110001 +011001100101 +101000011010 +111011100001 +011000001111 +001110000000 +001010001110 +100010001000 +001011100111 +101000000100 +101011101000 +000101111111 +111010010101 +000100000001 +000011010100 +010101111010 +110110011001 +001101001011 +001101111111 +111010000110 +001100011010 +000011111010 +000100100010 +100100001110 +101001011111 +101110001100 +000000011000 +100110010101 +111100110101 +000000011010 +000101110010 +000101101000 +100100101110 +011011111010 +010111111001 +010011001110 +101010010111 +101010001000 +101010100111 +011100000110 +101101010011 +010000001110 +100100110100 +111111100010 +111111000100 +000100111010 +101011110110 +011111001100 +011101001110 +011100111011 +110011001011 +111011111101 +111011101100 +100000100100 +010001000111 +111000001010 +001111110000 +010001010010 +000110110000 +100000100110 +001010000110 +010011011100 +111000010011 +110101001110 +101010101110 +111001001100 +111001111011 +110100011110 +110011011001 +010110010111 +001111111101 +110010000011 +110100111010 +110101110000 +000101001111 +010000010011 +101011100101 +011001010010 +100110011001 +111001101001 +010100100001 +000111011100 +000011110110 +111010101101 +111110000111 +010100101011 +011100011110 +100000011010 +111111011011 +100111110011 +001111011100 +001011000101 +010010011111 +101101100101 +100110110100 +011100010000 +011101000100 +000100101101 +101100101000 +110111111110 +110100000111 +101010001100 +100111110101 +100000010010 +010110011111 +010101100111 +010101100001 +001101011011 +010101001100 +000010110010 +111001011010 +001110111110 +000100001110 +111001110001 +111100111110 +001111101101 +000111100110 +101001100001 +101000001110 +100010101100 +101101001110 +001110011101 +000111110100 +100110100000 +101001010010 +111110011111 +101101100110 +101000000110 +101000100011 +001010101011 +001110001101 +100011010010 +101111111100 +010100111000 +100110100100 +001011110001 +000011111111 +111110110101 +100011110100 +101111010111 +001100000111 +001101111000 +000011001000 +011000101001 +111101011011 +100111010010 +111101010011 +001010010110 +010111011101 +111000110011 +000110111010 +101011001100 +111011100110 +001111000010 +111111101100 +110000000010 +001010010001 +011111110110 +010110101110 +110001010011 +111000110101 +110101110001 +101101011011 +011111110001 +010010010110 +010010100111 +111001100101 +011100001011 +100001010100 +000010110101 +101010100100 +010110100010 +001000100110 +011010000100 +011111001000 +100000000001 +011010011000 +101101001011 +100011101001 +011010000010 +000011110101 +001101100110 +001000101010 +101100010110 +111010000001 +110110100011 +000100010100 +100110100111 +011011010011 +011001110110 +000000001111 +000000001000 +011100100110 +000000001100 +111010011101 +110011010100 +011100010011 +011110001011 +101110000000 +101110001010 +101001001000 +100000000111 +101011100110 +101110110100 +001001110011 +110110001100 +001100101001 +001110000011 +010111010111 +101101000011 +000000011101 +001010011010 +000001010100 +011010001100 +100001001101 +010100010000 +101111011110 +001110110001 +111111000001 +011111010110 +110100000000 +010110100100 +111001010110 +111010100101 +100100011011 +110000100011 +000001011001 +110010110011 +100010100100 +011000010111 +010101110000 +111110100101 +100010000000 +101011000000 +010100110110 +100011011011 +000010011000 +110000001000 +101101001100 +100111000001 +010000111011 +111000000011 +010010100101 +100100101010 +000011110111 +110111101010 +000000010110 +100111001011 +101000011001 +010011100101 +100111110100 +001111001001 +000111000101 +000010010001 +010101001101 +111010010001 +000011110000 +100001101000 +111111110100 +010101011001 +011110100101 +001010100111 +100001001111 +001011010111 +111111000101 +111000001110 +111000111100 +110111111101 +011101111110 +111000011011 +010111110101 +001111101001 +011100001010 +000100101100 +100111101010 +010011011111 +110010110101 +111111010110 +100011010111 +100101111001 +010100010010 +110010000001 +100111011101 +111001100111 +000001110001 +010110101111 +000111010101 +100001101001 +010000010000 +001110101101 +111101001111 +010011101010 +111110001011 +110101000110 +001001001011 +001100001011 +100010110000 +010100010011 +111000110111 +100010100001 +110000010000 +000001111001 +000100110110 +101110001101 +111011101010 +110111100111 +011101011000 +011110111010 +001001001110 +101001011000 +001010011110 +011011011101 +010100010110 +011010010111 +000110001010 +111111111101 +100110011111 +011100011011 +010001101111 +100000100111 +100011110101 +111011101110 +101001111010 +110001010111 +000010000101 +001000000000 +011000001101 +110011000011 +001000001001 +110111001010 +101110110001 +111100010101 +111110100011 +010111101000 +000011001001 +001110000110 +111001111110 +111110100100 +010000100111 +111110101101 +101001110111 +011001110111 +100000111101 +100110111111 +111001110011 +100011110000 +110001011100 +011100001001 +110101011001 +101111001001 +110111000110 +111110100001 +110011110011 +111000101111 +101111010110 +110101010111 +010010010101 +101000011111 +100010001110 +000010111101 +001001100001 +010111010000 +100011010100 +011110101101 +110001001110 +101000001001 +010000010001 +110110000011 +011011010010 +111001100011 +111000101000 +010000001010 +111110111001 +111010111011 +100001010111 +010111100110 +101001011100 +111101010000 +110110100000 +111100110000 +011101101100 +101101101110 +010001111001 +000010011001 +010000000101 +100000111011 +011100110001 +110110100101 +001100101101 +110010100100 +010101100000 +100000101010 +100011110010 +110111001101 +101101011000 +111101001000 +010101110101 +010111110111 +000110001101 +110011101100 +000100100011 +101111111111 +001001000000 +000001101100 +000110110101 +010010110001 +011101001011 +111000011110 +011111101100 +011001101010 +101101010100 +110000110110 +100000111110 +110101000000 +000110011000 +111010000100 +110101100111 +110110001101 +101001100011 +011101110100 +011010110110 +100110101000 +110000110000 +001101011000 +110101011011 +011011111001 +001010101100 +101010000001 +000111000000 +001011100011 +000001011101 +011010011110 +001100001110 +001101000100 +010110001101 +111011100000 +011001110001 +001001110101 +111100000111 +101000110011 +010011010000 +101101011010 +010101010011 +100110010110 +101101111101 +011010111000 +010001000000 +001000010011 +111101101101 +110111010001 +110101011110 +001111110001 +000110101110 +000001100000 +100110011011 +101110111011 +100111001100 +101100010001 +011011110100 +010100011100 +010010011100 +101010110111 +000111110011 +101010001111 +010001101011 +101110101101 +111000000000 +000000010000 +010101111001 +111001010111 +000101000001 +010110010100 +111111100101 +101100110101 +110011000100 +111010010011 +010100001000 +110011001001 +111101000010 +000010001001 +010111101111 +000110001110 +100000001011 +001110111000 +110001101011 +111100011101 +111100100101 +010011111010 +100100001100 +100000011111 +010000001101 +111001010100 +000101110000 +111010110110 +100110110000 +101100000111 +111010010010 +110000110100 +011101011101 +010001000101 +110000000011 +110110110101 +100110000010 +010001010001 +101001111000 +111111001010 +000000110011 +010110100111 +111110000101 +000100011011 +000101010101 +010100111011 +101010110000 +101111000011 +011001011101 +111101111010 +000000001011 +000110110001 +001001110010 +001000100010 +000001110111 +000110000101 +111100101011 +100111000100 +010011000000 +001110110010 +110010111010 +011111111110 +110101010100 +011101101110 +101111010010 +111100100011 +010011001111 +011101010110 +011111101111 +011000010000 +001001010110 +101000010011 +000111011111 +100110000001 +111001111010 +001001111101 +101000011011 +001111111000 +001111110011 +110111011011 +010100100110 +001110101001 +011100111101 +111111101010 +101100011010 +001010110111 +000000100011 +001110100011 +010110001001 +111001011001 +011010101101 +110100111110 +011111000001 +110110011111 +110100000010 +010000110111 +001100101000 +101000011101 +010100111101 +111101000000 +001001000110 +001111011010 +111010000011 +000110101101 +010000010111 +001101101101 +110100000101 +000010110001 +011000001000 +010011100000 +010100110100 +101000100101 +000000010101 +111000000001 +101001111110 +101101000000 +000000001010 +001111010100 +001110000010 +110110011100 +000111000010 +011011100001 +010011100100 +110101001001 +000001011110 +101001011101 +100111001001 +011000000011 +110110111000 +100101111000 +010101110111 +100010000101 +010011001011 +011111100010 +000000011011 +111000100001 +000011000110 +111101001101 +100011001111 +100111010100 +010011100010 +100100111111 +110001001010 +100100111010 +111001101101 +000100110001 +101111101100 +001110010101 +000010010000 +011100010101 +100100001011 +000001110011 +110111001000 +001001100110 +001100111111 +100000100101 +000110111110 +101010110100 +011001110011 +100011000111 +000100111100 +010100001001 +000110000100 +001100110000 +110111100011 +010110010010 +100010000010 +100100111101 +011000111101 +000100110100 +011101001000 +110111010110 +010111000000 +010111110100 +110111011000 +101010000000 +101001100101 +111100001100 +110010110010 +010111001001 +011101010001 +011100010001 +110011011010 +100110000011 +001111001100 +100010110111 +111000101100 +000101101110 +000100010101 +010101001001 +010101101000 +101100101110 +011011001000 +111110010011 +010111101101 +000001111111 +110111101001 +010101010110 +101100011011 +011110110011 +101011100011 +110010111001 +110010001111 +001010111101 +000101011110 +111011111010 +100100010011 +000100000010 +001110101011 +001001110000 +001011000001 +101001000100 +000111100010 +010101110100 +010111110000 +111100011100 +100001010011 +111111001100 +100010011000 +110100011011 +100111100111 +111110001001 +011000001001 +001001100010 +100001100111 +001011100110 +111001101010 +111011111011 +110011010001 +100001100010 +000010100011 +100101011101 +000111100101 +110001100111 +101000000101 +011010000101 +110000010011 +010110000011 +010101010001 +111010101010 +110111010010 +111101011000 +010010010000 +001000101011 +001001101010 +011101100110 +010001011000 +101001101010 +011110100000 +011110110000 +011010010000 +010010100001 +111001110010 +101010000111 +111101100001 +111011000010 +110000100101 +100110011010 +010000011011 +100001001001 +111100000000 +001000011111 +001001010010 +110110110110 +010111101100 +010000101100 +001110001110 +011000010001 +001101010000 +100111111111 +101110010111 +001101100100 +001000010010 +010000000011 +000110000010 +000001101000 diff --git a/2021/day03_binary_diagnostic/tests/sample_input b/2021/day03_binary_diagnostic/tests/sample_input new file mode 100644 index 0000000..a6366a8 --- /dev/null +++ b/2021/day03_binary_diagnostic/tests/sample_input @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010