mirror of
https://github.com/javalsai/aoc.git
synced 2026-01-13 01:19:59 +01:00
YOO: start 2025 with a benching rust framework
This commit is contained in:
24
2025/01/p1.rs
Normal file
24
2025/01/p1.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use std::{hint::unreachable_unchecked, io::BufRead};
|
||||
|
||||
#[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 amt: i16 = unsafe { amt.parse().unwrap_unchecked() };
|
||||
match dir {
|
||||
b'L' => pos -= amt,
|
||||
b'R' => pos += amt,
|
||||
_ => unsafe { unreachable_unchecked() },
|
||||
}
|
||||
|
||||
pos %= 100;
|
||||
count += (pos != 0) as i16;
|
||||
}
|
||||
|
||||
count.into()
|
||||
}
|
||||
37
2025/01/p2.rs
Normal file
37
2025/01/p2.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use std::{hint::unreachable_unchecked, io::BufRead};
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
unsafe 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) = (ln.as_bytes()[0], &ln[1..]);
|
||||
|
||||
let amt: isize = unsafe { amt.parse().unwrap_unchecked() };
|
||||
if amt == 0 {
|
||||
continue;
|
||||
}
|
||||
let prev_pos = pos;
|
||||
match dir {
|
||||
b'L' => pos -= amt,
|
||||
b'R' => pos += amt,
|
||||
_ => unsafe { unreachable_unchecked() },
|
||||
}
|
||||
|
||||
count += if pos < 0 {
|
||||
if prev_pos == 0 {
|
||||
count -= 1;
|
||||
};
|
||||
(pos - 1).div_euclid(100).abs()
|
||||
} else if pos > 0 {
|
||||
pos.div_euclid(100)
|
||||
} else {
|
||||
1
|
||||
};
|
||||
pos = pos.rem_euclid(100);
|
||||
}
|
||||
|
||||
count
|
||||
}
|
||||
Reference in New Issue
Block a user