···11-use crate::solutions::year_2022::{Day2, Day3, Day4, Day5};
11+use crate::solutions::year_2022::{Day2, Day3, Day4, Day5, Day6};
22use aoc_lib::Solver;
33pub mod solutions;
4455fn main() {
66- let sol = Day5::solve_part2();
66+ let sol = Day6::solve_part2();
77 println!("{:?}", sol);
88}
+75
2022/src/solutions/year_2022/day6.rs
···11+use std::collections::{HashSet, VecDeque};
22+33+use aoc_lib::{Day, Input, Solver};
44+55+pub struct Day6 {
66+ seq_checker: VecDeque<char>,
77+ chars_processed: u16,
88+ distinct_char_count: usize,
99+}
1010+1111+impl Day6 {
1212+ fn new(count: usize) -> Self {
1313+ Day6 {
1414+ seq_checker: VecDeque::with_capacity(count),
1515+ chars_processed: 0,
1616+ distinct_char_count: count,
1717+ }
1818+ }
1919+ fn update_chars_processed(&mut self) -> &mut Self {
2020+ self.chars_processed += 1;
2121+ self
2222+ }
2323+2424+ fn check_seq_marker(&mut self) -> Option<&mut Self> {
2525+ let s: HashSet<&char> = HashSet::from_iter(self.seq_checker.iter());
2626+ let ll = s.len();
2727+ if ll == self.distinct_char_count {
2828+ Some(self)
2929+ } else {
3030+ None
3131+ }
3232+ }
3333+3434+ fn marker_detected(&mut self, c: char) -> Option<&mut Self> {
3535+ let len = self.seq_checker.len();
3636+ match len {
3737+ x if x < self.distinct_char_count => {
3838+ self.seq_checker.push_back(c);
3939+ self.update_chars_processed();
4040+ self.check_seq_marker()
4141+ }
4242+ _ => {
4343+ self.seq_checker.pop_front();
4444+ self.seq_checker.push_back(c);
4545+ self.update_chars_processed();
4646+ self.check_seq_marker()
4747+ }
4848+ }
4949+ }
5050+5151+ fn common_sol(&mut self, input: Input) -> &mut Self {
5252+ let line = input.lines.first().expect("first line is imp");
5353+ let mut xs: Vec<char> = line.chars().rev().collect();
5454+ let mut c = xs.pop().expect("char from the stream");
5555+ while self.marker_detected(c).is_none() {
5656+ c = xs.pop().expect("char from the stream");
5757+ }
5858+ self
5959+ }
6060+}
6161+6262+impl Solver for Day6 {
6363+ fn solution_part1(input: Input) -> Result<Self::OutputPart1, Self::Error> {
6464+ Ok(Day6::new(4).common_sol(input).chars_processed)
6565+ }
6666+ fn solution_part2(input: aoc_lib::Input) -> Result<Self::OutputPart2, Self::Error> {
6767+ Ok(Day6::new(14).common_sol(input).chars_processed)
6868+ }
6969+ fn day() -> aoc_lib::Day {
7070+ Day::try_from(6).expect("could not parse day")
7171+ }
7272+ type Error = String;
7373+ type OutputPart1 = u16;
7474+ type OutputPart2 = u16;
7575+}
+2
2022/src/solutions/year_2022/mod.rs
···33mod day3;
44mod day4;
55mod day5;
66+mod day6;
67pub(crate) use day1::*;
78pub(crate) use day2::*;
89pub(crate) use day3::*;
910pub(crate) use day4::*;
1011pub(crate) use day5::*;
1212+pub(crate) use day6::*;