diff options
Diffstat (limited to '2022/day3/part2')
| -rw-r--r-- | 2022/day3/part2/Cargo.toml | 9 | ||||
| -rw-r--r-- | 2022/day3/part2/src/main.rs | 31 |
2 files changed, 40 insertions, 0 deletions
diff --git a/2022/day3/part2/Cargo.toml b/2022/day3/part2/Cargo.toml new file mode 100644 index 0000000..69fa504 --- /dev/null +++ b/2022/day3/part2/Cargo.toml | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | [package] | ||
| 2 | name = "part2" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | |||
| 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
| 7 | |||
| 8 | [dependencies] | ||
| 9 | itertools = "0.10.5" | ||
diff --git a/2022/day3/part2/src/main.rs b/2022/day3/part2/src/main.rs new file mode 100644 index 0000000..7139eb0 --- /dev/null +++ b/2022/day3/part2/src/main.rs | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | use itertools::Itertools; | ||
| 2 | use std::collections::HashMap; | ||
| 3 | use std::collections::HashSet; | ||
| 4 | |||
| 5 | fn main() { | ||
| 6 | let mut sum = 0; | ||
| 7 | for group in &include_str!("../../input").lines().chunks(3) { | ||
| 8 | let mut membership: HashMap<char, usize> = HashMap::new(); | ||
| 9 | for rucksack in group { | ||
| 10 | let mut items: HashSet<char> = HashSet::new(); | ||
| 11 | for item in &rucksack.chars().chunks(2) { | ||
| 12 | for c in item { | ||
| 13 | items.insert(c); | ||
| 14 | } | ||
| 15 | } | ||
| 16 | for c in &items { | ||
| 17 | *membership.entry(c.to_owned()).or_default() += 1; | ||
| 18 | } | ||
| 19 | } | ||
| 20 | for (item, value) in membership { | ||
| 21 | if value == 3 { | ||
| 22 | if item.is_lowercase() { | ||
| 23 | sum += item as i16 - 96; | ||
| 24 | } else { | ||
| 25 | sum += item as i16 - 38; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | } | ||
| 29 | } | ||
| 30 | println!("{}", sum); | ||
| 31 | } | ||
