diff --git a/day03/Cargo.lock b/day03/Cargo.lock new file mode 100644 index 0000000..21c74cb --- /dev/null +++ b/day03/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day03" +version = "0.1.0" diff --git a/day03/Cargo.toml b/day03/Cargo.toml new file mode 100644 index 0000000..2804bab --- /dev/null +++ b/day03/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day03" +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/day03/src/main.rs b/day03/src/main.rs new file mode 100644 index 0000000..9d98577 --- /dev/null +++ b/day03/src/main.rs @@ -0,0 +1,30 @@ +use std::collections::HashSet; +use std::error; +use std::fs::File; +use std::io::BufRead; +use std::io::BufReader; + + +fn set_from_str(chars: &str) -> HashSet { + HashSet::from_iter(chars.chars().map(|c| if c.is_uppercase() { c as u32 - 38 } else { c as u32 - 96 })) +} + + +fn main() -> Result<(), Box> { + let f = File::open("input.txt")?; + let reader = BufReader::new(f); + + let mut sum: u32 = 0; + + for line in reader.lines() { + let line = line?; + let (c1, c2) = line.split_at(line.len() / 2); + let c1_set = set_from_str(c1); + let c2_set = set_from_str(c2); + sum += c1_set.intersection(&c2_set).sum::(); + } + + println!("{}", sum); + + Ok(()) +}