diff --git a/Cargo.toml b/Cargo.toml index a60ae85..e226553 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,7 @@ path = "src/day1.rs" [[bin]] name = "day2" path = "src/day2.rs" + +[[bin]] +name = "day3" +path = "src/day3.rs" diff --git a/inputs/day3.txt b/inputs/day3.txt new file mode 100644 index 0000000..e1c1f3d --- /dev/null +++ b/inputs/day3.txt @@ -0,0 +1,1000 @@ +011101101110 +010110001101 +100111000110 +011110101000 +101101000100 +000001100110 +110111100010 +111000010011 +001011000110 +111110101101 +110100101011 +100100011010 +001010111001 +110111110100 +100011011110 +111111111010 +001000100011 +001101100000 +010101010000 +011000100101 +110100000110 +110011010011 +101011001011 +110111100001 +000100111111 +011110000100 +010110111010 +011000011011 +011001100100 +000111111100 +010101111101 +001110011101 +100001001001 +111100000011 +110110110111 +101000011011 +110100100000 +110101101001 +001010111101 +010101010100 +111101001111 +110001000001 +000111010111 +001001101111 +101100011110 +001001111011 +011100101100 +010100110000 +000100000010 +010000101011 +111001010011 +001100101111 +101000010100 +000000111011 +010111101000 +101000100110 +010101111100 +101101001100 +101001110001 +000111110011 +111110110000 +111101101101 +001010111000 +101001000101 +010000010011 +011110010010 +000000101011 +101000111100 +011100001011 +000000100001 +100011111101 +101111011001 +000011011010 +100010111101 +100011001011 +011001101100 +001101101011 +010010010000 +110011011011 +110100110111 +000110100100 +110101110001 +001010010001 +100110100001 +010111111001 +011101000011 +100110010110 +100010101101 +000001011000 +110000111010 +001110010100 +000110011000 +100001110011 +101111111001 +000011001100 +010110011111 +101010001101 +100001000110 +000101011111 +110000010101 +111100100000 +000111100100 +011110011000 +001111110101 +111011110011 +010111000101 +111101100101 +101100011111 +100001111111 +110011111100 +000000111001 +100101001001 +110001100010 +101001000111 +101010000101 +010000010101 +011011000010 +101001111011 +111100110001 +010100110100 +000011000011 +011001100001 +101110000111 +000110010000 +100011010101 +010000111100 +011011010011 +000011111100 +000010100110 +110000110001 +010110010101 +010101100110 +111001110011 +101011000001 +010011101010 +011101000001 +101111001010 +010100111100 +110100111011 +111010111001 +101100110101 +100101111110 +010111101110 +111011001000 +001100110100 +001110011010 +101100111110 +100110010101 +010011001000 +001100010010 +100001100101 +001001100111 +011001111101 +010100101101 +000011100011 +100111010100 +101010110100 +101011100110 +111000001100 +101111101101 +101001000011 +011000010111 +011111010101 +000000010001 +011010101110 +110101011100 +011000011000 +111011010001 +110001001111 +011100101111 +010010100100 +110111001111 +000001101111 +100011100010 +110111010110 +010000110000 +011010001110 +001111000111 +010001010111 +101010101101 +100000111001 +110110111100 +100111010110 +011100011100 +011011110111 +110011101001 +110010011000 +111010000101 +110000001010 +000000110100 +001010100111 +010011001011 +111010111110 +101111000000 +101001010101 +010110010000 +100010001001 +100010100011 +010100001100 +110000110010 +100011100110 +001111101100 +001110100110 +111000100111 +111011011000 +101010100001 +111100011010 +000101100101 +010111111101 +100011110100 +011110010001 +011001010111 +111101110101 +101010001010 +001010011110 +110011100011 +001001010100 +100010101110 +100011000101 +011010111110 +001011101101 +001100101010 +111101111011 +111101100001 +101111101100 +100101100000 +010011011000 +011001001001 +111111110001 +101000011000 +101000110101 +111001011100 +101100111011 +001000010011 +101111000110 +101001101111 +100101000000 +111011011010 +110001100001 +111110110010 +001010101001 +011010101111 +010101000001 +110100101010 +011110101111 +001010111110 +100111001010 +011011111110 +111011100100 +101101111011 +001111100110 +100110010100 +101110100100 +101111100010 +001111101001 +100000001111 +000111111010 +100011010111 +010100010101 +000100011010 +110001110101 +000101001101 +110101010101 +101011101011 +011101001110 +001000010100 +001110011100 +110100000010 +010010101111 +110101010010 +011111110101 +001111010011 +010001100000 +111101101000 +110100010101 +110000100111 +100000101100 +101110010101 +100100101100 +000010010101 +000111010101 +111000000000 +101011111010 +000101000110 +111110101100 +000001100100 +011101000101 +001011110001 +000011111000 +101000111010 +011100001100 +111010101000 +100010001101 +111100000101 +010001101110 +111000110010 +001100100110 +111001111011 +110101111010 +000100100101 +100101110100 +001011010100 +001000001010 +011101110011 +101110011111 +101111011011 +001000011001 +100011011100 +011101000111 +011010100000 +001111110000 +010100000101 +001010010000 +100101101101 +111100010011 +100101000011 +100000000001 +111000111000 +000101101010 +010101011000 +101101010001 +111111001001 +100001000000 +011110100110 +111101010111 +011111011101 +110000110110 +110010011110 +110000110100 +010001101100 +011101111110 +110111001010 +011000111111 +101110010010 +001010101101 +101101101111 +000101001110 +111000010001 +110110100111 +010101000100 +111001000110 +011010100001 +111100011110 +100101110011 +110001000111 +101000101010 +100101111000 +000011101001 +011110110010 +101111011110 +100000111100 +001100000101 +101010111111 +101011110001 +000000001010 +000000010101 +000011000100 +111101011101 +100100000111 +001111010111 +010110001010 +011001000001 +110011110101 +010100010110 +001111101011 +101111011101 +001111001001 +011001101111 +000011000001 +101010000011 +111101100100 +110100100111 +101000011101 +010101101111 +000000001011 +110101101111 +001110011110 +100101010011 +001010100101 +001110111101 +001111101101 +111111010110 +101011110011 +110010111011 +101101101011 +101100011000 +000010100101 +010000010001 +010101001110 +011100111001 +010010000001 +010111010101 +101110001110 +000100111110 +011110100010 +001010110111 +100010000010 +110010100111 +011111001010 +111000110011 +011000010100 +110011000100 +010010100110 +011110110111 +011001010110 +000111001111 +000111000011 +110110011001 +011110001000 +011110100111 +101001100101 +001101010000 +000111101101 +001001010000 +001100110000 +111001001000 +111110010101 +001001011100 +111111011110 +010110101110 +101001110110 +000000110110 +001011110111 +011111010111 +111100111101 +111010001111 +001111111010 +110001010101 +110000011000 +000001011001 +000010101001 +010100101100 +011101010111 +110100111000 +001101000110 +001101011010 +010000011100 +110101011101 +010110011010 +101011011001 +001111111101 +100011101100 +010011010100 +101011101010 +011000110101 +010001001001 +101110111101 +001010001000 +110101010000 +010101000010 +101101001010 +101000001000 +010011100011 +100100111000 +100010110110 +011100101011 +000010110111 +111000010010 +011011111011 +101000010001 +001010110010 +110100110000 +010111000000 +001011101011 +001000010101 +000110110111 +111001010101 +000111001100 +011110010011 +110010010101 +100000000101 +110100001010 +100000010100 +100001010011 +100110011101 +111100001101 +111011111001 +101110111110 +001000100111 +100000010010 +011110000110 +010101010001 +101101100111 +010100011000 +100110111101 +001000001011 +100111101001 +101100100110 +000100010100 +010011111101 +010001100010 +011111111110 +001011111000 +011110110100 +100101101111 +110111110001 +111000110001 +010110101001 +010110011001 +001010111100 +011110000010 +100001100010 +010011001110 +101101011100 +011100100110 +101000001010 +111110111101 +111101001000 +000010110101 +110110001010 +100011101101 +110111101011 +111111100111 +101110101010 +011010110000 +100011111110 +101110101111 +101001101101 +001011001000 +010010101011 +011100000111 +001011111110 +001001000110 +111000101000 +011011000111 +111110110011 +110010110001 +100001000100 +000011010100 +000101010111 +001011000010 +010110010011 +010111110011 +010011100000 +100011110001 +000100101110 +001010010110 +011100111011 +001101100001 +000001000100 +111010101100 +010001011101 +001011010110 +011011000100 +110000101011 +111100000000 +111110010100 +010010010011 +001110000011 +110101000011 +110101101101 +110101000110 +010011011100 +101101000001 +100001001010 +101111011111 +110111011110 +101110001100 +001111010101 +010110101100 +011101001011 +101000010000 +111000101001 +011001100010 +111011110100 +111101111000 +110011100010 +010000111011 +001101101000 +111000000100 +011000001001 +101110000100 +101100011010 +011011010101 +111000001101 +101000111111 +100010100101 +111000011010 +010110001111 +010011000011 +100011101000 +100001000001 +101111100100 +011000100010 +010000101110 +011101101100 +000011010001 +100001110101 +010101111110 +000011110101 +110101110011 +011101101001 +010000000110 +001000111111 +011101110010 +001011011111 +010001100101 +011001000111 +001010011111 +100000110100 +010111011010 +001110111110 +000101101100 +111000101110 +001010110011 +000100101010 +010111100111 +000110101100 +010010111101 +110110101110 +010010100001 +111001011111 +001100011011 +111000011101 +111000010110 +000000000100 +000010110100 +010011110000 +011001011000 +111110001101 +000100000011 +001100010100 +111110000110 +000010111101 +111010001101 +110011011110 +101001111101 +110100111110 +011010000011 +000010000100 +111101110100 +010000101001 +111001010000 +110011100111 +010111010011 +000010101011 +010001110001 +100010000100 +100000001001 +011111101001 +101010001111 +001101001100 +111110011010 +001101001010 +101000010111 +011010111000 +011001101000 +101000111001 +100111110011 +000000000001 +100111011001 +011101110110 +100001111000 +010110101101 +011111000000 +101010100100 +100010101000 +001000011111 +010110000010 +000100000100 +001001000010 +111000010111 +110010101101 +101000101101 +001110110101 +110010011011 +011011001111 +101010001000 +011001110101 +101000000101 +010010111111 +110100110001 +000100110001 +000000110011 +111011100101 +111000100010 +100100100010 +011100010100 +010010001110 +000010011000 +111110111000 +100101010010 +000001110111 +101111101011 +100011001001 +110110101010 +111100100001 +000100110000 +001111001101 +100000010000 +011111000001 +001111101110 +100111101000 +000111111011 +000001011010 +010100110101 +001111010000 +111011000110 +101000001110 +000101011000 +101011001010 +100101101000 +110101011011 +111000100101 +100100101110 +111100101000 +100110101110 +110001111000 +111010111011 +011011000000 +000001101110 +110110010110 +111100010010 +111111011010 +101110010100 +111010110000 +011101000010 +111010011001 +101001011101 +101100010010 +010010111011 +011110010100 +100000101000 +000101101001 +001000100000 +101000101100 +011011100100 +011100100010 +111110000000 +011011010001 +010110111100 +110110101011 +111111000010 +111101001101 +110101001111 +111110001110 +100001011110 +101111100110 +011110111111 +011011111101 +001000010010 +110111100000 +110001110000 +100001000111 +000010100010 +100000100010 +100010001011 +000100001100 +000000001000 +111101010001 +010100010001 +111001101011 +011010001000 +010001011100 +010101001001 +000110101001 +001001100001 +101000000100 +000100000101 +100011011000 +101100101101 +100110100000 +010100000111 +110100101110 +001111100000 +011110110110 +001100111100 +001111100011 +100111111010 +110011111110 +110010011111 +110111111101 +110000011100 +101011000110 +100110011111 +000011110011 +111000111101 +101011000100 +010111011111 +000111011011 +111010111100 +001110111100 +100010110011 +010000000010 +010000011000 +001111000001 +100100001001 +110101010110 +101110001001 +010011001100 +101100000101 +001010111010 +001000001100 +111100010001 +111110100001 +101010110101 +111100001111 +110001110001 +000110110100 +100100101010 +111110101010 +110111101010 +000111101111 +111000101100 +000100011101 +001110001010 +011110011001 +110001001011 +000111001000 +010001001011 +000100011100 +100000101110 +010111111000 +111011110010 +010011101101 +011010000110 +010100001111 +100100001000 +100001011010 +100111111011 +100000101011 +011101011010 +111100111001 +110010110111 +001010000101 +101000100000 +001111100100 +001100111011 +011000101101 +001110101101 +100011000111 +001010001101 +100110111110 +001001101000 +111000011000 +011001011101 +000110100101 +101000110010 +110110010000 +110100001100 +000001111110 +000111010110 +110101000000 +110011110001 +110100110101 +011111100011 +111011011110 +111111001100 +110111100110 +011101010010 +010001110101 +001001101110 +010110100011 +101101110001 +000111101100 +000010111111 +111001101001 +000101111110 +000000011110 +110011001011 +010101010010 +111101001010 +001010101110 +111010100001 +101110000010 +001101010011 +001101101100 +000110000110 +000010111001 +110100100011 +011001110111 +100001110100 +011001101001 +000100011011 +111001100000 +101110101000 +000001000001 +010111001000 +101101110011 +111111011000 +000001111111 +101111110101 +000101101111 +010111111010 +111010100110 +110011000010 +001110110010 +011100000010 +001010101010 +110110001000 +100011111000 +110101011000 +011001100111 +000101110000 +010110110000 +011010010100 +000101100110 +001011000100 +011100110000 +111100011011 +110000101100 +101110100000 +011110000111 +100010101111 +110111000111 +100010110000 +110010001111 +000001011110 +111110000111 +101101100110 +011001010100 +011011011110 +111001100101 +110101100011 +010100111111 +001010011001 +101101001011 +000011100000 +000100001001 +001010010101 +111100100101 +111101001100 +010111110110 +100011110111 +100101001000 +011011100111 +010110000011 +101001101011 +001101001101 +101101011000 +011100000011 +100101001110 +110001000110 +101111100111 +011111110001 +001111001000 +001101010010 +101110101011 +001111011111 +001000010000 +011011101001 +010101100010 +111000000110 +000100100000 +110000000100 +011100000110 +010100100110 +011001001010 +111010101011 +111001011011 +101101011111 +010111110101 +100101110000 +000101011100 +001010011011 +110110111111 +110001001001 +000001100101 +111110010010 +100110110101 +010101100001 +000001010000 +111100111100 +111011001100 +101100101111 +111111001111 +111100110100 +011010011100 +010000001010 +010010110010 +101110110111 +010000100000 +100101101100 +111001110001 +001110011000 +101010000111 +110011001000 +101100111111 +010110010010 +110011111001 +000111001110 +011101011011 +111011111101 +101100010011 +010000100101 +111110110101 +110101011010 +010110110101 +111011000000 +010001110111 +101111001100 +000100111000 +001011010001 +010011111100 +110011110111 +001100010111 +000010001110 +011001001110 +111101101001 +001000011000 diff --git a/src/day3.rs b/src/day3.rs new file mode 100644 index 0000000..e5966d5 --- /dev/null +++ b/src/day3.rs @@ -0,0 +1,111 @@ +use std::io::{self, BufRead}; + +fn most_common_bits(report: &Vec) -> String { + (0..12) + .map(|i| { + let counts = report + .iter() + .fold((0, 0), |acc, x| match x.chars().nth(i).unwrap() { + '0' => (acc.0 + 1, acc.1), + '1' => (acc.0, acc.1 + 1), + _ => acc, + }); + if counts.0 > counts.1 { + '0' + } else { + '1' + } + }) + .collect() +} + +fn most_common_value(report: Vec, n: usize) -> Option { + (0..n) + .fold(report, |r, i| { + if r.len() > 1 { + let counts = r + .iter() + .fold((0, 0), |acc, x| match x.chars().nth(i).unwrap() { + '0' => (acc.0 + 1, acc.1), + '1' => (acc.0, acc.1 + 1), + _ => acc, + }); + r.iter() + .filter(|x| match x.chars().nth(i).unwrap() { + '0' if counts.0 > counts.1 => true, + '1' if counts.0 <= counts.1 => true, + _ => false, + }) + .cloned() + .collect() + } else { + r + } + }) + .first() + .cloned() +} + +fn least_common_value(report: Vec) -> Option { + (0..12) + .fold(report, |r, i| { + if r.len() > 1 { + let counts = r + .iter() + .fold((0, 0), |acc, x| match x.chars().nth(i).unwrap() { + '0' => (acc.0 + 1, acc.1), + '1' => (acc.0, acc.1 + 1), + _ => acc, + }); + r.iter() + .filter(|x| match x.chars().nth(i).unwrap() { + '0' if counts.0 <= counts.1 => true, + '1' if counts.0 > counts.1 => true, + _ => false, + }) + .cloned() + .collect() + } else { + r + } + }) + .first() + .cloned() +} + +fn main() { + const INPUT: &'static str = include_str!("../inputs/day3.txt"); + let report: Vec = io::Cursor::new(INPUT).lines().map(|l| l.unwrap()).collect(); + let gamma_rate = u32::from_str_radix(&most_common_bits(&report), 2).unwrap(); + let epsilon_rate = 0xfff ^ gamma_rate; + println!("gamma rate {} epsilon rate {}", gamma_rate, epsilon_rate); + println!("solution {}", gamma_rate * epsilon_rate); + + let oxygen_generator_rating = + u32::from_str_radix(&most_common_value(report.clone(), 12).unwrap(), 2).unwrap(); + println!("oxygen generator rating {}", oxygen_generator_rating); + + let co2_scrubber_rating = u32::from_str_radix(&least_common_value(report).unwrap(), 2).unwrap(); + println!("CO2 scrubber rating {}", co2_scrubber_rating); + + println!("solution {}", oxygen_generator_rating * co2_scrubber_rating); +} + +#[test] +fn oxygen_generator_rating() { + let report = vec![ + "00100".to_string(), + "11110".to_string(), + "10110".to_string(), + "10111".to_string(), + "10101".to_string(), + "01111".to_string(), + "00111".to_string(), + "11100".to_string(), + "10000".to_string(), + "11001".to_string(), + "00010".to_string(), + "01010".to_string(), + ]; + assert_eq!(most_common_value(report, 5).unwrap(), "10111"); +}