day 6 solutions

This commit is contained in:
Stephen Waits committed 2020-12-05 21:47:17 -08:00
1 parent 29dee21cde
commit 0bd288d390
4 files changed
+2099 -1

No files matched your search

+2050
View File
File diff suppressed because it is too large. Load diff
+2
View File
@@ -0,0 +1,2 @@
6437
3229
+41
View File
@@ -0,0 +1,41 @@
// ==== day 6
use crate::utils;
use anyhow::Result;
use std::collections::{HashMap, HashSet};
fn count_uniq_alpha_chars(s: &str) -> usize {
let seen: HashSet<usize> = s
.chars()
.filter(|c| c.is_alphabetic())
.map(|c| c as usize)
.collect();
seen.len()
}
fn count_common_answers(s: &str) -> usize {
let mut seen: HashMap<char, usize> = HashMap::new();
for l in s.lines() {
for c in l.chars().filter(|c| c.is_alphabetic()) {
*seen.entry(c).or_insert(0) += 1;
}
}
let total = s.lines().count();
seen.values().filter(|v| *v == &total).count()
}
pub fn run() -> Result<(usize, usize)> {
let data = utils::read_file("data/input-06.txt")?;
let answers = utils::read_i64s("data/output-06.txt")?;
// parse into an array of Passport{}
let p1 = data.split("\n\n").map(|s| count_uniq_alpha_chars(s)).sum();
// count passports with all req'd and validated fields
let p2 = data.split("\n\n").map(|s| count_common_answers(s)).sum();
assert_eq!(p1, answers[0] as usize);
assert_eq!(p2, answers[1] as usize);
Ok((p1, p2))
}
+6 -1
View File
@@ -5,12 +5,16 @@ mod day02;
mod day03; mod day03;
mod day04; mod day04;
mod day05; mod day05;
mod day06;
mod utils; mod utils;
// TODO // TODO
// ==== // ====
// * move from i64 to something else that works with the data+problems, int, i32, usize?
// * clean up error handling (move to `?` inside closures) // * clean up error handling (move to `?` inside closures)
// * templatize read_i64s()
// * build out a bit of a harness for the runner
// * add criterion benchmarking
// * figure out nicer way to organize into modules
// ==== main() // ==== main()
@@ -20,5 +24,6 @@ fn main() -> Result<()> {
println!("Day 03: {:?}", day03::run()?); println!("Day 03: {:?}", day03::run()?);
println!("Day 04: {:?}", day04::run()?); println!("Day 04: {:?}", day04::run()?);
println!("Day 05: {:?}", day05::run()?); println!("Day 05: {:?}", day05::run()?);
println!("Day 06: {:?}", day06::run()?);
Ok(()) Ok(())
} }