clean up part 2 a bit

This commit is contained in:
David 2022-12-03 09:04:06 -05:00
parent 51525f1e93
commit 894ddd8ca7

View File

@ -1,10 +1,10 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::collections::hash_map::RandomState;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::HashSet; use std::collections::HashSet;
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::io::BufRead; use std::io::BufRead;
use std::iter::FromIterator;
use std::path::Path; use std::path::Path;
use std::time::Instant; use std::time::Instant;
@ -64,10 +64,7 @@ fn part_two() {
for line in lines { for line in lines {
if let Ok(contents) = line { if let Ok(contents) = line {
// build the group // build the group
let mut knapsack = HashSet::new(); let knapsack = HashSet::from_iter(contents.chars());
for item in contents.chars() {
knapsack.insert(item);
}
group.push(knapsack); group.push(knapsack);
if group.len() != 3 { if group.len() != 3 {
continue; continue;
@ -75,14 +72,13 @@ fn part_two() {
if let (Some(one), Some(two), Some(three)) = if let (Some(one), Some(two), Some(three)) =
(group.get(0), group.get(1), group.get(2)) (group.get(0), group.get(1), group.get(2))
{ {
let intermediate_one: _ = two let intermediate_one: HashSet<&char> =
.intersection(three) HashSet::from_iter(two.intersection(three));
.collect::<HashSet<&char, RandomState>>(); let intermediate_two: HashSet<&char> =
let intermediate_two: _ = one HashSet::from_iter(one.intersection(three));
.intersection(three)
.collect::<HashSet<&char, RandomState>>();
let mut result = intermediate_one.intersection(&intermediate_two); let mut result = intermediate_one.intersection(&intermediate_two);
if let Some(item) = result.next() { if let Some(item) = result.next() {
// double pointers!
if **item as u32 > 96 { if **item as u32 > 96 {
priorities += **item as u32 - 96 priorities += **item as u32 - 96
} else { } else {