This commit is contained in:
2025-12-02 18:53:02 +00:00
parent 784d15bedb
commit 5b1f51ab4e
4 changed files with 95 additions and 0 deletions

89
src/day2/main.rs Normal file
View File

@@ -0,0 +1,89 @@
use anyhow::Result;
use std::fs;
fn sol1(text: &String) {
let sum = text
.split("\n")
.nth(0)
.unwrap()
.split(",")
.filter(|s| s.len() > 0)
.fold(0, |sum, ids| {
let mut range = ids.split('-');
let start: i64 = range.next().unwrap().parse().unwrap();
let end: i64 = range.next().unwrap().parse().unwrap();
let mut to_sum: i64 = 0;
for i in start..(end + 1) {
// TODO maybe skip entire ones if the start and end are the same size and cannot have anything inside
if (i as f64).log10().ceil() as i64 % 2 == 0 {
let str = i.to_string();
let pivot = str.len() / 2;
if str[0..pivot] == str[pivot..] {
// println!("Found wrong id: {}", i);
to_sum += i;
}
}
}
sum + to_sum
});
println!("sol1: {}", sum);
}
fn sol2(text: &String) {
let sum = text
.split("\n")
.nth(0)
.unwrap()
.split(",")
.filter(|s| s.len() > 0)
.fold(0, |sum, ids| {
let mut range = ids.split('-');
let start: i64 = range.next().unwrap().parse().unwrap();
let end: i64 = range.next().unwrap().parse().unwrap();
let mut to_sum: i64 = 0;
'bigloop: for i in start..(end + 1) {
// println!("Started Num: {}", i);
let str = i.to_string();
let str_len = str.len();
'other_num: for test_num in 1..(str_len / 2) + 1 {
if str_len % test_num != 0 {
continue 'other_num;
}
for test_index in (test_num..str_len).step_by(test_num) {
let prev = test_index - test_num;
/*println!(
"Testing: {} {} {} != {}",
test_index,
test_num,
str[prev..prev + test_num].to_owned(),
str[test_index..test_index + test_num].to_owned()
);*/
if str[prev..prev + test_num] != str[test_index..test_index + test_num] {
// println!("SKIP");
continue 'other_num;
}
}
println!("Found wrong id: {}", i);
to_sum += i;
continue 'bigloop;
}
}
sum + to_sum
});
println!("sol1: {}", sum);
}
fn main() -> Result<()> {
let text = fs::read_to_string("src/day2/day2-act.txt")?;
sol1(&text);
sol2(&text);
Ok(())
}