Add day 3
This commit is contained in:
parent
34523aebca
commit
879272687b
3 changed files with 55 additions and 0 deletions
53
AoC_2025/src/days/day03.rs
Normal file
53
AoC_2025/src/days/day03.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
use crate::days::Solution;
|
||||||
|
|
||||||
|
pub struct Day03;
|
||||||
|
|
||||||
|
impl Solution for Day03 {
|
||||||
|
type Input = Vec<Vec<u32>>;
|
||||||
|
|
||||||
|
fn parse(&self, data: &str) -> Self::Input {
|
||||||
|
data.split("\n")
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(|s| s.chars().map(|c| c.to_digit(10).unwrap_or(0)).collect())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(&self, input: &Self::Input) -> usize {
|
||||||
|
input
|
||||||
|
.iter()
|
||||||
|
.map(|l| {
|
||||||
|
let (i1, m1) = max(&l[..l.len() - 1]);
|
||||||
|
let (_, m2) = max(&l[i1 + 1..]);
|
||||||
|
10 * m1 + m2
|
||||||
|
})
|
||||||
|
.sum::<u32>() as usize
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(&self, input: &Self::Input) -> usize {
|
||||||
|
println!("Part2");
|
||||||
|
input
|
||||||
|
.iter()
|
||||||
|
.map(|l| {
|
||||||
|
(0..12)
|
||||||
|
.rfold((0, 0), |(start_index, joltage), n| {
|
||||||
|
let (i, m) = max(&l[start_index..l.len() - n]);
|
||||||
|
(
|
||||||
|
start_index + i + 1,
|
||||||
|
usize::pow(10, n as u32) * m as usize + joltage,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.1
|
||||||
|
})
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn max(l: &[u32]) -> (usize, u32) {
|
||||||
|
let x = l.iter().enumerate().fold(
|
||||||
|
(0, 0),
|
||||||
|
|(index_m, m), (i, &x)| if x > m { (i, x) } else { (index_m, m) },
|
||||||
|
);
|
||||||
|
|
||||||
|
println!("Max of {:?} is {:?}", l, x);
|
||||||
|
x
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
pub mod day01;
|
pub mod day01;
|
||||||
pub mod day02;
|
pub mod day02;
|
||||||
|
pub mod day03;
|
||||||
|
|
||||||
pub trait Solution {
|
pub trait Solution {
|
||||||
type Input;
|
type Input;
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ fn main() {
|
||||||
match cli.day {
|
match cli.day {
|
||||||
1 => run(cli.day, days::day01::Day01),
|
1 => run(cli.day, days::day01::Day01),
|
||||||
2 => run(cli.day, days::day02::Day02),
|
2 => run(cli.day, days::day02::Day02),
|
||||||
|
3 => run(cli.day, days::day03::Day03),
|
||||||
_ => {
|
_ => {
|
||||||
eprintln!("Day {:02} is not implemented yet!", cli.day);
|
eprintln!("Day {:02} is not implemented yet!", cli.day);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue