Browse Source

day 6

master
Dan Ballard 1 year ago
parent
commit
0f73aa6d6f
  1. 2132
      inputs/06-01.txt
  2. 50
      src/aoc06/mod.rs
  3. 3
      src/lib.rs
  4. 1
      src/main.rs

2132
inputs/06-01.txt
File diff suppressed because it is too large
View File

50
src/aoc06/mod.rs

@ -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(())
}

3
src/lib.rs

@ -2,4 +2,5 @@ pub mod aoc01;
pub mod aoc02;
pub mod aoc03;
pub mod aoc04;
pub mod aoc05;
pub mod aoc05;
pub mod aoc06;

1
src/main.rs

@ -12,6 +12,7 @@ fn main() {
3 => advent_of_code::aoc03::both("./inputs/03-01.txt"),
4 => advent_of_code::aoc04::both("./inputs/04-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()),
};

Loading…
Cancel
Save