Add day 2
This commit is contained in:
parent
3b4dcd6ee8
commit
34523aebca
1 changed files with 51 additions and 0 deletions
51
AoC_2025/src/days/day02.rs
Normal file
51
AoC_2025/src/days/day02.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue