From 2f86fd2decd64b344c3b70573e12a07639a7c3e0 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sun, 6 Dec 2020 02:25:55 -0800 Subject: [PATCH] problem 6 --- src/main.rs | 12 ++++++++--- src/problem6/lib.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++ src/problem6/mod.rs | 1 + 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/problem6/lib.rs create mode 100644 src/problem6/mod.rs diff --git a/src/main.rs b/src/main.rs index 520da7e..595cd10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,14 @@ use crate::problem2::lib::Problem2; use crate::problem3::lib::Problem3; use crate::problem4::lib::Problem4; use crate::problem5::lib::Problem5; +use crate::problem6::lib::Problem6; mod problem1; mod problem2; mod problem3; mod problem4; mod problem5; +mod problem6; mod util; @@ -41,7 +43,11 @@ fn main() { // problem4.run_part1(); // problem4.run_part2(); - let problem5 = Problem5::new(&util::get_problem(5)); - problem5.run_part1(); - problem5.run_part2(); + // let problem5 = Problem5::new(&util::get_problem(5)); + // problem5.run_part1(); + // problem5.run_part2(); + + let problem6 = Problem6::new(&util::get_problem(6)); + problem6.run_part1(); + problem6.run_part2(); } diff --git a/src/problem6/lib.rs b/src/problem6/lib.rs new file mode 100644 index 0000000..dbd5d08 --- /dev/null +++ b/src/problem6/lib.rs @@ -0,0 +1,52 @@ +/* +Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together. +*/ + +use crate::Problem; +use std::collections::HashSet; + +pub struct Problem6 { + groups: Vec>, +} + + +impl Problem6 {} + +impl Problem for Problem6 { + fn new(input: &String) -> Self { + Problem6 { + groups: input + .split("\n\n") + .filter_map(|s| { + let s = s.trim().split("\n"); + let mut sets = Vec::new(); + for i in s { + let v : HashSet = i.chars().filter_map(|c| { + if c.is_whitespace() { + None + } else { + Some(c) + } + }).collect::>(); + sets.push(v); + } + let mut iter = sets.iter(); + let intersection : HashSet = + iter.next().map(|set| iter.fold(set.clone(), |set1, set2| set1.intersection(set2).map(|c| *c).collect())).unwrap().clone(); + Some(intersection) + }).collect(), + } + } + + fn run_part1(&self) { + let mut v = 0; + for i in &self.groups { + v += i.len(); + } + println!("{}\n{:?}", v, self.groups); + } + + fn run_part2(&self) { + } +} + diff --git a/src/problem6/mod.rs b/src/problem6/mod.rs new file mode 100644 index 0000000..965f28e --- /dev/null +++ b/src/problem6/mod.rs @@ -0,0 +1 @@ +pub mod lib;