From 894ddd8ca759bf8fbb07608108cb3dc40f853c9c Mon Sep 17 00:00:00 2001 From: David Ashby Date: Sat, 3 Dec 2022 09:04:06 -0500 Subject: [PATCH] clean up part 2 a bit --- 03/main.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/03/main.rs b/03/main.rs index 77d6b8b..7d1c1bb 100644 --- a/03/main.rs +++ b/03/main.rs @@ -1,10 +1,10 @@ #![allow(dead_code)] -use std::collections::hash_map::RandomState; use std::collections::HashMap; use std::collections::HashSet; use std::fs::File; use std::io; use std::io::BufRead; +use std::iter::FromIterator; use std::path::Path; use std::time::Instant; @@ -64,10 +64,7 @@ fn part_two() { for line in lines { if let Ok(contents) = line { // build the group - let mut knapsack = HashSet::new(); - for item in contents.chars() { - knapsack.insert(item); - } + let knapsack = HashSet::from_iter(contents.chars()); group.push(knapsack); if group.len() != 3 { continue; @@ -75,14 +72,13 @@ fn part_two() { if let (Some(one), Some(two), Some(three)) = (group.get(0), group.get(1), group.get(2)) { - let intermediate_one: _ = two - .intersection(three) - .collect::>(); - let intermediate_two: _ = one - .intersection(three) - .collect::>(); + let intermediate_one: HashSet<&char> = + HashSet::from_iter(two.intersection(three)); + let intermediate_two: HashSet<&char> = + HashSet::from_iter(one.intersection(three)); let mut result = intermediate_one.intersection(&intermediate_two); if let Some(item) = result.next() { + // double pointers! if **item as u32 > 96 { priorities += **item as u32 - 96 } else {