Compare commits

..

2 Commits

Author SHA1 Message Date
MasterofJOKers 01789441db day06: Make more generic and add part 2 with it
Part 2 is basically the same just with 14 chars instead of 4.
2022-12-19 14:38:32 +01:00
MasterofJOKers 035f8e86ac 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.
2022-12-19 14:34:02 +01:00
3 changed files with 54 additions and 0 deletions

7
day06/Cargo.lock generated Normal file
View File

@ -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"

8
day06/Cargo.toml Normal file
View File

@ -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]

39
day06/src/main.rs Normal file
View File

@ -0,0 +1,39 @@
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(())
}