day 6
This commit is contained in:
parent
6c06cbcce4
commit
0f73aa6d6f
File diff suppressed because it is too large
Load Diff
|
@ -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(())
|
||||||
|
}
|
||||||
|
|
|
@ -2,4 +2,5 @@ pub mod aoc01;
|
||||||
pub mod aoc02;
|
pub mod aoc02;
|
||||||
pub mod aoc03;
|
pub mod aoc03;
|
||||||
pub mod aoc04;
|
pub mod aoc04;
|
||||||
pub mod aoc05;
|
pub mod aoc05;
|
||||||
|
pub mod aoc06;
|
|
@ -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()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue