advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

main.rs (856B)

    1 use std::collections::{HashMap, VecDeque};
    2 
    3 fn main() {
    4     let input = std::fs::read_to_string("input.txt").unwrap().to_string();
    5     // 1542
    6     println!("Part 1: {}", part_1(&input, 4).unwrap());
    7     // 3153
    8     println!("Part 2: {}", part_1(&input, 14).unwrap());
    9 }
   10 
   11 fn part_1(input: &str, header_size: usize) -> Option<usize> {
   12     let mut buf = VecDeque::new();
   13     let mut counter = HashMap::new();
   14     for (i, c) in input.chars().enumerate() {
   15         buf.push_back(c);
   16         counter.insert(c, *counter.get(&c).unwrap_or(&0) + 1);
   17         if buf.len() > header_size {
   18             let h = buf.pop_front().unwrap();
   19             counter.insert(h, *counter.get(&h).unwrap() - 1);
   20             if buf.iter().filter(|x| *counter.get(x).unwrap() == 1).count() == header_size {
   21                 return Some(i + 1);
   22             }
   23         }
   24     }
   25     None
   26 }