diff --git a/input/day3.txt b/input/day3.txt new file mode 100644 index 0000000..1d938ed --- /dev/null +++ b/input/day3.txt @@ -0,0 +1,1000 @@ +110001010110 +011101111101 +111011110101 +011110101000 +100100011001 +011001001110 +101011000101 +110000111111 +110110101000 +010101101000 +001101001101 +111000001011 +111000011010 +001111001000 +100110001111 +111100110100 +100000010101 +001110100000 +100111110110 +100110000110 +111100101101 +100101010100 +011100010011 +111010110000 +110100011110 +111010101100 +111001001101 +110001110100 +100110111011 +101111111011 +011100111000 +010011110010 +111001011011 +110100111000 +111000011001 +010100000000 +100000101111 +010100110010 +110001101000 +000101101001 +001111101010 +100100000001 +101000111011 +101110001111 +000100011000 +100110101110 +111011101010 +010110001101 +110100001111 +011110101100 +101000011100 +111101111101 +110101010100 +101100111111 +111110110101 +011011110111 +101101000111 +111101000011 +001011110110 +110011110111 +000011101111 +100001101010 +111011101101 +010110111000 +110101111100 +110010101000 +001011001010 +000110101001 +000001000011 +111100001010 +100110111010 +111011111000 +011111101111 +000010100111 +111011011111 +011101110010 +000000100111 +100111000100 +000010010111 +101111101011 +011111000101 +011100100101 +011000111101 +011110001010 +100110101011 +011011010011 +011100000101 +011110000100 +000110110010 +001101101110 +110011000001 +110100111110 +110011000101 +000101011110 +001010000000 +000011000010 +110010111110 +011111110110 +100000110000 +011101111000 +100011100101 +101000000101 +001111010011 +010110110011 +000100100100 +001010001001 +010011100100 +110101110011 +001001000001 +101110110010 +001001000110 +010010100000 +000111101011 +010010001000 +011111000111 +010011010001 +110101101101 +000110010101 +111011110100 +010000110101 +110110101111 +010110111101 +100000010000 +001010011111 +111001001111 +011110110111 +101011000111 +010100011110 +111100100011 +110100110110 +001000011000 +011011111101 +111111110010 +000011110010 +110101111101 +001011111101 +000010011000 +001111101101 +100010010101 +111101110001 +011010110101 +011000000110 +111110100011 +000010111001 +101001111100 +001011011010 +100110111110 +000111001010 +001011101110 +100010110110 +001100110000 +000110001010 +100010001000 +111110011110 +110011111000 +100011101110 +001110100101 +010110110000 +101110101010 +110111010111 +110011000100 +100110101100 +101110100110 +010101011101 +100111100010 +010110110010 +000111100001 +101001101001 +011101110101 +011111100111 +000101010011 +110111100010 +011110000000 +011101110110 +110100110100 +111110111001 +000100000011 +011110111011 +011010100111 +111100110111 +100010011101 +011001001111 +001010101100 +010110111010 +111000110010 +011011100111 +101111000010 +110011011100 +110110011110 +000101011100 +100100110000 +110000100011 +011001101001 +110000100010 +001000000011 +010110001100 +111111000001 +101111011100 +100100100101 +101010100110 +011110000001 +010100110110 +010111011000 +011110100010 +101000110010 +110001011011 +111110011111 +111100100110 +111110101000 +010001111101 +111000101111 +111001010110 +101000001111 +000100101001 +011101011001 +111101111100 +010001100100 +010111011100 +100101010110 +001010110010 +010010111110 +101110011100 +111100101010 +001001111111 +011101010000 +111011111101 +011000001110 +111100110010 +001001111101 +011100001110 +001001001000 +110011101111 +010001011110 +001100100100 +011100110000 +111100001110 +111110110000 +000011000011 +011000000011 +110010111111 +011110001011 +101110100010 +100100111000 +111101011010 +000111110101 +010100001001 +110010101101 +001101011111 +000101100101 +110011101100 +110111111111 +111000011000 +010000100100 +110101101011 +101010101001 +100111001101 +111011000001 +101101111010 +111001111001 +001001110100 +000111110000 +010111010110 +100011010111 +111011011101 +100010100001 +000111001110 +111100010001 +101001101101 +111101100111 +100110100010 +000010100101 +010010110111 +101100000101 +111011010110 +101101101101 +001110110111 +110011011111 +011111111001 +100100001100 +101010011101 +100000110110 +110101010000 +110110110111 +101001110110 +100001000101 +110100001001 +000111000100 +101000101001 +111111001100 +101011000100 +111000010100 +100111000110 +111011010100 +101101011101 +010000101101 +000110110110 +001110000101 +001010001000 +100111101111 +001011101001 +001100111101 +110111011111 +111010011010 +001110101111 +101000001110 +101001110001 +110111010010 +011111100101 +110001001001 +010010111010 +000001001010 +011000001100 +100000111011 +111000010101 +001110011001 +010001001100 +001100010101 +001011011000 +001101100100 +011001111111 +010010001011 +100010011111 +100011000100 +000110011110 +010010000000 +110110011001 +111000111010 +111010000111 +111011110110 +101101100111 +111100101001 +110001101110 +111101100001 +011111110111 +110110101001 +000111010101 +101011111111 +100110001000 +101000100000 +110101110010 +010100011011 +000000100101 +000000011110 +101011110000 +101001100001 +111010101101 +000011011100 +000000100010 +001011100000 +001110111111 +110100111001 +010110110111 +010111111010 +110100011010 +000101011111 +001000011100 +010011001011 +111011001100 +101111001101 +100010000001 +011000001010 +101010000011 +110110010100 +101001011101 +100001010001 +110011010101 +000001100000 +100101000101 +110110100110 +110001111010 +111011101011 +010010111101 +000110100101 +011110101110 +000101110001 +111111101100 +011100110010 +100011111010 +010011111101 +101000010101 +011000101000 +011111001111 +111001111010 +100000000100 +010000101001 +110000001000 +100100001101 +011001111010 +110111110111 +000011100111 +001100110001 +000111010001 +101110011110 +110010000010 +101100001101 +001000001100 +101110011001 +111000000110 +000111111100 +001110001110 +100011110100 +001011101100 +011010010010 +101000011111 +110001011010 +011001000000 +010011110001 +100001111011 +011011001111 +110100010011 +010111010001 +010101011011 +100000001100 +100111101010 +100010100000 +110000110001 +010000010101 +010101000010 +010100101100 +110101000001 +100010010110 +110011101011 +010101011001 +010001000011 +000011111110 +111110011101 +011010010000 +011011111100 +110010110011 +001101111011 +000011111101 +110000010110 +111011011011 +111110000000 +101100111010 +100110011111 +110111011001 +110110010010 +010010011110 +010110010101 +101110000000 +011001110110 +100110100011 +111101111010 +110101010011 +111110100010 +100110001100 +010010100010 +101010100001 +000000000101 +111001011100 +100100111110 +000000011010 +110011111101 +010100000011 +100001111111 +110001011001 +111111100011 +111101110000 +100011001101 +110001010100 +100110000111 +111110110110 +001011000011 +111111000000 +101011101101 +110001111011 +011111001000 +000001011101 +001101000011 +001000010000 +000011001110 +011001101000 +110101100101 +001111110111 +111110111101 +000011000111 +001010011100 +110010010011 +110001110111 +111010010011 +100010001110 +110000101000 +011001011000 +011100100000 +001000011001 +101011110011 +001010011110 +110100000101 +010011011110 +011001100001 +011110011010 +100110000000 +010110010100 +000100111110 +110101110101 +000110111100 +010000000111 +111101010100 +111011011100 +000010000011 +001100100001 +110010110001 +010010000010 +001000010110 +011001000001 +010100101110 +011100010100 +100000011010 +010111100000 +010111011001 +011100101001 +101110101011 +110010011001 +111110001110 +011100011100 +110000101010 +000010100010 +100101101110 +100010111010 +110101100001 +101111000100 +101000111001 +011111101101 +111100111110 +011001001000 +110100110010 +000011010101 +111010111011 +010010011000 +100101000100 +111010100100 +000111001101 +010001011000 +111101101100 +111110001111 +110100011011 +111100010101 +111001111100 +011100111011 +011011101111 +011001000011 +011100111101 +010100100010 +110101010110 +101100001000 +001100101001 +000001001111 +000110010100 +111101101010 +100111000111 +001011000111 +001011111011 +110101011100 +110000110101 +010001000110 +011100101111 +101110010001 +001011001000 +100100110010 +000011111000 +010010010000 +001011100010 +000000110011 +000101011010 +101111110000 +100001010100 +101110110110 +001100000101 +110111001101 +010001010011 +101011001111 +111111111110 +110011010011 +011101000101 +100001101000 +011001101100 +011100010000 +001111111000 +010110011111 +111101110100 +100011011101 +100010110011 +110111011000 +100110010101 +001001000000 +101000100110 +011010011010 +000000000110 +010011001010 +110000110010 +100001011010 +101111001001 +100010101001 +110111111100 +000010111110 +000110101111 +011101001011 +101110011010 +111101010101 +001101100110 +111100001101 +000011100000 +100111010110 +110100100111 +100011100110 +100001011110 +001110110011 +101011010011 +001100001000 +111011001110 +110011100000 +101000011001 +011100100001 +000110001011 +001001001001 +000011011101 +000010111010 +011001100111 +101001011011 +010001101101 +101110001011 +000001011110 +011100110001 +110011111011 +001101010011 +000101000110 +110101000000 +110000101111 +000110110111 +110100101100 +111011110000 +011110011101 +001101000001 +011010001010 +101000111010 +111101111000 +000010000000 +000010001010 +110000100110 +110010100111 +110000001100 +100010000100 +110010100101 +111011101001 +100011110000 +010110110110 +111100011011 +110001101111 +110010101111 +001001001010 +100100000011 +110100100011 +101010110001 +001001011111 +101101110110 +001111010101 +110000010011 +010110001110 +011011011100 +000000110100 +100101110001 +011011001100 +100101000001 +100101101101 +101010001110 +110101100110 +111010111101 +111001000101 +010101111100 +000110000000 +001010111010 +110110000001 +100000101110 +111011110010 +000111000101 +011001110010 +000100101010 +101001010010 +010001100001 +000101100000 +111000101110 +000100010010 +110110000011 +010101001010 +110101101110 +101111101000 +110011100101 +111110110011 +000100010011 +110110010001 +111100000010 +011011111011 +101101101001 +101011011001 +011011011111 +101100110001 +011011100010 +011101010101 +011101110100 +010000101011 +010110011011 +111001101001 +010010010010 +010111000010 +100001101100 +000000101110 +000000110101 +111001000100 +110111001110 +111100111001 +110101010010 +111100011000 +100110100100 +000100000000 +110111101011 +001011110111 +010101101011 +101110001100 +111111101010 +111100010110 +101000110011 +110100010000 +010010001100 +011101111100 +000011110001 +011010101010 +110001101010 +101001000011 +100010000110 +110100111101 +000011110110 +001010100110 +010111010101 +101011100111 +000101001000 +110010001000 +001100000100 +010000111100 +111011111111 +100101001010 +110011100111 +100011100000 +101010011111 +010111100101 +110110110101 +010111010000 +110011001110 +101100111101 +000001100001 +111010100001 +010000010110 +001001000101 +101011110001 +000101101110 +011110000010 +110011110100 +100001000001 +100000011110 +001101100101 +101111101101 +110010010100 +010111101110 +010101110111 +101010101000 +000100000010 +111000001010 +101100001100 +101010000001 +101001101011 +110110000000 +101100101101 +101010110011 +011000001001 +101010111010 +000001001100 +101001000010 +110000001111 +110101001101 +110011111111 +010110101111 +001101110110 +111010001010 +001010100010 +000101101011 +001100010111 +101111100111 +010100000101 +110111110000 +000110111011 +110000011000 +100101010000 +101010111100 +100101000111 +001110100111 +001000111011 +011010010100 +111110010010 +010101010000 +101100010110 +000011001100 +010010101011 +011011000100 +011100111100 +111010111010 +101111010000 +101011010000 +001010100111 +010100111100 +011001000010 +011110110100 +111000001001 +110010110010 +101011111001 +110001111001 +101001000100 +010100100011 +000110110101 +001000100010 +100110110010 +000100010001 +010100010010 +001011000000 +110101011000 +001001101011 +000111101110 +000100110000 +010000010100 +001011011011 +100000001110 +111010110100 +111001101011 +101110110011 +111010111111 +010101010100 +001110111110 +111010110110 +001101001011 +010111101011 +000110100011 +100001010000 +011111001010 +011001001001 +101100001001 +001100111011 +001001110111 +101110110000 +010011010100 +001100001111 +001111100001 +010100010011 +001001000010 +100101001100 +001010001101 +011011001001 +101110110001 +010001110100 +101101011001 +010001111001 +011101001000 +010011100011 +001000111010 +111110100101 +110000010001 +110100000000 +101011101110 +011101001111 +111110001101 +101100010000 +010100010101 +000010001000 +111111010110 +000100010100 +110100010101 +110010000110 +001111010111 +000100101100 +001111100111 +011110001101 +000111001111 +000001000110 +101100100000 +110100010100 +001011010000 +101011100110 +010000101010 +101001000101 +011000010110 +010111110100 +110010001001 +001111010000 +010010100111 +011001010001 +100010100010 +011111010101 +011100000010 +110101110000 +001010110100 +101110111110 +000010110100 +101100000100 +111010110111 +001001101010 +111010010110 +011011001010 +100001010111 +110010011101 +010110100001 +110000111101 +011001110111 +111110001010 +000101101010 +010011101101 +110111010001 +111011011010 +001111111010 +100111001111 +010111111111 +010000010000 +001111110011 +100110100000 +000100101111 +001000010010 +110001010001 +001011100111 +101111111111 +100101101001 +000010011001 +011011001101 +010110100000 +101001101100 +010010001111 +110111001001 +101011001000 +001001010010 +011011001000 +001110010110 +011011101011 +100111111101 +110001001111 +001110101001 +101101111011 +101011011101 +100100011111 +100001001111 +101000001010 +000010011010 +010110010010 +100101110100 +010100010100 +110111001111 +100000101001 +010110101100 +011101010011 +000111000011 +000000101010 +101001110010 +010101001000 +111100000001 +100101000010 +010011011010 +011111110011 +011111010011 +110001000101 +100010101011 +111010011101 +010110010110 +110011101000 +101001000000 +000000011100 +010011100000 +101010111000 +000101110101 +100111111110 +010100100110 +110110110100 +110001001011 +100010111101 +111111111101 +111111110101 +000110100110 +001100110011 +000011100101 +111000110011 +111010100010 +101010011001 +001110011010 +010100000100 +010010000011 +101000000001 +000011110000 +010011011100 +111000110001 +010111101111 +101100010010 +101110111111 +111101100101 +001100101011 +100110111100 +011100011010 diff --git a/src/day3.rs b/src/day3.rs new file mode 100644 index 0000000..97774ba --- /dev/null +++ b/src/day3.rs @@ -0,0 +1,87 @@ +pub fn day3() { + // let input = r#" + // 00100 + // 11110 + // 10110 + // 10111 + // 10101 + // 01111 + // 00111 + // 11100 + // 10000 + // 11001 + // 00010 + // 01010 + // "#; + let input = include_str!("../input/day3.txt"); + + let numbers: Vec = input + .trim() + .lines() + .map(|l| u32::from_str_radix(l.trim(), 2).unwrap()) + .collect(); + let nbits = input.trim().lines().next().unwrap().trim().len(); + + let gamma = gamma(&numbers); + println!("gamma: {}", gamma); + println!("epsilon: {}", epsilon(gamma, nbits)); + + println!("oxygen: {}", oxygen(&numbers, nbits)); + println!("co2: {}", co2(&numbers, nbits)); +} + +fn gamma(numbers: &[u32]) -> u32 { + let mut gamma: u32 = 0; + for i in 0..32 { + gamma = gamma | (most_common_bit(numbers, i) << i); + } + gamma +} + +fn most_common_bit(numbers: &[u32], pos: u32) -> u32 { + let count = numbers.iter().filter(|&n| (n >> pos) & 1 == 1).count(); + let frac = (count as f32) / (numbers.len() as f32); + frac.round() as u32 +} + +fn epsilon(gamma: u32, nbits: usize) -> u32 { + println!("{:#b}", !gamma); + println!("{:#b}", 2_u32.pow(nbits as u32) - 1); + !gamma & (2_u32.pow(nbits as u32) - 1) +} + +fn oxygen(numbers: &[u32], nbits: usize) -> u32 { + let mut vec: Vec = numbers.into(); + let mut bit: u32 = nbits as u32; + while vec.len() != 1 { + bit -= 1; + let most_common = most_common_bit(&vec, bit); + vec = vec + .into_iter() + .filter(|n| ((n >> bit) & 1) == most_common) + .collect(); + for n in vec.iter() { + print!("{:#b}, ", n); + } + println!(); + } + *vec.get(0).unwrap() +} + +fn co2(numbers: &[u32], nbits: usize) -> u32 { + let mut vec: Vec = numbers.into(); + let mut bit: u32 = nbits as u32; + while vec.len() != 1 { + bit -= 1; + let least_common = 1 - most_common_bit(&vec, bit); + vec = vec + .into_iter() + .filter(|n| ((n >> bit) & 1) == least_common) + .collect(); + // for n in vec.iter() { + // print!("{:#b}, ", n); + // } + // println!(); + } + *vec.get(0).unwrap() +} diff --git a/src/main.rs b/src/main.rs index ce49bb9..06ffbba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,8 @@ mod day1; mod day2; +mod day3; fn main() { - day2::day2(); + day3::day3(); }