From 3c97ec63d921c7d2f132a834f9b739cc8d039c99 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Fri, 10 Dec 2021 19:54:01 +0300 Subject: 2021, day3: done --- 2021/day3/Cargo.toml | 8 + 2021/day3/input | 1000 +++++++++++++++++++++++++++++++++++++++++++++++ 2021/day3/input.example | 12 + 2021/day3/src/main.rs | 99 +++++ 4 files changed, 1119 insertions(+) create mode 100644 2021/day3/Cargo.toml create mode 100644 2021/day3/input create mode 100644 2021/day3/input.example create mode 100644 2021/day3/src/main.rs (limited to '2021/day3') diff --git a/2021/day3/Cargo.toml b/2021/day3/Cargo.toml new file mode 100644 index 0000000..898e70d --- /dev/null +++ b/2021/day3/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day3" +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/day3/input b/2021/day3/input new file mode 100644 index 0000000..929c455 --- /dev/null +++ b/2021/day3/input @@ -0,0 +1,1000 @@ +010101110000 +010011000110 +010101000011 +111100100001 +011100110101 +110001010101 +001111110101 +101100011100 +010111111011 +101010111101 +101000001110 +001000100001 +000100011110 +100011000100 +101100100001 +101010111010 +111000001011 +101101001011 +000010010110 +110111100111 +111101011110 +100100010010 +110001011110 +010011101000 +111110000110 +110001011111 +001001010110 +110111100100 +100111000101 +110011110000 +000110000010 +010001100001 +111110111011 +100010101011 +000000010101 +100010101110 +100110100111 +100111101111 +110011110001 +001000111100 +001101010010 +111101011101 +011000110001 +111111011010 +011000000000 +100000111001 +011110101100 +011101010111 +011001111110 +011100101101 +101100010011 +001100110000 +110101101100 +110110001100 +000101101110 +001010110010 +111110110110 +111010010011 +100010111111 +001001001000 +110111001011 +010110000010 +100000000100 +100001010010 +110010011101 +000001011011 +000100010100 +101011000001 +011101110111 +001100010100 +101000010110 +111111001100 +110011001011 +110000000000 +010100110101 +001111100011 +100011010010 +100111100011 +110101001010 +011111011000 +001010100110 +010101010100 +110111011111 +001001010000 +001011010101 +101111011011 +101001101000 +000011101110 +000101110010 +100011001010 +011001010010 +100100110011 +010010011100 +001100100110 +100111110111 +000111111000 +100000001101 +001111010101 +000001001111 +000101100000 +000101001111 +101010001001 +111011110001 +000111100011 +110011000010 +110011111101 +101001010110 +101111111000 +000110100010 +101111010001 +010011110001 +111110010010 +101111011101 +101110111101 +010001011111 +101110011010 +001110001100 +000110011001 +000101000101 +101010111011 +110010101100 +000100101101 +010111011111 +111001010110 +000010100100 +100000111100 +101101110001 +100001111010 +000111010010 +111100000110 +010010000000 +010001001011 +111101010000 +010011111110 +111011101011 +110110100110 +100001000011 +010010000110 +010000000001 +000001000101 +011010100000 +001011011000 +110101010001 +011000111110 +010011101101 +010011000011 +011010011101 +001101110100 +010001100101 +111011100011 +010111101000 +111100000010 +010000110000 +000100111001 +010010110100 +000101110110 +000011001110 +111010111100 +100101010001 +111011000101 +011111101100 +000110010001 +100011101111 +100101001110 +101101110010 +000000011010 +000001101000 +010110011000 +110100001001 +111111101100 +011010110001 +011100111101 +001101010110 +100111000111 +000000001111 +001110101010 +101000011001 +000111010111 +100110110001 +100011000011 +110111101011 +000110111001 +000010010011 +010111011001 +000111001000 +100011011010 +011101000100 +101010010100 +010111110000 +011011100111 +000001110001 +000100111000 +111100001011 +100101010000 +000110110100 +110011110111 +111000010111 +101110011000 +110011010000 +001111000100 +111010001001 +110001111000 +011101011111 +000001001100 +110100111011 +110001011011 +001010000100 +110011101110 +101100100100 +100101100001 +001010001010 +101010001000 +000110101001 +001100010000 +110001000101 +000100111110 +000110100111 +100010001010 +100100010110 +010001110011 +101111010010 +100000001001 +000110110000 +100010011011 +111101000001 +101111111011 +111111100110 +101001101010 +000011111011 +110000000011 +000001111010 +001001000100 +101110001101 +100111011111 +110101101110 +100011011011 +101000010001 +101001111000 +111101110010 +000101000011 +100100010100 +010101111101 +100110001011 +010011010111 +110111100101 +110010010101 +100010101101 +100110000110 +001101000001 +111001001000 +010111010011 +000110001111 +010101011010 +000000001000 +001001100100 +101111111001 +001110101000 +000110100100 +001111101111 +010100011000 +010000110110 +011101101110 +010100100100 +111110010101 +101100000110 +011010110000 +001001011101 +000000000101 +100101111001 +110101110110 +100111101010 +010010101101 +010100001111 +111111111110 +011111000000 +000001101111 +011001011001 +001101101000 +011010110110 +100101000110 +110001101110 +110100101011 +010001110100 +011110110101 +010001010001 +110110101000 +010000100011 +010101011001 +101111111010 +000011110010 +001000011101 +110001100010 +110110000011 +010000111011 +111010010000 +011001001000 +000111011010 +011111111100 +111001111100 +001101000100 +101110001011 +101110000111 +011010111111 +110011100111 +001101110010 +011111101111 +001110000110 +010111100001 +110100101001 +101010110010 +111011010100 +101001110010 +101101000100 +100111010001 +000011010110 +100110000100 +101000101010 +000000001010 +110100111010 +110010011010 +100110010100 +011101110010 +000111111010 +001000101001 +110100110010 +100100111011 +011001010111 +111000101010 +011001011011 +011010000000 +001101101110 +101010111000 +101010011000 +011000010101 +111001011010 +100111111111 +001101001100 +111011011010 +110110100011 +111010010100 +000110011101 +011100010011 +011111100111 +010110100000 +001100001000 +000110000110 +000101011011 +101011101011 +111101111110 +111111110000 +101101001100 +011000110110 +100111001000 +011011111100 +001111011110 +111011100100 +000101010010 +100011011000 +100000100110 +100000110011 +011000010110 +001010110100 +100111100010 +011110101011 +110111111010 +011100110110 +100100000000 +011010101101 +001101000010 +010110010111 +100010011100 +111101010101 +011011010111 +000000000110 +101001100000 +010100110011 +010101111111 +101110111001 +101101111000 +101110101000 +011011111110 +010001101010 +001000101010 +111100111111 +001011011110 +101010101001 +011111001010 +001001001111 +101000101101 +001111011000 +000100001101 +101101011000 +001100000100 +001000111101 +000001110100 +111101011111 +111010000010 +101111011111 +001100110101 +101010110001 +001110111010 +010010111110 +101101010011 +100001100110 +110010010001 +111100111001 +000100010001 +110010101010 +111010110101 +000001010110 +000111110110 +111010101100 +011110001100 +010110001001 +010111100011 +001010111000 +100111100111 +110110011001 +010100000000 +001011001111 +110010110000 +100011001111 +100111111011 +110001001001 +000100101111 +011010011011 +001001111101 +111110001100 +101111110010 +101100101110 +100110111001 +111010110011 +110010011110 +001111001101 +010011100111 +000111000011 +101101000000 +000001100101 +011101111001 +110100100110 +000110000001 +110111011101 +011100000110 +001011110100 +010001011011 +110000011110 +110001001011 +110100000001 +011010010100 +010010000101 +001010110111 +110001001111 +101100000001 +010000000000 +101110010001 +001100101110 +010000011010 +010100101011 +011010101100 +111101011011 +110111010000 +111111101111 +111011111011 +110001110111 +011100100011 +100001110101 +100111011001 +100110010000 +100110101110 +110101100001 +001010001101 +011101101000 +110001001110 +110010111011 +111100110100 +101011100011 +010100100101 +010001101111 +111000110101 +001000101011 +101101000101 +011000100111 +001111000011 +011000000011 +000011001111 +001010011111 +101001100010 +101010100001 +001001110010 +101010000111 +010111011010 +110101110111 +000110101111 +000110010011 +010110110010 +100111001011 +001100111111 +111110000001 +110110111000 +101010000100 +011100101010 +001000010101 +010010011001 +011111000110 +010011010011 +001011101101 +001001110100 +110101011100 +011000110100 +010101101000 +100000100000 +011011111010 +011010011111 +111011011101 +001111101110 +001000110101 +010001011100 +110101100010 +001100100100 +001100101011 +100110111000 +001100101101 +101000001011 +001100010101 +001100011010 +101011010101 +110101011111 +101111001011 +000010001111 +011110001111 +110000000100 +000110100001 +010010101111 +110000000110 +010011000000 +011001000101 +001101011010 +000011001101 +110100101010 +111001010111 +010011010101 +111010100101 +111110110101 +111110011100 +110010000110 +000110100011 +001000111000 +000101101100 +101110001010 +010010010110 +100001110111 +111010100010 +110110010111 +101000011000 +100100111010 +001000011011 +111101101011 +101001010111 +100110000011 +100001010001 +011011000001 +010110010101 +110001010000 +001110000000 +111001001110 +011011101100 +000100001111 +111000110011 +111101101101 +100110000000 +100011110110 +001000000010 +110011011000 +111000100110 +100110110000 +010011101010 +111011001010 +001101111000 +001100000111 +001110011110 +100001000100 +000010000111 +011010101001 +111110001001 +110110000010 +011011101101 +110001111010 +011001101110 +010011000010 +100101110100 +101000100001 +000111011101 +101100001011 +100000011010 +111001001001 +100000000001 +000101111001 +100101101101 +111001101111 +001011011011 +000010101000 +001010111010 +000100100011 +001110101101 +100000111000 +110110001110 +000111101101 +001010001111 +011000111011 +100000100100 +100011010110 +000010000000 +001000001101 +010001001110 +000111111011 +100110000001 +110010111000 +111100111100 +001000001100 +000010110100 +011010001110 +110001110101 +001100000101 +100010110100 +111000000010 +110100100101 +010010111011 +010110011001 +001001101100 +001010101010 +100001011100 +010001101001 +000000010010 +000101111010 +011000101011 +111111010101 +011110101111 +011110011001 +001101101101 +111110101110 +101111110100 +010101001111 +010010011101 +000110101100 +011100000011 +100011101001 +101101100111 +110011101100 +000110111010 +000011001000 +111101010011 +011011101110 +111101001100 +100100101111 +111010110111 +000001100110 +110100110101 +110100001011 +111010000001 +001011110011 +001010110101 +100001100000 +101000101100 +011011100001 +000011011011 +100101110001 +101100111100 +010000111110 +010100101101 +001111011111 +110101010000 +000110010100 +001001011011 +110111111011 +111100010110 +110000101011 +111001000100 +000100000111 +001011001110 +011110100010 +011011110101 +001100011001 +111010011111 +011011110110 +111101000000 +101111110000 +011100111000 +010010011011 +101000110010 +001101101111 +010001000011 +001010010110 +101000010010 +110111001111 +011111010101 +111111011001 +100011110011 +110001100100 +100000010100 +001111011010 +111001111000 +110101001011 +101000110001 +111001111111 +110000010111 +000000100100 +101011101110 +111001110111 +001110011010 +110011101000 +001011100100 +011001011111 +010100010101 +011111111010 +010010110000 +100011101110 +000011000110 +001001010111 +010111110100 +010110110101 +010000101000 +101010101010 +000100100010 +000110011000 +101010110101 +100000100001 +101100010010 +010110000110 +011011010011 +000001010010 +011101111110 +100010000100 +000100101110 +010110010001 +101100011011 +010111010000 +101100011111 +010011001010 +100010100000 +001001101001 +110010011001 +111000110100 +111111010000 +111100101110 +101010001010 +111111100100 +011110001110 +101100001111 +001010001100 +110100100011 +011100010101 +110001011000 +000010100001 +011000011110 +000000101101 +000011101101 +111010011000 +000101110100 +111100011010 +101100010110 +100001101011 +110100110100 +101100111010 +010000011110 +101001100011 +000101110001 +001101111111 +111101000101 +100101111101 +001101011110 +011111101011 +100000010101 +011010000001 +011000011101 +110101011110 +111010101000 +110111111100 +101111110011 +101101001001 +000111101001 +010110000011 +110011000000 +000001111101 +001011101000 +001001111011 +110110000111 +101100010101 +101100100111 +000010110001 +110000111011 +110111001000 +010010111111 +100010110000 +010000111100 +101001000111 +010100001100 +001111001010 +010100101010 +100001000010 +010011101111 +100011000001 +110000001101 +011001011110 +100111111001 +001100111101 +010110100110 +001000011001 +101000111110 +000100001011 +111110100111 +011100111001 +100011111001 +100001100001 +110110001011 +110011101111 +011010111011 +110101010110 +010111101001 +111110000011 +111100000111 +011111100100 +101001000001 +110111010100 +000011000000 +000100111101 +001100010011 +100000010011 +010010001001 +010111000100 +110110010100 +011100101110 +110110100010 +100111010111 +001111010011 +000110110110 +000010000110 +110100010001 +010100111101 +011001000110 +101011010100 +001110001101 +101111110101 +110011001010 +110100011100 +001101011000 +100101101110 +000001000111 +111001101011 +101011000010 +001001011111 +111000111010 +111011100110 +001001011100 +000010011000 +000101001001 +100110001111 +010100100010 +001101000101 +010111111100 +100010000001 +110101101111 +100010101010 +110110010001 +011101000111 +011110001010 +100110011110 +010011100100 +110101110100 +000000110010 +001111101100 +010011010010 +010000100000 +110001000100 +110110111111 +011100001111 +110110001101 +111110000010 +010001110010 +001101110101 +111111101101 +010101101101 +001011011100 +001011000001 +001011011101 +010001100010 +011110110011 +000101101011 +000110111000 +101101110000 +101011011001 +001001001110 +000001000000 +101011010010 +101110010111 +110011100001 +001011101001 +000000100001 +010000100110 +101111010000 +011000011100 +100011100110 +110111110011 +110010000100 +101110000000 +110010101101 +011101111000 +001111000110 +101001011100 +011111001111 +111011101010 +001110101110 +101001000011 +111101101111 +111001110101 +000110100110 +111111011011 +101011011011 +111100101101 +111111010100 +101010010000 +101000110011 +011110100000 +010100101001 +010011100011 +101000111001 +101100000111 +101010100010 +101110011111 +111011011110 +110011011001 +101001101101 +110010001011 +010100101000 +001010011010 +001100000000 +110000010000 +000110111011 +011101111111 +011001001001 +011011001001 +000100101001 +110010010010 +000110001001 +001000100011 +101011101111 +100111110010 +110010101011 +000000110110 +010111001101 +100100001111 +100010001011 +011101100100 +000011111000 +010100111011 +010101101100 +100110100110 +110100101110 +010111111001 +010010011110 +111110000100 +000110001101 +100100100001 +111011010111 +101001010101 +111011010010 +101001000000 +100111101100 +101010110000 +000111001101 +000100100110 +100100101000 +111000011001 +000110110101 +111010000011 +101100110001 +000001011000 +001110110011 +101000100110 +100110100101 +010001000111 +100011010001 +010100011101 +110100110110 +100010100110 +101100010001 +010011000100 +100100010101 +110000101101 +010101001100 +100011100100 +001111010100 +100111001001 +110000100000 +100100010111 +010101000010 +101101001010 +110101111101 +010100000110 +011101100110 +011011010010 diff --git a/2021/day3/input.example b/2021/day3/input.example new file mode 100644 index 0000000..a6366a8 --- /dev/null +++ b/2021/day3/input.example @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 diff --git a/2021/day3/src/main.rs b/2021/day3/src/main.rs new file mode 100644 index 0000000..2440a39 --- /dev/null +++ b/2021/day3/src/main.rs @@ -0,0 +1,99 @@ +use std::env; +use std::fs::File; +use std::io::{BufRead, BufReader}; + +fn main() { + let args: Vec = env::args().collect(); + + if args.len() != 2 { + eprintln!("Usage: {} filename", args[0]); + std::process::exit(1); + } + + let filename = &args[1]; + + let file = File::open(filename).unwrap_or_else(|_| panic!("No such file: {}", filename)); + let reader = BufReader::new(file); + + let mut numbers: Vec = Vec::new(); + + for line in reader.lines() { + let line = line.unwrap(); + numbers.push(isize::from_str_radix(&line, 2).unwrap().try_into().unwrap()); + } + + let mut co2_numbers: Vec = numbers.clone(); + let mut oxygen: u32 = 0; + let mut co2: u32 = 0; + + for bit in (0..12).rev() { + let keep; + let mut one_bits = 0; + let mut zero_bits = 0; + for num in &numbers { + one_bits += (num >> bit) & 1; + zero_bits += ((num >> bit) & 1) ^ 1; + } + + println!("for bit: {}", bit); + println!("one_bits: {}", one_bits); + println!("zero_bits: {}", zero_bits); + + if zero_bits > one_bits { + keep = 0; + } else if zero_bits < one_bits { + keep = 1; + } else { + keep = 1; + } + + println!("keeping {}", keep); + + numbers.retain(|num| ((num >> bit) & 1) == keep); + + if numbers.len() == 1 { + oxygen = numbers.pop().unwrap(); + break; + } + + println!("{:?}", numbers); + } + + println!("====co2 now===="); + + for bit in (0..12).rev() { + let keep; + let mut one_bits = 0; + let mut zero_bits = 0; + for num in &co2_numbers { + one_bits += (num >> bit) & 1; + zero_bits += ((num >> bit) & 1) ^ 1; + } + + println!("for bit: {}", bit); + println!("one_bits: {}", one_bits); + println!("zero_bits: {}", zero_bits); + + if zero_bits > one_bits { + keep = 1; + } else if zero_bits < one_bits { + keep = 0; + } else { + keep = 0; + } + + println!("keeping {}", keep); + + co2_numbers.retain(|num| (num >> bit) & 1 == keep); + + if co2_numbers.len() == 1 { + co2 = co2_numbers.pop().unwrap(); + break; + } + + println!("{:?}", co2_numbers); + } + + println!("oxygen: {}\nco2: {}", oxygen, co2); + println!("answer: {}", oxygen * co2); +} -- cgit v1.2.3-70-g09d2