mirror of
https://github.com/javalsai/aoc.git
synced 2026-01-13 01:19:59 +01:00
add: combined solution to d01
This commit is contained in:
34
2025/01/all.rs
Normal file
34
2025/01/all.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
type SmolI = i32;
|
||||||
|
|
||||||
|
#[unsafe(no_mangle)]
|
||||||
|
unsafe extern "Rust" fn challenge_isize_duple(buf: &[u8]) -> (isize, isize) {
|
||||||
|
let mut count_p1 = 0;
|
||||||
|
let mut count_p2 = 0;
|
||||||
|
let mut pos = 50;
|
||||||
|
|
||||||
|
let buf = unsafe { str::from_utf8_unchecked(buf.get_unchecked(..(buf.len() - 1))) };
|
||||||
|
|
||||||
|
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() };
|
||||||
|
if amt == 0 { continue; }
|
||||||
|
let prev_pos = pos;
|
||||||
|
pos += ((((dir == b'R') as SmolI) << 1) - 1) * amt;
|
||||||
|
|
||||||
|
count_p2 += if pos < 0 {
|
||||||
|
if prev_pos == 0 {
|
||||||
|
count_p2 -= 1;
|
||||||
|
};
|
||||||
|
(pos - 1).div_euclid(100).abs()
|
||||||
|
} else if pos > 0 {
|
||||||
|
pos.div_euclid(100)
|
||||||
|
} else {
|
||||||
|
1
|
||||||
|
};
|
||||||
|
pos = pos.rem_euclid(100);
|
||||||
|
count_p1 += (pos == 0) as SmolI;
|
||||||
|
}
|
||||||
|
|
||||||
|
(count_p1 as isize, count_p2 as isize)
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
All this year is going to be firstly made in rust, I will do performance too. For that I'ma make a general helper that compiles each rust file into an `.so` file with a common exported main function that takes the input byte buffer and returns a solution (hoping all of them will be integers).
|
All this year is going to be firstly made in rust, I will do performance too. For that I'ma make a general helper that compiles each rust file into an `.so` file with a common exported main function that takes the input byte buffer and returns a solution (hoping all of them will be integers).
|
||||||
|
|
||||||
I will make an external rust program that takes the day to run and the input, it will buffer and measure the execution speed.
|
I will make an external rust program that takes the day to run and the input, it will buffer and measure the execution speed.
|
||||||
|
|
||||||
|
Performance is critical so we can break behavior with UB for the sake of correct data.
|
||||||
|
|||||||
@@ -31,7 +31,11 @@ pub mod dl {
|
|||||||
/// To get the error call [`ffi::error()`].
|
/// To get the error call [`ffi::error()`].
|
||||||
pub fn try_drop(self) -> Result<(), (Self, i32)> {
|
pub fn try_drop(self) -> Result<(), (Self, i32)> {
|
||||||
let err = unsafe { ffi::close(self.0) };
|
let err = unsafe { ffi::close(self.0) };
|
||||||
if err == 0 { Ok(()) } else { Err((self, err)) }
|
if err == 0 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err((self, err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
@@ -176,8 +180,8 @@ pub mod loader {
|
|||||||
// drop.
|
// drop.
|
||||||
untry!(unsafe { handle.symfn::<C<isize>>(c"challenge_isize") }.map(V::Isize));
|
untry!(unsafe { handle.symfn::<C<isize>>(c"challenge_isize") }.map(V::Isize));
|
||||||
untry!(unsafe { handle.symfn::<C<usize>>(c"challenge_isize") }.map(V::Usize));
|
untry!(unsafe { handle.symfn::<C<usize>>(c"challenge_isize") }.map(V::Usize));
|
||||||
untry!(unsafe { handle.symfn::<C<(usize, usize)>>(c"challenge_isize") }.map(V::UsizeDuple));
|
untry!(unsafe { handle.symfn::<C<(usize, usize)>>(c"challenge_isize_duple") }.map(V::UsizeDuple));
|
||||||
untry!(unsafe { handle.symfn::<C<(isize, isize)>>(c"challenge_isize") }.map(V::IsizeDuple));
|
untry!(unsafe { handle.symfn::<C<(isize, isize)>>(c"challenge_isize_duple") }.map(V::IsizeDuple));
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user