day6
This commit is contained in:
parent
45d84ad89b
commit
59a21265c6
72
src/day6.rs
Normal file
72
src/day6.rs
Normal file
@ -0,0 +1,72 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
mod day6;
|
||||
mod day5;
|
||||
mod day1;
|
||||
mod day4;
|
||||
|
Loading…
Reference in New Issue
Block a user