From 8b702bca9f79f12ccb73d7f8c1dff0ddef2df2b9 Mon Sep 17 00:00:00 2001 From: Daniel Seiller Date: Sun, 6 Dec 2020 16:06:16 +0100 Subject: [PATCH] Day 6 --- src/day6.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 44 insertions(+) create mode 100644 src/day6.rs diff --git a/src/day6.rs b/src/day6.rs new file mode 100644 index 0000000..489b6cb --- /dev/null +++ b/src/day6.rs @@ -0,0 +1,43 @@ +use aoc_runner_derive::{aoc, aoc_generator}; +use std::collections::HashSet; +#[aoc_generator(day6)] +pub fn input_generator(input: &str) -> Vec>> { + input + .split("\n\n") + .map(|block| { + block + .split("\n") + .map(|line| line.chars().collect()) + .collect() + }) + .collect() +} + +#[aoc(day6, part1)] +fn solve_part1(data: &Vec>>) -> usize { + let mut total = 0; + for group in data { + let mut answers: HashSet = HashSet::new(); + for person in group { + answers.extend(person); + } + total += answers.len(); + } + return total; +} + +#[aoc(day6, part2)] +fn solve_part2(data: &Vec>>) -> usize { + let mut total = 0; + for group in data { + let mut answers: HashSet = HashSet::new(); + for person in group { + answers.extend(person); + } + for person in group { + answers = answers.intersection(&person).map(|v| *v).collect(); + } + total += answers.len(); + } + return total; +} diff --git a/src/lib.rs b/src/lib.rs index 23a9869..349216a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,4 +4,5 @@ pub mod day2; pub mod day3; pub mod day4; pub mod day5; +pub mod day6; aoc_lib! { year = 2020 }