mirror of
https://github.com/chylex/Advent-of-Code.git
synced 2025-05-16 01:34:04 +02:00
Add 2020 - Day 6 - Part 2
This commit is contained in:
parent
4d73c56888
commit
dec975132e
@ -1,6 +1,6 @@
|
||||
use std::collections::HashSet;
|
||||
use std::error::Error;
|
||||
|
||||
use crate::group::Group;
|
||||
use crate::utils::GenericError;
|
||||
|
||||
#[path = "../utils/mod.rs"]
|
||||
@ -10,8 +10,10 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
let lines = utils::read_input_lines()?;
|
||||
let groups = load_groups(&lines)?;
|
||||
|
||||
let total_answers = groups.iter().map(|group| group.combined_answers.len()).sum::<usize>();
|
||||
println!("Total 'yes' answers among groups: {}", total_answers);
|
||||
let total_answers_any_members = groups.iter().map(Group::count_answers_by_any_members).sum::<usize>();
|
||||
let total_answers_all_members = groups.iter().map(Group::count_answers_by_all_members).sum::<usize>();
|
||||
println!("Total 'yes' answers, counting answers by any group member: {}", total_answers_any_members);
|
||||
println!("Total 'yes' answers, counting answers by all group members: {}", total_answers_all_members);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -33,31 +35,45 @@ fn load_groups(lines: &Vec<String>) -> Result<Vec<Group>, GenericError> {
|
||||
Ok(groups)
|
||||
}
|
||||
|
||||
struct Group {
|
||||
individual_answers: Vec<HashSet<char>>,
|
||||
combined_answers: HashSet<char>,
|
||||
}
|
||||
|
||||
impl Group {
|
||||
fn new() -> Group {
|
||||
Group {
|
||||
individual_answers: Vec::new(),
|
||||
combined_answers: HashSet::new(),
|
||||
}
|
||||
mod group {
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::GenericError;
|
||||
|
||||
pub struct Group {
|
||||
individual_answers: Vec<HashSet<char>>,
|
||||
combined_answers: HashSet<char>,
|
||||
}
|
||||
|
||||
fn add_individual(&mut self, answer_str: &str) -> Result<(), GenericError> {
|
||||
let answer_set = answer_str.chars().collect::<HashSet<char>>();
|
||||
|
||||
for answer in &answer_set {
|
||||
if answer.is_ascii_lowercase() {
|
||||
self.combined_answers.insert(answer.clone());
|
||||
} else {
|
||||
return Err(GenericError::new(format!("Invalid answer: {}", answer)));
|
||||
impl Group {
|
||||
pub fn new() -> Group {
|
||||
Group {
|
||||
individual_answers: Vec::new(),
|
||||
combined_answers: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
self.individual_answers.push(answer_set);
|
||||
Ok(())
|
||||
pub fn add_individual(&mut self, answer_str: &str) -> Result<(), GenericError> {
|
||||
let answer_set = answer_str.chars().collect::<HashSet<char>>();
|
||||
|
||||
for answer in &answer_set {
|
||||
if answer.is_ascii_lowercase() {
|
||||
self.combined_answers.insert(answer.clone());
|
||||
} else {
|
||||
return Err(GenericError::new(format!("Invalid answer: {}", answer)));
|
||||
}
|
||||
}
|
||||
|
||||
self.individual_answers.push(answer_set);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn count_answers_by_any_members(&self) -> usize {
|
||||
return self.combined_answers.len();
|
||||
}
|
||||
|
||||
pub fn count_answers_by_all_members(&self) -> usize {
|
||||
return self.combined_answers.iter().filter(|answer| self.individual_answers.iter().all(|answers| answers.contains(&answer))).count();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user