1
Fork 0
advent-of-code/source/year_2020/day_06/mod.rs

52 lines
1.0 KiB
Rust
Raw Permalink Normal View History

2024-01-14 21:04:10 +00:00
//! Day 06 of 2020.
2022-10-03 16:02:40 +00:00
use crate::prelude::*;
2024-01-14 21:04:10 +00:00
/// Get the solution for day 06 of 2020.
2022-10-03 16:02:40 +00:00
pub fn solution() -> Solution {
Solution::new(Day::new(6, 2020), part_1, part_2).with_expected(6437, 3229)
}
2024-01-14 21:04:10 +00:00
/// The logic to solve part one.
2022-10-03 16:02:40 +00:00
fn part_1(input: &str) -> Result<String> {
Ok(
input
.split("\n\n")
.map(|group| {
group
.replace('\n', "")
.chars()
.collect::<HashSet<char>>()
.len()
})
.sum::<usize>()
.to_string(),
)
}
2024-01-14 21:04:10 +00:00
/// The logic to solve part two.
2022-10-03 16:02:40 +00:00
fn part_2(input: &str) -> Result<String> {
Ok(
input
.split("\n\n")
.map(|group| {
let mut count = 0;
let possibilities =
group.replace('\n', "").chars().collect::<HashSet<char>>();
for character in possibilities {
if group
.trim()
.split('\n')
.all(|person| person.contains(character))
{
count += 1;
}
}
count
})
.sum::<usize>()
.to_string(),
)
}