From 7c7c69255d09885fcc0fface2a8d5e3850f93941 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Tue, 6 Dec 2022 18:23:43 +0100 Subject: [PATCH] Replace indirect indexing 230 byte overhead is worth it to avoid conversions and potential indexing errors --- 2022/src/day06.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/2022/src/day06.rs b/2022/src/day06.rs index f36ac0d..ad1e3b4 100644 --- a/2022/src/day06.rs +++ b/2022/src/day06.rs @@ -1,19 +1,15 @@ use anyhow::Result; fn find_first(input: &[u8], unique: usize) -> Result { - #[inline] - const fn index(c: u8) -> usize { - (c - b'a') as usize - } - let mut seen = [false; 26]; + let mut seen = [false; 256]; let mut first = 0; // Loop invariant: input[first..last] contains only unique characters for (last, &c) in input.iter().enumerate() { - if seen[index(c)] { + if seen[c as usize] { while input[first] != c { - seen[index(input[first])] = false; + seen[input[first] as usize] = false; first += 1; } first += 1; @@ -23,7 +19,7 @@ fn find_first(input: &[u8], unique: usize) -> Result { return Ok(first + unique); } - seen[index(c)] = true; + seen[c as usize] = true; } }