AdventOfCode2020/src/day6.rs

72 lines
1.2 KiB
Rust
Raw Normal View History

2023-12-27 15:29:51 -06:00
use std::collections::HashSet;
use aoc_runner_derive::{aoc, aoc_generator};
use itertools::Itertools;
#[aoc_generator(day6, part1)]
fn parse(input: &str) -> Vec<usize> {
input.split("\n\n")
.map(|group| {
group.chars()
.filter(|char| !char.is_ascii_whitespace())
.collect::<HashSet<_>>().len()
}).collect_vec()
}
#[aoc(day6, part1)]
fn part1(input: &Vec<usize>) -> usize {
input.iter().sum()
}
#[aoc_generator(day6, part2)]
fn parse2(input: &str) -> Vec<usize> {
input.split("\n\n")
.map(|group| {
let people = group.lines().collect_vec();
let mut first = people[0].chars().collect_vec();
for person in &people[1..] {
first = first.iter()
.cloned()
.filter(|question| person.contains(*question))
.collect_vec()
}
first.len()
}).collect_vec()
}
#[aoc(day6, part2)]
fn part2(input: &Vec<usize>) -> usize {
input.iter().sum()
}
#[cfg(test)]
mod tests {
use super::*;
const EX: &str = r"abc
a
b
c
ab
ac
a
a
a
a
b";
#[test]
fn part1_example() {
assert_eq!(part1(&parse(EX)), 11);
}
#[test]
fn part2_example() {
assert_eq!(part2(&parse2(EX)), 6);
}
}