Day 6!
This commit is contained in:
2061
inputs/day6.txt
Normal file
2061
inputs/day6.txt
Normal file
File diff suppressed because it is too large
Load Diff
15
inputs/day6_test.txt
Normal file
15
inputs/day6_test.txt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
abc
|
||||||
|
|
||||||
|
a
|
||||||
|
b
|
||||||
|
c
|
||||||
|
|
||||||
|
ab
|
||||||
|
ac
|
||||||
|
|
||||||
|
a
|
||||||
|
a
|
||||||
|
a
|
||||||
|
a
|
||||||
|
|
||||||
|
b
|
||||||
82
src/bin/customs_form.rs
Normal file
82
src/bin/customs_form.rs
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
use advent2020::errors::TopLevelError;
|
||||||
|
use std::collections::BTreeSet;
|
||||||
|
use std::env;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
fn real_main() -> Result<(), TopLevelError> {
|
||||||
|
let mut customs_forms_any = Vec::new();
|
||||||
|
let mut customs_forms_all = Vec::new();
|
||||||
|
|
||||||
|
for argument in env::args().skip(1) {
|
||||||
|
let contents = fs::read_to_string(argument)?;
|
||||||
|
let mut current_form_any = BTreeSet::new();
|
||||||
|
let mut current_form_all = every_seat();
|
||||||
|
|
||||||
|
for line in contents.lines() {
|
||||||
|
if line.is_empty() {
|
||||||
|
customs_forms_any.push(current_form_any);
|
||||||
|
customs_forms_all.push(current_form_all);
|
||||||
|
current_form_any = BTreeSet::new();
|
||||||
|
current_form_all = every_seat();
|
||||||
|
} else {
|
||||||
|
let mut person_answers = BTreeSet::new();
|
||||||
|
|
||||||
|
for char in line.chars() {
|
||||||
|
person_answers.insert(char);
|
||||||
|
}
|
||||||
|
|
||||||
|
current_form_any = current_form_any.union(&person_answers).cloned().collect();
|
||||||
|
current_form_all = current_form_all
|
||||||
|
.intersection(&person_answers)
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
customs_forms_any.push(current_form_any);
|
||||||
|
customs_forms_all.push(current_form_all);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut sum_all = 0;
|
||||||
|
let mut sum_any = 0;
|
||||||
|
|
||||||
|
for (form_any, form_all) in customs_forms_any.iter().zip(customs_forms_all.iter()) {
|
||||||
|
println!(
|
||||||
|
"Form: any |{}| / all |{}|",
|
||||||
|
display_form(form_any),
|
||||||
|
display_form(form_all)
|
||||||
|
);
|
||||||
|
sum_any += form_any.len();
|
||||||
|
sum_all += form_all.len();
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Checked boxes (any): {}", sum_any);
|
||||||
|
println!("Checked boxes (all): {}", sum_all);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn display_form(x: &BTreeSet<char>) -> String {
|
||||||
|
let mut retval = String::new();
|
||||||
|
|
||||||
|
for char in x.iter() {
|
||||||
|
retval.push(*char);
|
||||||
|
}
|
||||||
|
|
||||||
|
retval
|
||||||
|
}
|
||||||
|
|
||||||
|
fn every_seat() -> BTreeSet<char> {
|
||||||
|
let mut result = BTreeSet::new();
|
||||||
|
|
||||||
|
for c in 'a'..='z' {
|
||||||
|
result.insert(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
if let Err(e) = real_main() {
|
||||||
|
eprintln!("ERROR: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user