-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.rs
51 lines (45 loc) · 1.23 KB
/
day06.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufRead};
fn is_done(map: &HashMap<char, usize>, num_chars: usize) -> bool {
map.len() == num_chars && map.values().sum::<usize>() == num_chars
}
fn parse_codes(num_chars: usize) -> std::io::Result<usize> {
let file = File::open("./inputs/day06-1.txt")?;
let reader = io::BufReader::new(file);
let mut total: usize = 0;
for line in reader.lines() {
let l = line?;
let mut map = HashMap::new();
for (i, c) in l.chars().enumerate() {
if is_done(&map, num_chars) {
total += i;
break;
} else if i >= num_chars {
let key = l.chars().nth(i - num_chars).unwrap();
match map.get_mut(&key) {
Some(value) => {
*value -= 1;
if *value == 0 {
map.remove(&key);
}
}
None => panic!("Missing value"),
}
}
match map.get_mut(&c) {
Some(num_times) => *num_times += 1,
None => {
map.insert(c, 1);
()
}
};
}
}
Ok(total)
}
fn main() -> std::io::Result<()> {
println!("Characters processed {}", parse_codes(4)?);
println!("Characters processed {}", parse_codes(14)?);
Ok(())
}