diff --git a/day3/.gitignore b/day3/.gitignore new file mode 100644 index 0000000..a6f89c2 --- /dev/null +++ b/day3/.gitignore @@ -0,0 +1 @@ +/target/ \ No newline at end of file diff --git a/day3/Cargo.lock b/day3/Cargo.lock new file mode 100644 index 0000000..4104d70 --- /dev/null +++ b/day3/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day3" +version = "0.1.0" diff --git a/day3/Cargo.toml b/day3/Cargo.toml new file mode 100644 index 0000000..87951b8 --- /dev/null +++ b/day3/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day3" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day3/input.txt b/day3/input.txt new file mode 100644 index 0000000..40073fe --- /dev/null +++ b/day3/input.txt @@ -0,0 +1,1000 @@ +111011110101 +011000111010 +100000010010 +000111100110 +110011111011 +001100010111 +011000100100 +110011111010 +101011010111 +010001001011 +001011011010 +110001000110 +011101111111 +100101010010 +011010010001 +010111011110 +001101101001 +100000100101 +011010100101 +010011010011 +100000000000 +001010001000 +101100110001 +011010000111 +011011101101 +101000011111 +100010110000 +101011000010 +010001100100 +100100011001 +111000101110 +101010111010 +000111011101 +011101110101 +000010101101 +110110000000 +111110100101 +100110101100 +000001100001 +101001110010 +011101100001 +110111011101 +000001111001 +000110110000 +111001110110 +101011110101 +010010011000 +101011110011 +011101101110 +100010000110 +001000010110 +110000000100 +010111101101 +100000001110 +111010100101 +101000000110 +100001101111 +000010011010 +111001000100 +000100011101 +000101101001 +100111110011 +111011010010 +110111001010 +100111111100 +011011110101 +011001111111 +111010101101 +011111010000 +100010010000 +111110111110 +000111101100 +100001011101 +000100001010 +111110101110 +001000011010 +010100010011 +101111101011 +001001001101 +111111100101 +000101110011 +100001000000 +100001111011 +001001000111 +101101001101 +010011111011 +111001001110 +000100010100 +000101100001 +111011000100 +010101111010 +110110100101 +001011000101 +100110000101 +001110100100 +100111110111 +011100001101 +001001111110 +000001011011 +101001011011 +010111101011 +101001011101 +011101110011 +111110100100 +101100010010 +110001000100 +001101000100 +000110011011 +100111101111 +010100000110 +100101001011 +101101000000 +110000111100 +010011110100 +110101000110 +011111010101 +100101010100 +111111010000 +011000010101 +000001010001 +100100100100 +101101101111 +011011000111 +100101101011 +101001001101 +010110111000 +000111111000 +110010100001 +011011011011 +111001111111 +101010010010 +011110101000 +001110110100 +111011010111 +001011010011 +101000010100 +100001000001 +000001111101 +011000000101 +101010101111 +100011100010 +010010100111 +011111000010 +110000111010 +010111110110 +101000001100 +001111001010 +000000100110 +100000101100 +010001011011 +000000110011 +011101100110 +000011101110 +011110011010 +100000110110 +010011011010 +001000000100 +001001101011 +000101101111 +011000110011 +101001000101 +011010101100 +010010111111 +110010100101 +011100101011 +011110111000 +001000101001 +010011111001 +001111001000 +111010000110 +000010000011 +100101011010 +100010001010 +010111010000 +000000111101 +001000110101 +001101010000 +100000010101 +111000010000 +001100010010 +000110101100 +001111110111 +100111010100 +000110001001 +101010111000 +110111101011 +110000010101 +011000101011 +010111001111 +011000001101 +011011011100 +011011000110 +011110111100 +010110010011 +000100110101 +101100111101 +111001100001 +001111010011 +110100010110 +111010000011 +000011110000 +010010111010 +100010101010 +110010101011 +001100001010 +010011001011 +110011000001 +010011101001 +010101000111 +001011000001 +001110101001 +011111110101 +000011100000 +001001000011 +010111110000 +011001000000 +111111011001 +111010001100 +011011001001 +111001101100 +111001111011 +111100111110 +100110111011 +011011110100 +111011100001 +000111110101 +011100001010 +001110110101 +101110011010 +100010100000 +100100111011 +110000110010 +001000101010 +110001100011 +001101100100 +000101010000 +011110110001 +100100110101 +111101111100 +011011111001 +111101000000 +000100111000 +101100100001 +111100001011 +000111110110 +100101001010 +100000111100 +100100000000 +011001011101 +111111100100 +111101010110 +011110001110 +100101110010 +101110011101 +110001100111 +001110011011 +001111111100 +001101111111 +011000100010 +010001110111 +010010110111 +001001001000 +010010010001 +100111010011 +000011110100 +110000110011 +100001101101 +001111110010 +100111110100 +111010111011 +101011101001 +000100100000 +111010110100 +011111010011 +000101011111 +110011110111 +110101001110 +101111111010 +000110111111 +101011010100 +100011000000 +101010001001 +001110100010 +110100111101 +001100011100 +100111001000 +001010000110 +000001100110 +001000101110 +010011101010 +110001100000 +011011111101 +110110001101 +010101011010 +010010001101 +010101111000 +001110001011 +001001100000 +101110110011 +000011011011 +001010110101 +101101011100 +101011100011 +010100010100 +011111010100 +100110101110 +001100100100 +001100111100 +000000100111 +101011001110 +010010100101 +010100001111 +000010111101 +000111101101 +100010000010 +110101100100 +111100101100 +101101110000 +110000011100 +101111100111 +010111100000 +010100001101 +000110111001 +010010011100 +110000110100 +011000000001 +010100100110 +001111011101 +111101111010 +011011101111 +010011001000 +110010101000 +101001000111 +101100000111 +111000110011 +101101010011 +110000111000 +000010000000 +100010101111 +000111101001 +000100110000 +110011011101 +110110010110 +110100110100 +011110111101 +000001011100 +101010100000 +010100101000 +001111001100 +101010000010 +111000100110 +111101101111 +001000010111 +110000011101 +110101010010 +110101110100 +101001001011 +001001101000 +010001111110 +010011111000 +101010110001 +101110010101 +111011011110 +111011111000 +110000110101 +011110101010 +110011110110 +110001001111 +110001111011 +000011010011 +010001001001 +001111110001 +101000111101 +101010110010 +010100101100 +101000100100 +001110100101 +011110110110 +101001111011 +111100000010 +011010100011 +000011000001 +010101110111 +000010100110 +111110010101 +000110100100 +010111001110 +111100111101 +001001011001 +000111010011 +010100011011 +011111100101 +110111010111 +101111000001 +101010010001 +001111110101 +111111111001 +011010110111 +000110011000 +010010111100 +101011110100 +110010101001 +111011001101 +110010001001 +010100000000 +011101010001 +011100111101 +000111010100 +111111001100 +011100101100 +110100000011 +100011101100 +101101001011 +100111100001 +011010111001 +101010111100 +000011111001 +101010100110 +000110111011 +101100011011 +001100111101 +100110001010 +001011011111 +110111011011 +100011001001 +010000101100 +000111101000 +101011110000 +001111001011 +111010000101 +101010011010 +000100010101 +110100101001 +100010111010 +100011111111 +010010011111 +101010010111 +001000101100 +000000111100 +101000001010 +100010010011 +110011001111 +010001101111 +000000000010 +111100110100 +100001001010 +100101101010 +101100000001 +000110100001 +111101110010 +001100111011 +001101001001 +001010010000 +000001001111 +100101001100 +110010010001 +011010010010 +101100011010 +001101111101 +101111101010 +110111101110 +001000011000 +101100010110 +010001100111 +001011000010 +000010110000 +111100000100 +111100100111 +100101100110 +111101000100 +101110010100 +101011010010 +100111110000 +111101100000 +110111110011 +000000100000 +011011110111 +100110101111 +010011111110 +011111101001 +101001100000 +011000010010 +101001000011 +001001010011 +000010001100 +000110101000 +101110100011 +010001001010 +000110011111 +111001101110 +100011101110 +101001110001 +101001110100 +001001000001 +101100110011 +001111100101 +101111010101 +001111001001 +110101100011 +000000011001 +100100011010 +011011101000 +000011011000 +101011100010 +101110101001 +000110101010 +011000000100 +001111010001 +110001110010 +001101101110 +000100111101 +001000001001 +000111111100 +001110111010 +101000011110 +100001101100 +110101000111 +000011010101 +101110010010 +111011000000 +100100111111 +111100000001 +000100011001 +100001100010 +110011100001 +100100101010 +100100110000 +011000110100 +101001110000 +101111100110 +100011101001 +110100111001 +100011111110 +000001001110 +000110100111 +001000100100 +101011111110 +011100100111 +100101111111 +001011111110 +000010101100 +110100011101 +000100101010 +111001011101 +011010000000 +011011010100 +001001100110 +100000100111 +011000011010 +010000100100 +011010110011 +001011100001 +010111101111 +010100110000 +001111101101 +111001101001 +001001100011 +000000100011 +110010000001 +110000110110 +010011100001 +001000011110 +110001110100 +111111011111 +010000111001 +001101001011 +000111110011 +011110011101 +111001010011 +000010111111 +111000110111 +010100010101 +010100111011 +110001110011 +110100010000 +011110111110 +010111010011 +100001001011 +011010110101 +001111110110 +111011110000 +001110111100 +001101010111 +010110011001 +111000000001 +101011000110 +010110111001 +101010110101 +010010101001 +001110011110 +010100000101 +010011010100 +011110110101 +100000001000 +011011100111 +000101100000 +101001101000 +001101010010 +101110101011 +000110100011 +100110001100 +001010110111 +010000100111 +100100110111 +101000111001 +101011001101 +011110101101 +110010011000 +011100101111 +111101111101 +101001001000 +111010100001 +011011100101 +100110011101 +011100000011 +011110001001 +011111000011 +011100011001 +010100011010 +111010101111 +101100100101 +110110101111 +001011100110 +001110111011 +010010001011 +111001010010 +001011011101 +110100010011 +010110001000 +001110100111 +110011011110 +001011111000 +011111111011 +100111111000 +110000010011 +010011011100 +010111001011 +010110100111 +011010000110 +001111100000 +000010110110 +101011001100 +001100011001 +010010011001 +100010101001 +110100111000 +111010010010 +101110110000 +110100001010 +001010000011 +110111111001 +010100010110 +000110110001 +100101100010 +111101101001 +011001010110 +011100010010 +000111100000 +110000010001 +001010001101 +001001011101 +011101101000 +000101111101 +101101001000 +111010010001 +001010011010 +110010111100 +100111101101 +011111110100 +000110010011 +000011110111 +110111100011 +111111100010 +101101100010 +000111100011 +011010000101 +101010010101 +010101101000 +001001101100 +111000011001 +100100001111 +000001011110 +000101000000 +011011011000 +101111010100 +010101010010 +011001001001 +011100010001 +101101110011 +101110111101 +111011010000 +110000011000 +011111110001 +101111010000 +100101101101 +011001111101 +100101111100 +001110001110 +011001101001 +111101110101 +111100110011 +011101010011 +111111101010 +000011100111 +110110111100 +001010100110 +001011101001 +101001111001 +111101001010 +010110001111 +010100001110 +001110100011 +111111000000 +010110011101 +000110010010 +001011001111 +010111110001 +100100000111 +100110010110 +110100001101 +100001001100 +000010110111 +010000110001 +101110101000 +010000010010 +001001101001 +010110011011 +110101000100 +000000101011 +101001111100 +110100110110 +100001100100 +111010110000 +101110010001 +000000000110 +000100010111 +011010100100 +110111001000 +000000001111 +111111011101 +010100010111 +110010100110 +100100110110 +001101100101 +101010100111 +001110000100 +001010011101 +011110001011 +000101100110 +011000011101 +011110011000 +001011000100 +110001100100 +001111011000 +101000001101 +110111000010 +110010101101 +110001001101 +000011111000 +111110011110 +000010100111 +111111011110 +110010001010 +000101100111 +101000110001 +011111011010 +010111110010 +011001100011 +110011001010 +010011001101 +110101010111 +011101001110 +010011000110 +110000001100 +100001111111 +010101001011 +001100101000 +101010011101 +000011101010 +100011011110 +111001111110 +100110001111 +100101011101 +011111000100 +000010101010 +110010010100 +001110000010 +100100000100 +101011011110 +100111001111 +100101011011 +110001001110 +001101011110 +010001010001 +000110100000 +001000001100 +100111000100 +100010000000 +101111111111 +010011101111 +101011100100 +010111011111 +110011011010 +111101000110 +010000001010 +100110100100 +011000011100 +100011011001 +101000001111 +100001100101 +111110111010 +110101010100 +000110110111 +001110000011 +101110101101 +000101011100 +010011000101 +010101000100 +000001001001 +111011101110 +001010011011 +000001000010 +111110001011 +110101111111 +010000010001 +001100000011 +101101000011 +110100001001 +111100010011 +000000001100 +100000101010 +001011101111 +011111101000 +111011001000 +010011101101 +100101010011 +101000010110 +011100000101 +011101001000 +111000001011 +100001011010 +001000111011 +001100001000 +001011010000 +001000110100 +010011100010 +000111010101 +000000010011 +000011001000 +101100110101 +100110110100 +001100100000 +011100000010 +010101111110 +100100100001 +110111111000 +110110011110 +000101010010 +010011110010 +000000111000 +111000001000 +000110001000 +101010110110 +010111111111 +011100101101 +010100000111 +100010011011 +110010001011 +110011110010 +010101110110 +110000011110 +101010011001 +100100110010 +110100010111 +100001010101 +111010010011 +011100001110 +111101110011 +001011010100 +000001000001 +010010101011 +110000101110 +111100011000 +100001011111 +010011010110 +100001000100 +010001110000 +011011001000 +101101010001 +011101101101 +011001010010 +111010010111 +100110011001 +111101010111 +111110111011 +000011101001 +001010111001 +000000011111 +000000000000 +100100000001 +011100011010 +111101010101 +001001001100 +100010110111 +110111010010 +001110101010 +111010101000 +100011100001 +000000111011 +011000010110 +010000111011 +111110110000 +100001100000 +001110000111 +100000100110 +101000110000 +000110001011 +111111101101 +010100101111 +100111000010 +010110110111 +101010111111 +000110111101 +000100000100 +011010001111 +110010111001 +101001000001 +010010011010 +110011111110 +011000010001 +110110010100 +110110111110 +101100010001 +010101011000 +011000111111 +010110010000 +101100111001 +000111001000 +010010001001 +101110100101 +001110010110 +101011011011 +111111110100 +010100101101 +001110110010 +010101001010 +110011010010 +011011011010 +100110010111 +011111101011 +110010110100 +100100111110 +100100000010 +010000111100 +001111101011 +010101101001 +110100001011 +110101010110 +010111000110 +011101100011 +001011100111 +011001100010 +101001110011 +001100111010 +101000011001 +110101111100 +110011001110 +100001010111 +111111000010 +010110010010 +101000000101 +000010010001 +001100110100 +100010011111 +100111111101 +111111000101 +010000110100 +000001101010 +011000001010 +010000111111 +000010001001 +111110100000 +110101010000 +001011011011 +011011100011 +001011110001 +010110111100 +100011010001 +011010001110 +110110111000 +011011110001 +110100100000 +111011001001 +101001100110 +010010110101 +111111111110 +100001100110 +001110001101 +100111100101 +001101100001 \ No newline at end of file diff --git a/day3/src/main.rs b/day3/src/main.rs new file mode 100644 index 0000000..c3ec348 --- /dev/null +++ b/day3/src/main.rs @@ -0,0 +1,58 @@ +use std::{ + fs::File, + io::{BufRead, BufReader, Lines, Result}, + path::Path, +}; + +static INPUT_PATH: &str = "input.txt"; + +fn main() -> Result<()> { + let diagnostic_report = read_diagnostic_report(INPUT_PATH)?; + let [gamma_rate, epsilon_rate] = extract_report_rates(&diagnostic_report); + println!("First puzzle: {}", gamma_rate * epsilon_rate); + Ok(()) +} + +fn extract_report_rates(report: &[String]) -> [i32; 2] { + let sums: Vec = report.iter().fold(vec![], |sums, curr| { + curr.chars() + .enumerate() + .map(|(index, char)| { + let value = if char == '0' { -1 } else { 1 }; + sums.get(index).map_or(value, |sum| sum + value) + }) + .collect() + }); + let gamma_rate: String = sums + .iter() + .map(|&sum| if sum > 0 { "1" } else { "0" }) + .collect::>() + .join(""); + let epsilon_rate: String = sums + .iter() + .map(|&sum| if sum > 0 { "0" } else { "1" }) + .collect::>() + .join(""); + [ + binary_str_to_int(&gamma_rate), + binary_str_to_int(&epsilon_rate), + ] +} + +fn read_diagnostic_report(file_path: &str) -> Result> { + read_lines(file_path).map(|lines| lines.filter_map(|line| line.ok()).collect()) +} + +fn binary_str_to_int(binary_str: &str) -> i32 { + i32::from_str_radix(binary_str, 2).unwrap() +} + +// The output is wrapped in a Result to allow matching on errors +// Returns an Iterator to the Reader of the lines of the file. +fn read_lines

(filename: P) -> Result>> +where + P: AsRef, +{ + let file = File::open(filename)?; + Ok(BufReader::new(file).lines()) +} diff --git a/day3/test.txt b/day3/test.txt new file mode 100644 index 0000000..665fd57 --- /dev/null +++ b/day3/test.txt @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 \ No newline at end of file