From ef7e8df2825480f34c1034015a8221c09f6ebdf6 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Sun, 4 Dec 2022 14:19:12 +0300 Subject: 2022, day3: done --- 2022/day3/part1/Cargo.toml | 9 +++++++++ 2022/day3/part1/src/main.rs | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 2022/day3/part1/Cargo.toml create mode 100644 2022/day3/part1/src/main.rs (limited to '2022/day3/part1') diff --git a/2022/day3/part1/Cargo.toml b/2022/day3/part1/Cargo.toml new file mode 100644 index 0000000..f14fe90 --- /dev/null +++ b/2022/day3/part1/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "part1" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.10.5" diff --git a/2022/day3/part1/src/main.rs b/2022/day3/part1/src/main.rs new file mode 100644 index 0000000..2de2ce8 --- /dev/null +++ b/2022/day3/part1/src/main.rs @@ -0,0 +1,24 @@ +use std::collections::HashSet; + +fn main() { + let rucksacks: Vec<(&str, &str)> = include_str!("../../input") + .lines() + .map(|line| line.split_at(line.len() / 2)) + .collect(); + + let mut sum = 0; + for (f, s) in rucksacks { + let first: HashSet = HashSet::from_iter(f.chars()); + let second: HashSet = HashSet::from_iter(s.chars()); + + for x in first.intersection(&second) { + if x.is_lowercase() { + sum += *x as i16 - 96; + } else { + sum += *x as i16 - 38; + } + } + } + + println!("{}", sum); +} -- cgit v1.2.3-70-g09d2