mirror of
https://github.com/javalsai/aoc.git
synced 2026-01-13 09:30:01 +01:00
add: d03
This commit is contained in:
34
2025/03/p1.rs
Normal file
34
2025/03/p1.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use std::cmp::Ordering;
|
||||
|
||||
fn cmp_with_eq_as_gt<T: Ord>(a: &T, b: &T) -> Ordering {
|
||||
let order = a.cmp(b);
|
||||
if order == Ordering::Equal {
|
||||
Ordering::Greater
|
||||
} else {
|
||||
order
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
fn challenge_usize(buf: &[u8]) -> usize {
|
||||
let mut total_joltage = 0;
|
||||
|
||||
let s = unsafe { str::from_utf8_unchecked(buf) };
|
||||
for array in s.trim().split('\n') {
|
||||
let bats = array.as_bytes();
|
||||
let bats_but_last = &bats[0..(bats.len() - 1)];
|
||||
|
||||
let first_dig = bats_but_last
|
||||
.iter()
|
||||
.enumerate()
|
||||
.max_by(|a, b| cmp_with_eq_as_gt(a.1, b.1))
|
||||
.unwrap();
|
||||
let last_dig = bats[(first_dig.0 + 1)..].iter().max().unwrap();
|
||||
|
||||
println!("-->> {first_dig:?} {last_dig}");
|
||||
let arr_max_jolts = ((first_dig.1 - b'0') * 10) + (last_dig - b'0');
|
||||
total_joltage += Into::<usize>::into(arr_max_jolts);
|
||||
}
|
||||
|
||||
total_joltage
|
||||
}
|
||||
Reference in New Issue
Block a user