AoC_2020/src/day06.rs

44 lines
1.1 KiB
Rust
Raw Normal View History

2020-12-06 15:06:16 +00:00
use aoc_runner_derive::{aoc, aoc_generator};
use std::collections::HashSet;
#[aoc_generator(day6)]
pub fn input_generator(input: &str) -> Vec<Vec<HashSet<char>>> {
input
.split("\n\n")
.map(|block| {
block
.split("\n")
.map(|line| line.chars().collect())
.collect()
})
.collect()
}
#[aoc(day6, part1)]
fn solve_part1(data: &Vec<Vec<HashSet<char>>>) -> usize {
let mut total = 0;
for group in data {
let mut answers: HashSet<char> = HashSet::new();
for person in group {
answers.extend(person);
}
total += answers.len();
}
return total;
}
#[aoc(day6, part2)]
fn solve_part2(data: &Vec<Vec<HashSet<char>>>) -> usize {
let mut total = 0;
for group in data {
let mut answers: HashSet<char> = 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;
}