This commit is contained in:
Dory 2024-03-10 00:02:11 -08:00
parent 0c6b8a5106
commit b4ec1ca428
2 changed files with 34 additions and 9 deletions

View File

@ -1,25 +1,24 @@
use std::env; use std::env;
use std::fs::File; use std::fs::File;
use std::io::{BufReader, Read}; use std::io::{BufReader, Read};
use std::collections::{VecDeque, HashSet};
fn main() { fn main() {
let file = File::open(&env::args().nth(1).unwrap()).unwrap(); let file = File::open(&env::args().nth(1).unwrap()).unwrap();
let reader = BufReader::new(file); 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; let mut i = 0;
for byte in reader.bytes() { for byte in reader.bytes() {
i += 1; i += 1;
let c = byte.unwrap(); prevs.push_back(byte.unwrap());
if c != x1 && c != x2 && c != x3 if prevs.len() > 14 {
&& x1 != x2 && x1 != x3 && x2 != x3 prevs.pop_front();
&& x1 != 0 { }
let set: HashSet<u8> = HashSet::from_iter(prevs.iter().cloned());
if set.len() == 14 {
println!("{}", i); println!("{}", i);
break; break;
} else {
x1 = x2;
x2 = x3;
x3 = c;
} }
} }
} }

26
day6/src/main1.rs Normal file
View 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;
}
}
}