perf: several optimizations

This commit is contained in:
2025-12-02 02:25:40 +01:00
parent 888556b657
commit d44431c42a
4 changed files with 70 additions and 34 deletions

View File

@@ -1,24 +1,20 @@
use std::{hint::unreachable_unchecked, io::BufRead};
type SmolI = i16;
#[unsafe(no_mangle)]
extern "Rust" fn challenge_isize(buf: &[u8]) -> isize {
let mut count = 0;
let mut pos = 50;
for ln in buf.lines() {
let ln = unsafe { ln.unwrap_unchecked() };
let (dir, amt) = unsafe { (ln.as_bytes().get_unchecked(0), ln.get_unchecked(1..)) };
let buf = unsafe { str::from_utf8_unchecked(buf.get_unchecked(..(buf.len() - 1))) };
let amt: i16 = unsafe { amt.parse().unwrap_unchecked() };
match dir {
b'L' => pos -= amt,
b'R' => pos += amt,
_ => unsafe { unreachable_unchecked() },
}
for ln in buf.split('\n') {
let (dir, amt) = unsafe { (*ln.as_bytes().get_unchecked(0), ln.get_unchecked(1..)) };
let amt: SmolI = unsafe { amt.parse().unwrap_unchecked() };
pos += ((((dir == b'R') as SmolI) << 1) - 1) * amt;
pos %= 100;
count += (pos != 0) as i16;
count += (pos == 0) as SmolI;
}
count.into()
count as isize
}