Rust!
use std::collections::HashSet; #[aoc_generator(day6)] fn to_vec(input: &str) -> Vec<Vec<Vec<char>>> { input .split("\n\n") .map(|i| { i.lines() .map(|s| s.chars().collect::<Vec<char>>()) .collect() }) .collect() } #[aoc(day6, part1)] fn answer_count(input: &Vec<Vec<Vec<char>>>) -> usize { input .iter() .map(|group| { group .iter() .flat_map(|v| v.iter().map(|c| *c).collect::<HashSet<char>>()) .collect::<HashSet<char>>() .len() }) .sum() } #[aoc(day6, part2)] fn abs_answer_count(input: &Vec<Vec<Vec<char>>>) -> usize { input .iter() .map(|group| { group .iter() .map(|v| v.iter().map(|c| *c).collect::<HashSet<char>>()) .fold(Option::None, |l, g| { if l.is_none() { Some(g.clone()) } else { Some( l.unwrap_or(HashSet::new()) .intersection(&g) .map(|c| *c) .collect(), ) } }) .unwrap_or(HashSet::new()) .len() }) .sum() }
Ugh, made it too late for this day (but I had it done in time for the problem, honest!).
Used HashSet to my benefit here, as I could apply set theory to the problem and get it done easily.
As always, in Github.
use aoc_runner_derive::{aoc, aoc_generator}; use std::collections::HashSet; #[aoc_generator(day6)] fn parse_input_day6(input: &str) -> Vec<String> { input .split("\n\n") .map(|group| String::from(group)) .collect() } #[aoc(day6, part1)] fn sum_group_questions(input: &Vec<String>) -> usize { let answers: Vec<HashSet<char>> = input .iter() .map(|group| group.chars().filter(|x| *x != '\n').collect()) .collect(); answers.iter().map(|group| group.len()).sum() } #[aoc(day6, part2)] fn sum_group_common_questions(input: &Vec<String>) -> usize { input .iter() .map(|group| { let mut first_run = true; group .lines() .map(|answers| answers.chars().collect::<HashSet<char>>()) .fold(HashSet::new(), |memo, ans| { if memo.is_empty() && first_run { first_run = false; ans.clone() } else { let intersect = memo.intersection(&ans).cloned().collect(); intersect } }) .len() }) .sum() }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Rust!
Ugh, made it too late for this day (but I had it done in time for the problem, honest!).
Used HashSet to my benefit here, as I could apply set theory to the problem and get it done easily.
As always, in Github.