day06: Add part 1
Borrow-checker hindered me to keep the same HashSet around all the time and just clear it, so it feels a little inefficient to have a new set every time.
This commit is contained in:
parent
f083a70ae5
commit
035f8e86ac
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day06"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "day06"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,37 @@
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use std::error;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
|
||||||
|
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[3..])?;
|
||||||
|
while n > 0 {
|
||||||
|
for i in 4..(4 + n) {
|
||||||
|
pos += 1;
|
||||||
|
// println!("{}", std::str::from_utf8(&buf[(i - 4)..i]).unwrap());
|
||||||
|
// skip if we read uninitialized data from first run
|
||||||
|
if buf[i - 4] == 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let set: HashSet<&u8> = HashSet::from_iter(&buf[(i - 4)..i]);
|
||||||
|
if set.len() == 4 {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if found {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for i in 0..3 {
|
||||||
|
buf[i] = buf[29 + i];
|
||||||
|
}
|
||||||
|
n = f.read(&mut buf[3..])?;
|
||||||
|
}
|
||||||
|
println!("Found 4 different chars at position {}", pos);
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue