Completed day 6
This commit is contained in:
parent
ff14ce2219
commit
b2bcbd1dbb
14
day06/Cargo.lock
generated
Normal file
14
day06/Cargo.lock
generated
Normal file
@ -0,0 +1,14 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7"
|
||||
|
||||
[[package]]
|
||||
name = "day06"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
]
|
10
day06/Cargo.toml
Normal file
10
day06/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "day06"
|
||||
version = "0.1.0"
|
||||
authors = ["Tyler Hallada <tyler@hallada.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
2190
day06/input/input.txt
Executable file
2190
day06/input/input.txt
Executable file
File diff suppressed because it is too large
Load Diff
15
day06/input/test.txt
Normal file
15
day06/input/test.txt
Normal file
@ -0,0 +1,15 @@
|
||||
abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b
|
66
day06/src/main.rs
Normal file
66
day06/src/main.rs
Normal file
@ -0,0 +1,66 @@
|
||||
use anyhow::Result;
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs::read_to_string;
|
||||
|
||||
const INPUT: &str = "input/input.txt";
|
||||
|
||||
fn solve_part1(input_path: &str) -> Result<usize> {
|
||||
let answers = read_to_string(input_path)?;
|
||||
Ok(answers
|
||||
.split("\n\n")
|
||||
.map(|group| {
|
||||
let mut answered = HashSet::new();
|
||||
for person_answers in group.split("\n") {
|
||||
for c in person_answers.chars() {
|
||||
answered.insert(c);
|
||||
}
|
||||
}
|
||||
answered.len()
|
||||
})
|
||||
.sum())
|
||||
}
|
||||
|
||||
fn solve_part2(input_path: &str) -> Result<usize> {
|
||||
let answers = read_to_string(input_path)?;
|
||||
Ok(answers
|
||||
.split("\n\n")
|
||||
.map(|group| {
|
||||
let mut answer_counts = HashMap::new();
|
||||
let mut group_len = 0;
|
||||
for person_answers in group.trim().split("\n") {
|
||||
for c in person_answers.chars() {
|
||||
let counter = answer_counts.entry(c).or_insert(0);
|
||||
*counter += 1;
|
||||
}
|
||||
group_len += 1;
|
||||
}
|
||||
answer_counts
|
||||
.values()
|
||||
.filter(|&&count| count == group_len)
|
||||
.count()
|
||||
})
|
||||
.sum())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Part 1: {}", solve_part1(INPUT).unwrap());
|
||||
println!("Part 2: {}", solve_part2(INPUT).unwrap());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const TEST_INPUT: &str = "input/test.txt";
|
||||
|
||||
#[test]
|
||||
fn solves_part1() {
|
||||
assert_eq!(solve_part1(TEST_INPUT).unwrap(), 11);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solves_part2() {
|
||||
assert_eq!(solve_part2(TEST_INPUT).unwrap(), 6);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user