Advent of code 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.2 KiB

use std::error::Error;
use std::fs;
use std::collections::HashMap;
use std::str;
pub fn both(filename: &str) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(filename)?;
let mut answers: HashMap<u8, bool> = HashMap::new();
let mut sum = 0;
for line in contents.lines() {
if line.trim() == "" {
sum += answers.len();
answers = HashMap::new();
continue
}
for byte in line.as_bytes() {
answers.insert(*byte, true);
}
}
sum += answers.len();
println!("sum of group unique questions with yes: {}", sum);
answers = HashMap::new();
let mut first = true;
sum = 0;
for line in contents.lines() {
if line.trim() == "" {
sum += answers.len();
answers = HashMap::new();
first = true;
continue
}
if first {
for byte in line.as_bytes() {
answers.insert(*byte, true);
}
first = false;
} else {
answers.retain(|k, _| line.as_bytes().contains(k));
}
}
sum += answers.len();
println!("sum of group questions with all yes: {}", sum);
Ok(())
}