Compare commits

...

2 Commits

Author SHA1 Message Date
f2131a114f Day 2 - Cleanup 2025-12-02 19:08:24 +00:00
5b1f51ab4e Day 2 2025-12-02 18:53:02 +00:00
4 changed files with 84 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

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

@@ -0,0 +1,78 @@
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) {
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..] {
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) {
let str = i.to_string();
let str_len = str.len();
'other_num: for test_num in (1..(str_len / 2) + 1).rev() {
if str_len % test_num != 0 {
continue 'other_num;
}
for test_index in (test_num..str_len).step_by(test_num).rev() {
let prev = test_index - test_num;
if str[prev..prev + test_num] != str[test_index..test_index + test_num] {
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(())
}