diff --git a/day3/Cargo.toml b/day3/Cargo.toml new file mode 100644 index 0000000..8aa34bb --- /dev/null +++ b/day3/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day2" +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/day3/input b/day3/input new file mode 100644 index 0000000..3566496 --- /dev/null +++ b/day3/input @@ -0,0 +1,1000 @@ +101000001100 +011111100111 +111100001110 +110000011001 +001001001011 +010011101000 +011001110011 +010100010000 +101110110111 +110110111111 +011100011011 +000010100101 +001110001001 +110111100011 +011111101110 +001100000001 +101101101101 +011110110101 +010101010100 +111010001111 +010011010111 +110111101011 +001011000100 +100111001100 +010011001011 +110101111110 +010011100101 +110100010100 +100110010000 +010100000111 +100110000011 +011111011100 +010011100100 +010110100110 +110011000001 +101000100001 +110111101010 +001011010100 +100101010110 +000011001011 +000110101000 +010111100001 +000111110101 +000000100011 +001011100101 +010111101010 +001101100100 +010000101011 +011011111101 +011111011010 +100101000101 +111001110100 +110011100011 +100000011000 +111101101101 +110100110011 +100101000011 +101001101010 +111111000000 +110010110111 +011010101010 +101111011111 +110011011100 +001110010100 +110110000000 +101000010100 +111110011100 +101110101010 +001111110011 +100110000010 +000011000111 +000110111000 +111100011100 +101001100101 +111111001101 +110101100101 +100011110111 +001100101001 +010101110111 +001101111001 +100101010010 +011010101101 +001110100010 +100111011101 +100001010110 +001000010110 +001010110011 +011011110101 +010100010100 +111110000100 +001100110100 +000110011101 +110010010000 +101101111111 +000110100001 +100001101010 +101000101100 +000000000001 +101101110011 +101101011001 +100000010000 +001111110111 +110101000010 +000000010011 +110010101101 +010011101011 +010100001001 +110000110010 +010110110000 +011101000011 +111100011110 +101010010111 +100111100000 +011110110000 +110001101010 +110010011000 +010001001100 +110100000000 +101001101100 +011011101010 +100100011011 +100111100001 +001011010011 +111001101001 +110001011111 +011110010100 +111000100100 +011111110010 +000010111101 +110110101000 +001110011100 +011000011011 +101111110001 +000100100011 +001110010010 +100010111101 +011101010110 +011001000010 +111110111110 +100110001011 +000011101100 +110000001111 +111010001100 +111100001010 +000101000111 +001101010101 +111000000011 +010010011100 +111010000111 +010110110110 +101111101000 +011100000011 +000101101101 +100110000100 +100010110011 +001011000001 +011001101111 +011010000111 +101100010000 +001010011001 +100001111111 +100000101110 +011001111100 +110100110110 +011100001100 +000111110010 +101100100000 +000010110010 +110111000001 +001111000010 +011000101000 +110001000101 +110010100001 +100100101000 +011011111010 +000010000011 +010111000110 +010011110101 +110111110111 +001001101101 +000011001111 +010011111001 +001011000000 +011111100000 +000101111100 +110011111010 +100100011111 +000101101100 +110101110010 +010100010101 +101010000011 +111101100101 +010100101100 +100110010100 +001100001010 +001000000101 +101100001110 +100100110011 +110110011011 +010010001000 +011000011111 +001000111000 +001110110000 +001111011001 +000101011001 +101010010110 +010101101001 +101011100100 +001110111100 +001101001011 +110001001001 +101011001001 +011111010011 +110101000101 +011110011111 +101101110110 +100010010100 +101001111101 +100110001100 +110001100110 +110111001101 +010001111100 +001011100010 +011010100101 +101010101010 +001111010101 +011101110110 +111110110011 +010110000000 +110001010111 +100001110111 +010000001000 +001000001001 +100110111011 +011010001111 +111011101100 +110111100001 +001101110000 +011101100001 +101110011001 +101001011100 +111000000100 +110011010101 +111011101000 +000111111110 +111111000111 +100100010100 +101111100001 +110011000100 +100001011111 +110011100111 +011111100010 +100111101101 +111110101111 +011001011001 +100011001110 +001101010001 +011010101000 +011011011101 +011100010000 +010001001111 +011111100011 +101011000110 +101010000000 +110100101001 +000010001100 +110001000001 +000001001011 +000100000101 +001101010010 +111101001010 +011001001000 +110001101110 +101111111110 +000110110110 +111111110110 +101101000110 +101100011001 +111110011000 +001001010011 +000101000001 +111010100110 +001101101011 +001011011100 +100111111101 +000110000101 +100001011000 +110111010111 +000111111000 +011010011010 +000000100000 +011010110011 +001101100101 +111001110111 +011010011011 +110000100011 +100001001010 +111001100100 +001100000100 +010011001100 +011000111000 +001100100010 +100101001010 +111000111011 +010101110010 +001001101000 +000010101110 +000000100100 +110001110011 +110111100000 +010100000000 +100110110100 +111111101101 +010100111111 +000101011100 +100000100101 +101011111100 +001101000011 +001011110001 +011010100001 +110110011010 +010010100101 +100001110101 +100001101110 +100100001000 +100010101010 +010001000111 +111100110000 +101111001001 +011000001101 +011001100111 +100110011010 +111110101100 +111101110001 +010111010000 +101101011010 +110011001010 +010001010100 +010000110011 +011010111000 +101000010011 +111101110101 +011111010110 +000101000000 +001001010100 +001001010001 +111100101111 +100101100110 +000100010101 +100011101001 +111011011100 +101100010011 +010100000100 +001010100111 +011111000010 +001011011000 +101000101000 +011010010101 +101011011011 +111110100110 +001110011000 +001101011000 +110010101110 +111010111011 +111000000001 +110111010100 +100101010111 +100011011101 +111111011100 +100111100110 +001110001111 +001101100110 +100010101001 +110100100010 +110111100111 +110011101100 +010101010101 +110011111101 +011110101010 +000110110000 +011101110101 +000001010001 +001001000100 +001001001010 +010110001001 +011000101011 +111000100000 +111000001011 +011101100000 +010110011101 +100001101001 +110101101011 +011111110111 +100001010111 +110001000010 +101010101111 +011110010010 +000100100110 +000011100010 +010111001011 +100110011110 +100000001000 +000101110101 +001000110100 +101101010101 +111000101000 +101001100010 +101010011010 +001101010011 +010001001110 +110101100000 +101110000010 +111010010010 +111001100101 +111110101110 +110101010110 +000111100100 +110001001100 +100011010010 +100001101111 +011001000011 +000100001111 +101100101110 +001110101101 +000001001101 +110000000010 +110101110011 +111011110100 +011010101001 +111000000110 +110001000000 +100111101000 +001111100111 +011100011001 +011011110000 +001110110100 +001010111010 +010110111010 +011100100101 +101011111111 +011000111111 +010111000010 +111011001011 +101100001011 +011110011010 +000100000110 +001011001100 +111001000011 +011100111010 +011111000101 +001000011010 +100100000111 +000001011011 +011101101011 +010110101000 +011101010000 +010011111011 +100101011101 +010001110010 +111111110111 +010001011100 +011111010100 +101010100101 +001100100110 +100111110011 +011111001001 +010110100111 +011100110001 +110010101111 +100001000001 +000100011101 +001111010111 +101001111001 +000111100011 +000100001110 +011011011111 +000101001000 +100000110010 +010001010111 +111011110001 +001110110110 +101101010011 +001111011111 +100011101100 +110011101000 +000100000000 +011000100110 +100000000010 +110000011111 +001100111110 +001111101001 +010111001101 +111000100011 +100011011011 +110010000111 +011000010111 +100100110111 +000011101101 +001101000010 +000011001000 +101010101110 +011001110000 +100000100000 +111111000010 +110111010001 +001010100001 +000011110011 +110100111100 +100110101110 +000010111110 +111101101100 +101100011110 +010000000001 +111101110000 +001110011101 +001101110010 +011100000111 +001111000111 +101110000000 +010110011010 +110111111111 +000010001101 +000010101100 +111011000101 +000010001111 +110101011110 +011011000100 +110110111101 +110101010011 +000101010110 +001001100110 +100101110000 +101001110010 +000101001010 +010110101100 +011110001101 +010010010100 +111000111111 +001000010010 +011110111101 +010001000011 +000110101011 +001011110111 +101010100010 +101010100001 +000101001101 +100000100011 +111010110101 +111010011010 +010101010110 +001101111101 +011011111100 +110011111001 +101011101011 +101010011001 +101011011100 +111100100001 +010001001000 +111101101111 +111110010111 +010111000011 +011010111011 +111011010101 +011000000000 +110100101000 +001001010000 +101101000011 +110010011001 +110011110101 +100000100111 +111101101001 +000100011111 +111010110111 +101111001100 +001101110110 +101111111101 +101111001011 +101101100010 +110100010010 +010111110000 +010000110010 +101000001111 +011100000110 +110111011010 +000011110000 +110010000011 +011110101000 +100111000010 +010101000110 +011100101010 +000111110000 +010100111000 +010000011110 +110110110001 +100100011001 +000010101001 +011001010111 +000001111101 +110010000010 +110101011011 +001100001111 +001001011000 +001100110010 +000011100101 +111010010011 +101000010001 +111000000010 +001110101011 +011110101100 +110011111111 +110001110010 +111001000100 +011100001111 +100001110011 +000100110011 +011100010011 +000000100110 +101000000111 +110101111001 +011011111001 +011100110000 +101010100100 +000001111000 +110000000011 +010100100010 +001001100101 +110100000001 +000010001110 +110010000001 +100110001110 +001110000100 +011011011000 +010011001001 +001010000010 +010011001111 +100001011100 +001111010001 +101011101000 +111001111100 +110111011011 +101100111001 +101100110100 +000011000100 +100001011010 +011111001000 +011000110100 +001001101110 +100000101100 +100110000001 +110110110000 +111001000110 +100100111000 +101010101011 +100010000111 +101010110001 +110000111010 +101110110100 +101111111001 +111111101110 +010101100110 +010110001100 +100000011111 +011100110010 +110011010000 +100001110000 +001000100100 +110010110100 +011011110110 +011001101001 +011001001100 +000000001001 +011100001101 +111110001011 +001010100011 +000110101001 +000010101101 +101001010001 +110101101111 +011011100011 +001011000111 +110110010110 +010100011100 +110100000010 +001011111110 +100010110101 +011010110001 +111001101100 +111010011101 +100010110000 +100110110111 +101111000000 +110110000001 +111011000110 +101011110010 +111010100101 +010100110010 +101110111111 +100010110001 +110111101001 +010110110011 +111111011000 +000101010010 +011101000100 +000000100001 +001001001110 +001010110001 +111011100001 +000010000111 +101001001001 +100000010110 +011101101001 +111000111001 +111100110110 +010010010010 +110001110111 +010010011010 +111000110011 +101101111011 +110101001001 +011111000011 +010011000110 +011000110010 +001001100011 +001110111000 +000001010100 +110010010010 +001001100000 +001100000000 +011000111110 +000001010101 +000000011010 +110111011001 +110111110100 +001111001101 +000110100010 +011011100000 +011001010000 +101111001111 +011011011010 +100001000100 +010001100110 +001001011010 +001101111110 +101100000111 +100000111110 +011000100101 +100101110011 +111011011110 +110100001100 +101010111100 +001000110010 +100011001011 +101100001001 +100110101001 +110011110000 +100111100100 +100000101101 +111011010010 +101010001110 +001011011001 +111011010001 +100101011000 +001010010001 +111100011001 +000100100000 +000000000000 +010011001010 +011011011001 +000011110100 +001010000111 +000111111101 +100000110101 +110000101100 +000111101110 +110100100110 +110100110100 +100011010110 +011100101100 +011001011110 +000101100111 +110001010010 +010101000011 +101010111110 +011001010010 +010001110111 +111100001100 +101001100111 +001111001011 +111001110001 +100010001000 +111000111010 +101111100000 +101110010110 +111111101001 +010011110110 +001001101001 +101111100110 +111111001011 +101110110011 +001111001111 +010110011001 +110110001110 +010011011001 +101011100111 +001011110100 +011010010000 +100001000011 +100000111111 +010010111000 +000010100111 +100011110100 +111111010110 +101011010101 +011011010001 +000000110111 +110101011001 +010011100001 +110010111010 +100111101100 +001001100001 +110010110010 +101111111111 +111000111110 +111111111100 +111010110110 +110001111011 +000111001001 +001100010010 +010011000001 +100001111011 +011111010010 +001000011000 +000110000011 +100111001110 +110101101000 +110011010110 +110100010011 +010110000100 +001101000101 +100110011000 +011011100111 +000010010100 +000001011101 +110010100101 +100110000000 +001101110001 +011110111100 +001000000000 +100110010110 +111000010010 +000100010001 +110101010101 +100001010100 +000010000010 +110001000100 +000100011011 +010000100101 +010000100100 +111011000100 +111101001000 +100011110010 +110011011010 +110000011100 +001111100000 +110011100001 +001000010011 +111100101101 +111101001011 +010011101010 +111111010011 +110100001110 +100000001100 +000011011011 +101111110010 +100111101111 +100010000011 +011110000101 +111010101111 +000001101010 +000011010000 +101100100001 +101100110010 +100000110000 +101000101110 +011011100010 +110111110000 +010110001110 +101110001101 +000011101000 +011110000001 +111011111001 +110011101001 +010001010110 +000101100011 +101000101011 +001010101010 +110100011011 +010101110001 +000100101100 +111011100000 +011010000010 +100100001011 +101000110000 +101001111110 +111111001000 +011011000011 +011111000100 +111011100101 +010011000010 +011100111011 +010000100011 +101011111000 +001111100011 +011011100001 +100101111100 +000101111110 +100000110111 +000100110001 +010010110000 +110100100011 +000101011110 +000001110011 +111010001110 +110000010000 +010001010000 +001100101101 +111100000100 +100100100010 +011101110000 +000001010111 +101101001100 +000111011110 +100101010011 +110101000000 +000101011111 +101011010000 +101001001101 +101100111110 +111010101110 +011101101000 +001001000111 +011001011111 +100001100100 +101000100011 +001101110101 +101101000100 +110111011111 +111101110100 +111110011101 +100110101011 +111100111001 +110011110111 +001100101011 +101010111010 +011100100010 +000010001010 +010000100010 +010001100100 +000100000111 +010100100110 +101111001101 +010110100011 +110110010100 +101011000101 +110010111111 +111011100011 +111011101111 +010111101101 +111011001110 +111001000111 +000011000010 +110100110111 +111100101000 +011111100001 +100010110110 +001000101001 +010001010011 +111001100000 +101110111000 +001000011101 +011000100000 +100010100011 +000000111001 +000010011101 +011000101110 +010101101100 +010101001101 +011010010100 +000011110110 +111001011101 +001101011011 +010110101010 +001010101001 +010001010001 +110100101100 +110101110000 +000010111100 +000100000010 +111110111000 +110000100111 diff --git a/day3/src/main.rs b/day3/src/main.rs new file mode 100644 index 0000000..4904ad5 --- /dev/null +++ b/day3/src/main.rs @@ -0,0 +1,79 @@ +use std::{fs, env, vec}; + + +fn read_input(path: &str) -> String { + return fs::read_to_string(path).expect("Cannot read file."); +} + +fn parse_input(s: &str) -> Vec> { + let mut r: Vec> = vec![]; + for e in s.split("\n") { + if !e.is_empty() { + r.push(e.chars().filter_map(|e| e.to_digit(2)).collect()); + } + } + return r; +} + +fn bit_vec_to_u32(v: &Vec, invert: bool) -> u32 { + let mut acc = 0; + for i in 0..v.len() { + acc += if invert { (v[i] + 1) % 2 } else { v[i] } << (v.len() - 1 - i); + } + return acc; +} + +fn get_most_in_col(v: &Vec>, c: usize) -> u32 { + let mut a = (0, 0); + for r in 0..v.len() { + if v[r][c] == 0 { + a.0 += 1; + } else { + a.1 += 1; + } + } + if a.1 == a.0 { return 1; } + else { return if a.1 > a.0 { 1 } else { 0 }; } +} + +fn get_o2(v: &Vec>, c: usize) -> u32 { + if v.len() == 1 { return bit_vec_to_u32(&v[0], false); } + + let a = get_most_in_col(&v, c); + let f = v.into_iter().filter(|e| e[c] == (a + 1) % 2) + .fold(vec![], |mut acc, e| { acc.push(e.to_owned()); return acc;}); + return get_o2(&f, c + 1); +} + +fn get_co2(v: &Vec>, c: usize) -> u32 { + if v.len() == 1 { return bit_vec_to_u32(&v[0], false); } + + let a = get_most_in_col(&v, c); + let f = v.into_iter().filter(|e| e[c] == a) + .fold(vec![], |mut acc, e| { acc.push(e.to_owned()); return acc;}); + return get_co2(&f, c + 1); +} + +fn get_gamma_epsilon(v: &Vec>) -> (u32, u32) { + let mut acc: Vec = vec![]; + + for c in 0..v[0].len() { + acc.push(get_most_in_col(&v, c)); + } + return (bit_vec_to_u32(&acc, false), bit_vec_to_u32(&acc, true)); +} + + +fn main() { + let args: Vec = env::args().collect(); + for arg in args.iter().skip(1) { + let v = parse_input(&read_input(&arg)); + let t = get_gamma_epsilon(&v); + let o = get_o2(&v, 0); + let c = get_co2(&v, 0); + println!("[{}]", arg); + println!("\tPart 1 : {} x {} = {}", t.0, t.1, t.0 * t.1); + println!("\tPart 2 : {} x {} = {}", o, c, o * c); + } +} +