Compare commits

..

2 Commits

Author SHA1 Message Date
MasterofJOKers 6dfb583345 day03: Make it solve part 2 instead 2022-12-16 18:50:29 +01:00
MasterofJOKers 55038c87da day03: Add part 1 solution 2022-12-16 18:33:33 +01:00
3 changed files with 51 additions and 0 deletions

7
day03/Cargo.lock generated Normal file
View File

@ -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"

8
day03/Cargo.toml Normal file
View File

@ -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]

36
day03/src/main.rs Normal file
View File

@ -0,0 +1,36 @@
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<u32> {
HashSet::from_iter(chars.chars().map(|c| if c.is_uppercase() { c as u32 - 38 } else { c as u32 - 96 }))
}
fn main() -> Result<(), Box<dyn error::Error>> {
let f = File::open("input.txt")?;
let reader = BufReader::new(f);
let mut sum: u32 = 0;
let mut group_items: HashSet<u32> = HashSet::new();
for (i, line) in reader.lines().enumerate() {
let line = line?;
if group_items.is_empty() {
group_items = set_from_str(line.as_str());
} else {
group_items = set_from_str(line.as_str()).intersection(&group_items).cloned().collect();
}
if (i + 1) % 3 == 0 {
sum += group_items.iter().next().unwrap();
group_items.drain();
}
}
println!("{}", sum);
Ok(())
}