You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
928 B

use std::collections::HashSet;
use std::error;
use std::fs::File;
use std::io::prelude::*;
const N_CHARS: usize = 14;
fn main() -> Result<(), Box<dyn error::Error>> {
let mut f = File::open("input.txt")?;
let mut buf = [0; 32];
let mut pos = 0;
let mut found = false;
let mut n = f.read(&mut buf[(N_CHARS - 1)..])?;
while n > 0 {
for i in N_CHARS..(N_CHARS + n) {
pos += 1;
// println!("{}", std::str::from_utf8(&buf[(i - N_CHARS)..i]).unwrap());
// skip if we read uninitialized data from first run
if buf[i - N_CHARS] == 0 {
continue;
}
let set: HashSet<&u8> = HashSet::from_iter(&buf[(i - N_CHARS)..i]);
if set.len() == N_CHARS {
found = true;
break;
}
}
if found {
break;
}
for i in 0..(N_CHARS - 1) {
buf[i] = buf[(32 - N_CHARS + 1) + i];
}
n = f.read(&mut buf[(N_CHARS - 1)..])?;
}
println!("Found 4 different chars at position {}", pos);
Ok(())
}