2020: day06: part 1

This commit is contained in:
Antoine Martin 2020-12-06 12:43:48 +01:00
parent de9b94c9a6
commit bc0ff1b150
6 changed files with 2230 additions and 2 deletions

View file

@ -5,6 +5,7 @@ use aoc2020::day02;
use aoc2020::day03; use aoc2020::day03;
use aoc2020::day04; use aoc2020::day04;
use aoc2020::day05; use aoc2020::day05;
use aoc2020::day06;
fn aoc2020_all(c: &mut Criterion) { fn aoc2020_all(c: &mut Criterion) {
c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); c.bench_function("day01", |b| b.iter(|| day01::run().unwrap()));
@ -12,11 +13,12 @@ fn aoc2020_all(c: &mut Criterion) {
c.bench_function("day03", |b| b.iter(|| day03::run().unwrap())); c.bench_function("day03", |b| b.iter(|| day03::run().unwrap()));
c.bench_function("day04", |b| b.iter(|| day04::run().unwrap())); c.bench_function("day04", |b| b.iter(|| day04::run().unwrap()));
c.bench_function("day05", |b| b.iter(|| day05::run().unwrap())); c.bench_function("day05", |b| b.iter(|| day05::run().unwrap()));
c.bench_function("day06", |b| b.iter(|| day06::run().unwrap()));
} }
criterion_group! { criterion_group! {
name = all_days; name = all_days;
config = Criterion::default().sample_size(30); config = Criterion::default().sample_size(200);
targets = aoc2020_all targets = aoc2020_all
} }
criterion_main!(all_days); criterion_main!(all_days);

2131
aoc2020/input/day06.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,15 @@
abc
a
b
c
ab
ac
a
a
a
a
b

71
aoc2020/src/day06.rs Normal file
View file

@ -0,0 +1,71 @@
use std::collections::HashSet;
use std::fmt::Write;
const INPUT: &str = include_str!("../input/day06.txt");
pub fn run() -> aoc::Result<String> {
let mut res = String::with_capacity(128);
writeln!(res, "part 1: {}", part1(INPUT)?)?;
Ok(res)
}
fn get_groups(input: &str) -> aoc::Result<Vec<Group>> {
let mut groups = Vec::new();
let mut answers = Vec::new();
for line in input.lines() {
if line.is_empty() {
groups.push(Group {
answers: answers.clone(),
});
answers.clear();
} else {
let person_answers = line.bytes().collect();
answers.push(person_answers);
}
}
if !answers.is_empty() {
groups.push(Group { answers });
}
Ok(groups)
}
fn part1(input: &str) -> aoc::Result<usize> {
let groups = get_groups(input)?;
Ok(groups
.iter()
.map(|group| {
group.answers.iter().fold(HashSet::new(), |set, answers| {
set.union(answers).copied().collect()
})
})
.map(|set| set.len())
.sum())
}
struct Group {
answers: Vec<HashSet<u8>>,
}
#[cfg(test)]
mod tests {
use super::*;
static PROVIDED: &'static str = include_str!("../input/day06_provided.txt");
#[test]
fn part1_provided() {
assert_eq!(part1(PROVIDED).unwrap(), 11);
}
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 6382);
}
}

View file

@ -3,3 +3,4 @@ pub mod day02;
pub mod day03; pub mod day03;
pub mod day04; pub mod day04;
pub mod day05; pub mod day05;
pub mod day06;

View file

@ -6,9 +6,17 @@ use aoc2020::day02;
use aoc2020::day03; use aoc2020::day03;
use aoc2020::day04; use aoc2020::day04;
use aoc2020::day05; use aoc2020::day05;
use aoc2020::day06;
fn main() -> Result<()> { fn main() -> Result<()> {
let days: &[DayFunc] = &[day01::run, day02::run, day03::run, day04::run, day05::run]; let days: &[DayFunc] = &[
day01::run,
day02::run,
day03::run,
day04::run,
day05::run,
day06::run,
];
aoc::run(days) aoc::run(days)
} }