mirror of
https://github.com/javalsai/aoc.git
synced 2026-01-13 01:19:59 +01:00
add: d02, optimizations might come later
will namely add a numberic based algorithm, maybe even the stack .to_string() is too slow also might not do it, grialion already optimized tf out of it
This commit is contained in:
66
2025/02/all.rs
Normal file
66
2025/02/all.rs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
#[unsafe(no_mangle)]
|
||||||
|
fn challenge_usize_duple(b: &[u8]) -> (usize, usize) {
|
||||||
|
let mut total1 = 0;
|
||||||
|
let mut total2 = 0;
|
||||||
|
let s = unsafe { str::from_utf8_unchecked(b) };
|
||||||
|
|
||||||
|
let ranges = s.trim_end().split(',');
|
||||||
|
for range in ranges {
|
||||||
|
let (start, end) = range.split_once('-').unwrap();
|
||||||
|
let (start, end): (usize, usize) = (start.parse().unwrap(), end.parse().unwrap());
|
||||||
|
|
||||||
|
for id in start..=end {
|
||||||
|
let mut buf = [0; _];
|
||||||
|
let id_s = &fast_to_string(id, &mut buf);
|
||||||
|
|
||||||
|
if is_repeating1(id_s) {
|
||||||
|
total1 += id;
|
||||||
|
}
|
||||||
|
if is_repeating2(id_s) {
|
||||||
|
total2 += id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(total1, total2)
|
||||||
|
}
|
||||||
|
|
||||||
|
const MAX_USIZE_LEN: usize = const { usize::MAX.ilog10() + 1 } as usize;
|
||||||
|
/// Output is reversed btw
|
||||||
|
fn fast_to_string(mut n: usize, into: &mut [u8; MAX_USIZE_LEN]) -> &mut [u8] {
|
||||||
|
let mut len = 0;
|
||||||
|
while n != 0 {
|
||||||
|
into[len] = (n % 10) as u8 + b'0';
|
||||||
|
n /= 10;
|
||||||
|
len += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
&mut into[0..len]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_repeating1(s: &[u8]) -> bool {
|
||||||
|
if !s.len().is_multiple_of(2) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mid = s.len() / 2;
|
||||||
|
let (left, right) = s.split_at(mid);
|
||||||
|
left == right
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_repeating2(s: &[u8]) -> bool {
|
||||||
|
for sublen in 0..(s.len() / 2) {
|
||||||
|
let sublen = sublen + 1;
|
||||||
|
if !s.len().is_multiple_of(sublen) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let pref = &s[0..sublen];
|
||||||
|
|
||||||
|
let is_repeating = s[sublen..].chunks_exact(sublen).all(|c| c == pref);
|
||||||
|
if is_repeating {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
69
2025/02/p1.rs
Normal file
69
2025/02/p1.rs
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#[unsafe(no_mangle)]
|
||||||
|
fn challenge_usize(b: &[u8]) -> usize {
|
||||||
|
let mut total = 0;
|
||||||
|
let s = unsafe { str::from_utf8_unchecked(b) };
|
||||||
|
|
||||||
|
dbg!(is_repeating(b"11"));
|
||||||
|
let ranges = s.trim_end().split(',');
|
||||||
|
for range in ranges {
|
||||||
|
let (start, end) = range.split_once('-').unwrap();
|
||||||
|
if start.starts_with('0') || end.starts_with('0') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let (start, end): (usize, usize) = (start.parse().unwrap(), end.parse().unwrap());
|
||||||
|
println!("{start}-{end}");
|
||||||
|
|
||||||
|
for id in start..=end {
|
||||||
|
let id_s = id.to_string();
|
||||||
|
|
||||||
|
if is_repeating(id_s.as_bytes()) {
|
||||||
|
total += id;
|
||||||
|
println!("-->> invalid {id_s}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
total
|
||||||
|
}
|
||||||
|
|
||||||
|
const MAX_USIZE_LEN: usize = const { usize::MAX.ilog10() + 1 } as usize;
|
||||||
|
/// Output is reversed btw
|
||||||
|
fn fast_to_string(mut n: usize, into: &mut [u8; MAX_USIZE_LEN]) -> &mut [u8] {
|
||||||
|
let mut len = 0;
|
||||||
|
while n != 0 {
|
||||||
|
into[len] = (n % 10) as u8 + b'0';
|
||||||
|
n /= 10;
|
||||||
|
len += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
&mut into[0..len]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_repeating(s: &[u8]) -> bool {
|
||||||
|
if !s.len().is_multiple_of(2) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mid = s.len() / 2;
|
||||||
|
let (left, right) = s.split_at(mid);
|
||||||
|
left == right
|
||||||
|
}
|
||||||
|
|
||||||
|
// LMAO: I did p2 instead of p1 firstly bcs I misread it.
|
||||||
|
|
||||||
|
// fn is_repeating(s: &[u8]) -> bool {
|
||||||
|
// for sublen in 0..(s.len() / 2) {
|
||||||
|
// let sublen = sublen + 1;
|
||||||
|
// if !s.len().is_multiple_of(sublen) {
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
// let pref = &s[0..sublen];
|
||||||
|
|
||||||
|
// let is_repeating = s[sublen..].chunks_exact(sublen).all(|c| c == pref);
|
||||||
|
// if is_repeating {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// false
|
||||||
|
// }
|
||||||
54
2025/02/p2.rs
Normal file
54
2025/02/p2.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#[unsafe(no_mangle)]
|
||||||
|
fn challenge_usize(b: &[u8]) -> usize {
|
||||||
|
let mut total = 0;
|
||||||
|
let s = unsafe { str::from_utf8_unchecked(b) };
|
||||||
|
|
||||||
|
dbg!(is_repeating(b"11"));
|
||||||
|
let ranges = s.trim_end().split(',');
|
||||||
|
for range in ranges {
|
||||||
|
let (start, end) = range.split_once('-').unwrap();
|
||||||
|
if start.starts_with('0') || end.starts_with('0') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let (start, end): (usize, usize) = (start.parse().unwrap(), end.parse().unwrap());
|
||||||
|
println!("{start}-{end}");
|
||||||
|
|
||||||
|
for id in start..=end {
|
||||||
|
let id_s = id.to_string();
|
||||||
|
|
||||||
|
if is_repeating(id_s.as_bytes()) {
|
||||||
|
total += id;
|
||||||
|
println!("-->> invalid {id_s}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
total
|
||||||
|
}
|
||||||
|
|
||||||
|
// fn is_repeating(s: &[u8]) -> bool {
|
||||||
|
// if !s.len().is_multiple_of(2) {
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// let mid = s.len() / 2;
|
||||||
|
// let (left, right) = s.split_at(mid);
|
||||||
|
// left == right
|
||||||
|
// }
|
||||||
|
|
||||||
|
fn is_repeating(s: &[u8]) -> bool {
|
||||||
|
for sublen in 0..(s.len() / 2) {
|
||||||
|
let sublen = sublen + 1;
|
||||||
|
if !s.len().is_multiple_of(sublen) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let pref = &s[0..sublen];
|
||||||
|
|
||||||
|
let is_repeating = s[sublen..].chunks_exact(sublen).all(|c| c == pref);
|
||||||
|
if is_repeating {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
@@ -179,9 +179,9 @@ pub mod loader {
|
|||||||
// SAFETY: This is actually safe as it just creates the fn ptr, no weird fn with weird
|
// SAFETY: This is actually safe as it just creates the fn ptr, no weird fn with weird
|
||||||
// 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_usize") }.map(V::Usize));
|
||||||
untry!(unsafe { handle.symfn::<C<(usize, usize)>>(c"challenge_isize_duple") }.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_duple") }.map(V::IsizeDuple));
|
untry!(unsafe { handle.symfn::<C<(isize, isize)>>(c"challenge_usize_duple") }.map(V::IsizeDuple));
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user