summaryrefslogtreecommitdiff
path: root/06/src/part-2.rs
blob: 8f004b0664d2d84e881d9a53a8b13aa291705d82 (plain)
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
/*
Just for fun, we'll try to pry out efficiency everywhere here:
 - Don't do the convenient line iteration (causing allocations).
 - Don't do UTF-8 allocation.
 - Store seen characters in bitmap.
 - TODO: There are probably some non-elided indexing safety checks.
*/

use std::io::{BufRead};

const MARKER_LEN: usize = 14;

fn count_ones<const N: usize>(xs: & [usize; N]) -> usize {
    let mut ret: usize = 0;
    for x in xs {
        if *x == 1 { ret += 1; }
    }
    ret
}

fn main() {    
    let stdin = std::io::stdin();
    let mut handle = stdin.lock();
    
    let mut buf: Vec<u8> = Vec::new();
      
    loop {
        buf.clear();
        let num_bytes = handle.read_until(b'\n', &mut buf).expect("IO error");
        if num_bytes == 0 { break; }
        if buf.len() < MARKER_LEN { panic!("Malformed input"); }

        let mut counts: [usize; 26] = [0; 26];
        
        for i in 0..buf.len() {
            if i >= MARKER_LEN {
                counts[(buf[i-MARKER_LEN] - b'a') as usize] -= 1;
            }
            counts[(buf[i] - b'a') as usize] += 1;
            if count_ones(& counts) == MARKER_LEN {
                println!("{}", i + 1);
                break;
            }
        }
    }
    
}