This commit is contained in:
Dan Ballard 2020-12-07 00:12:08 -08:00
parent 6c06cbcce4
commit 0f73aa6d6f
4 changed files with 2185 additions and 1 deletions

2132
inputs/06-01.txt Normal file

File diff suppressed because it is too large Load Diff

50
src/aoc06/mod.rs Normal file
View File

@ -0,0 +1,50 @@
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(())
}

View File

@ -3,3 +3,4 @@ pub mod aoc02;
pub mod aoc03; pub mod aoc03;
pub mod aoc04; pub mod aoc04;
pub mod aoc05; pub mod aoc05;
pub mod aoc06;

View File

@ -12,6 +12,7 @@ fn main() {
3 => advent_of_code::aoc03::both("./inputs/03-01.txt"), 3 => advent_of_code::aoc03::both("./inputs/03-01.txt"),
4 => advent_of_code::aoc04::both("./inputs/04-01.txt"), 4 => advent_of_code::aoc04::both("./inputs/04-01.txt"),
5 => advent_of_code::aoc05::both("./inputs/05-01.txt"), 5 => advent_of_code::aoc05::both("./inputs/05-01.txt"),
6 => advent_of_code::aoc06::both("./inputs/06-01.txt"),
_ => Err(format!("unsupported day {}", day).into()), _ => Err(format!("unsupported day {}", day).into()),
}; };