d6p2
This commit is contained in:
parent
0c6b8a5106
commit
b4ec1ca428
@ -1,25 +1,24 @@
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Read};
|
||||
use std::collections::{VecDeque, HashSet};
|
||||
|
||||
fn main() {
|
||||
let file = File::open(&env::args().nth(1).unwrap()).unwrap();
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let (mut x1, mut x2, mut x3) = (0, 0, 0);
|
||||
let mut prevs: VecDeque<u8> = VecDeque::new();
|
||||
let mut i = 0;
|
||||
for byte in reader.bytes() {
|
||||
i += 1;
|
||||
let c = byte.unwrap();
|
||||
if c != x1 && c != x2 && c != x3
|
||||
&& x1 != x2 && x1 != x3 && x2 != x3
|
||||
&& x1 != 0 {
|
||||
prevs.push_back(byte.unwrap());
|
||||
if prevs.len() > 14 {
|
||||
prevs.pop_front();
|
||||
}
|
||||
let set: HashSet<u8> = HashSet::from_iter(prevs.iter().cloned());
|
||||
if set.len() == 14 {
|
||||
println!("{}", i);
|
||||
break;
|
||||
} else {
|
||||
x1 = x2;
|
||||
x2 = x3;
|
||||
x3 = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
26
day6/src/main1.rs
Normal file
26
day6/src/main1.rs
Normal file
@ -0,0 +1,26 @@
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Read};
|
||||
|
||||
fn main() {
|
||||
let file = File::open(&env::args().nth(1).unwrap()).unwrap();
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let (mut x1, mut x2, mut x3) = (0, 0, 0);
|
||||
let mut i = 0;
|
||||
for byte in reader.bytes() {
|
||||
i += 1;
|
||||
let c = byte.unwrap();
|
||||
if c != x1 && c != x2 && c != x3
|
||||
&& x1 != x2 && x1 != x3 && x2 != x3
|
||||
&& x1 != 0 {
|
||||
println!("{}", i);
|
||||
break;
|
||||
} else {
|
||||
x1 = x2;
|
||||
x2 = x3;
|
||||
x3 = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user