day6
This commit is contained in:
parent
2492469277
commit
ae04d78974
3 changed files with 2328 additions and 0 deletions
|
@ -31,3 +31,7 @@ path = "src/day04/main.rs"
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "day05"
|
name = "day05"
|
||||||
path = "src/day05/main.rs"
|
path = "src/day05/main.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "day06"
|
||||||
|
path = "src/day06/main.rs"
|
||||||
|
|
98
src/day06/main.rs
Normal file
98
src/day06/main.rs
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
use anyhow::Result;
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
static INPUT: &str = include_str!("../../input06");
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let uniq_votes: usize = GroupParser::from(INPUT)
|
||||||
|
.into_iter()
|
||||||
|
.map(|group| group.uniq_votes)
|
||||||
|
.sum();
|
||||||
|
println!("uniqe votes: {}", uniq_votes);
|
||||||
|
let approval_votes: usize = GroupParser::from(INPUT)
|
||||||
|
.into_iter()
|
||||||
|
.map(|group| group.approval_votes)
|
||||||
|
.sum();
|
||||||
|
println!("approval votes: {}", approval_votes);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
struct GroupParser<'a> {
|
||||||
|
inner: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a str> for GroupParser<'a> {
|
||||||
|
fn from(data: &'a str) -> Self {
|
||||||
|
GroupParser { inner: data }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for GroupParser<'a> {
|
||||||
|
type Item = Group;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
while self.inner.len() > 0 {
|
||||||
|
let mut splitted = self.inner.splitn(2, "\n\n");
|
||||||
|
let line = splitted.next().unwrap();
|
||||||
|
self.inner = splitted.next().or(Some(""))?;
|
||||||
|
|
||||||
|
match line {
|
||||||
|
"" => (),
|
||||||
|
line => return Some(Group::from(line)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Group {
|
||||||
|
uniq_votes: usize,
|
||||||
|
approval_votes: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&str> for Group {
|
||||||
|
fn from(value: &str) -> Self {
|
||||||
|
let uniq_votes: HashSet<char> = value.chars().filter(|&c| c.is_alphabetic()).collect();
|
||||||
|
|
||||||
|
let approval_votes: HashSet<char> = value
|
||||||
|
.trim()
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
line.chars().filter(|&c| c.is_alphabetic()).collect()
|
||||||
|
})
|
||||||
|
.fold(
|
||||||
|
"abcdefghijklmnopqrstuvwxyz".chars().collect(),
|
||||||
|
|set, chars| set.intersection(&chars).cloned().collect(),
|
||||||
|
);
|
||||||
|
|
||||||
|
Group {
|
||||||
|
uniq_votes: uniq_votes.len(),
|
||||||
|
approval_votes: approval_votes.len(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fails() {
|
||||||
|
let uniq_votes: usize = GroupParser::from(INPUT)
|
||||||
|
.into_iter()
|
||||||
|
.map(|group| group.uniq_votes)
|
||||||
|
.sum();
|
||||||
|
assert_ne!(uniq_votes, 46);
|
||||||
|
assert_ne!(uniq_votes, 15008);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn day1() {
|
||||||
|
let uniq_votes: usize = GroupParser::from(INPUT)
|
||||||
|
.into_iter()
|
||||||
|
.map(|group| group.uniq_votes)
|
||||||
|
.sum();
|
||||||
|
assert_eq!(uniq_votes, 6587);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue