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

View File

@@ -7,5 +7,9 @@ edition = "2024"
name = "day1" name = "day1"
path = "src/day1/main.rs" path = "src/day1/main.rs"
[[bin]]
name = "day2"
path = "src/day2/main.rs"
[dependencies] [dependencies]
anyhow = "1.0.100" anyhow = "1.0.100"

1
src/day2/day2-act.txt Normal file
View File

@@ -0,0 +1 @@
67562556-67743658,62064792-62301480,4394592-4512674,3308-4582,69552998-69828126,9123-12332,1095-1358,23-48,294-400,3511416-3689352,1007333-1150296,2929221721-2929361280,309711-443410,2131524-2335082,81867-97148,9574291560-9574498524,648635477-648670391,1-18,5735-8423,58-72,538-812,698652479-698760276,727833-843820,15609927-15646018,1491-1766,53435-76187,196475-300384,852101-903928,73-97,1894-2622,58406664-58466933,6767640219-6767697605,523453-569572,7979723815-7979848548,149-216

1
src/day2/day2-test.txt Normal file
View File

@@ -0,0 +1 @@
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124

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(())
}