mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 21:30:31 +01:00
Replace indirect indexing
230 byte overhead is worth it to avoid conversions and potential indexing errors
This commit is contained in:
@@ -1,19 +1,15 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
fn find_first(input: &[u8], unique: usize) -> Result<usize> {
|
fn find_first(input: &[u8], unique: usize) -> Result<usize> {
|
||||||
#[inline]
|
let mut seen = [false; 256];
|
||||||
const fn index(c: u8) -> usize {
|
|
||||||
(c - b'a') as usize
|
|
||||||
}
|
|
||||||
let mut seen = [false; 26];
|
|
||||||
|
|
||||||
let mut first = 0;
|
let mut first = 0;
|
||||||
|
|
||||||
// Loop invariant: input[first..last] contains only unique characters
|
// Loop invariant: input[first..last] contains only unique characters
|
||||||
for (last, &c) in input.iter().enumerate() {
|
for (last, &c) in input.iter().enumerate() {
|
||||||
if seen[index(c)] {
|
if seen[c as usize] {
|
||||||
while input[first] != c {
|
while input[first] != c {
|
||||||
seen[index(input[first])] = false;
|
seen[input[first] as usize] = false;
|
||||||
first += 1;
|
first += 1;
|
||||||
}
|
}
|
||||||
first += 1;
|
first += 1;
|
||||||
@@ -23,7 +19,7 @@ fn find_first(input: &[u8], unique: usize) -> Result<usize> {
|
|||||||
return Ok(first + unique);
|
return Ok(first + unique);
|
||||||
}
|
}
|
||||||
|
|
||||||
seen[index(c)] = true;
|
seen[c as usize] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user