Add day 2

This commit is contained in:
dolphinau 2025-12-04 11:34:09 +01:00
parent 3b4dcd6ee8
commit 34523aebca
No known key found for this signature in database

View file

@ -0,0 +1,51 @@
use crate::days::Solution;
pub struct Day02;
impl Solution for Day02 {
type Input = Vec<(usize, usize)>;
fn parse(&self, data: &str) -> Self::Input {
data.split("\n")
.next()
.unwrap()
.split(",")
.map(|s| {
let mut ints = s.split("-").map(|x| x.parse::<usize>());
(ints.next().unwrap().unwrap(), ints.next().unwrap().unwrap())
})
.collect()
}
fn part1(&self, input: &Self::Input) -> usize {
input
.iter()
.map(|(start, end)| {
(*start..=*end)
.map(|x| x.to_string())
.filter(|s| {
let len = s.len();
len % 2 == 0 && s[..len / 2] == s[len / 2..]
})
.map(|s| s.parse::<usize>().unwrap())
.sum::<usize>()
})
.sum()
}
fn part2(&self, input: &Self::Input) -> usize {
input
.iter()
.map(|(start, end)| {
(*start..=*end)
.map(|x| x.to_string())
.filter(|s| {
let len = s.len();
(1..=len / 2).any(|n| len % n == 0 && s == &s[..n].repeat(len / n))
})
.map(|s| s.parse::<usize>().unwrap())
.sum::<usize>()
})
.sum()
}
}