2020: day06: part 1
This commit is contained in:
parent
de9b94c9a6
commit
bc0ff1b150
|
@ -5,6 +5,7 @@ use aoc2020::day02;
|
|||
use aoc2020::day03;
|
||||
use aoc2020::day04;
|
||||
use aoc2020::day05;
|
||||
use aoc2020::day06;
|
||||
|
||||
fn aoc2020_all(c: &mut Criterion) {
|
||||
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("day04", |b| b.iter(|| day04::run().unwrap()));
|
||||
c.bench_function("day05", |b| b.iter(|| day05::run().unwrap()));
|
||||
c.bench_function("day06", |b| b.iter(|| day06::run().unwrap()));
|
||||
}
|
||||
|
||||
criterion_group! {
|
||||
name = all_days;
|
||||
config = Criterion::default().sample_size(30);
|
||||
config = Criterion::default().sample_size(200);
|
||||
targets = aoc2020_all
|
||||
}
|
||||
criterion_main!(all_days);
|
||||
|
|
2131
aoc2020/input/day06.txt
Normal file
2131
aoc2020/input/day06.txt
Normal file
File diff suppressed because it is too large
Load diff
15
aoc2020/input/day06_provided.txt
Normal file
15
aoc2020/input/day06_provided.txt
Normal file
|
@ -0,0 +1,15 @@
|
|||
abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b
|
71
aoc2020/src/day06.rs
Normal file
71
aoc2020/src/day06.rs
Normal 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);
|
||||
}
|
||||
}
|
|
@ -3,3 +3,4 @@ pub mod day02;
|
|||
pub mod day03;
|
||||
pub mod day04;
|
||||
pub mod day05;
|
||||
pub mod day06;
|
||||
|
|
|
@ -6,9 +6,17 @@ use aoc2020::day02;
|
|||
use aoc2020::day03;
|
||||
use aoc2020::day04;
|
||||
use aoc2020::day05;
|
||||
use aoc2020::day06;
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue