Add day 2 (rough version)

bram-benchmarks
Bram 2025-12-02 09:20:53 +01:00
parent 64ba806ee0
commit a278f98f4b
3 changed files with 52 additions and 5 deletions

View File

@ -0,0 +1,2 @@
8123221734-8123333968,2665-4538,189952-274622,4975-9031,24163352-24202932,1233-1772,9898889349-9899037441,2-15,2147801-2281579,296141-327417,8989846734-8989940664,31172-42921,593312-632035,862987-983007,613600462-613621897,81807088-81833878,13258610-13489867,643517-782886,986483-1022745,113493-167913,10677-16867,372-518,3489007333-3489264175,1858-2534,18547-26982,16-29,247-366,55547-103861,57-74,30-56,1670594-1765773,76-129,134085905-134182567,441436-566415,7539123416-7539252430,668-1146,581563513-581619699

View File

@ -1,5 +1,50 @@
use crate::utils;
use std::ops::RangeInclusive;
pub fn answer(s : String) -> ( u32, u32 ) {
( 0, 0 )
pub fn answer(text : String) -> ( u64, u64 ) {
( text.split(",").flat_map(to_range).filter(|x| is_invalid_by(x, 2)).sum()
, text.split(",").flat_map(to_range).filter(is_invalid).sum()
)
}
fn is_invalid(n : &u64) -> bool {
for b in 2..=10 {
if is_invalid_by(n, b) {
return true;
}
}
false
}
fn is_invalid_by(n : &u64, by : usize) -> bool {
let s : String = n.to_string();
let size : usize = s.len() / by;
if s.len() % by == 0 {
for i in 1..by {
if s[0..size] != s[i * size..(i + 1) * size] {
return false;
}
}
true
} else {
false
}
}
fn to_range(s : &str) -> RangeInclusive<u64> {
let empty = 1..=0;
if let Some((low, high)) = s.split_once("-") {
match ( utils::str_to_u64(low), utils::str_to_u64(high) ) {
( Some(l), Some(h) ) =>
l..=h,
_ =>
// Empty iterator
empty,
}
} else {
empty
}
}

View File

@ -36,6 +36,6 @@ pub fn str_to_i16(s : &str) -> Option<i16> {
// s.trim().to_string().parse::<u32>().ok()
// }
// pub fn str_to_u64(s : &str) -> Option<u64> {
// s.trim().to_string().parse::<u64>().ok()
// }
pub fn str_to_u64(s : &str) -> Option<u64> {
s.trim().to_string().parse::<u64>().ok()
}