From 879272687b14c264a6d4df4be49ff0a3b4ef5b09 Mon Sep 17 00:00:00 2001 From: dolphinau Date: Thu, 4 Dec 2025 12:24:05 +0100 Subject: [PATCH 1/2] Add day 3 --- AoC_2025/src/days/day03.rs | 53 ++++++++++++++++++++++++++++++++++++++ AoC_2025/src/days/mod.rs | 1 + AoC_2025/src/main.rs | 1 + 3 files changed, 55 insertions(+) create mode 100644 AoC_2025/src/days/day03.rs diff --git a/AoC_2025/src/days/day03.rs b/AoC_2025/src/days/day03.rs new file mode 100644 index 0000000..5bdce68 --- /dev/null +++ b/AoC_2025/src/days/day03.rs @@ -0,0 +1,53 @@ +use crate::days::Solution; + +pub struct Day03; + +impl Solution for Day03 { + type Input = Vec>; + + 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::() 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 +} diff --git a/AoC_2025/src/days/mod.rs b/AoC_2025/src/days/mod.rs index d16c304..3b80959 100644 --- a/AoC_2025/src/days/mod.rs +++ b/AoC_2025/src/days/mod.rs @@ -1,5 +1,6 @@ pub mod day01; pub mod day02; +pub mod day03; pub trait Solution { type Input; diff --git a/AoC_2025/src/main.rs b/AoC_2025/src/main.rs index f8c8e10..01667eb 100644 --- a/AoC_2025/src/main.rs +++ b/AoC_2025/src/main.rs @@ -35,6 +35,7 @@ fn main() { match cli.day { 1 => run(cli.day, days::day01::Day01), 2 => run(cli.day, days::day02::Day02), + 3 => run(cli.day, days::day03::Day03), _ => { eprintln!("Day {:02} is not implemented yet!", cli.day); process::exit(1); From 182cea15a7dd392dea5193a62c72ab758a3243cc Mon Sep 17 00:00:00 2001 From: dolphinau Date: Thu, 4 Dec 2025 13:16:32 +0100 Subject: [PATCH 2/2] Add day 04 part 1 --- AoC_2025/src/days/day04.rs | 66 ++++++++++++++++++++++++++++++++++++++ AoC_2025/src/days/mod.rs | 1 + AoC_2025/src/main.rs | 1 + 3 files changed, 68 insertions(+) create mode 100644 AoC_2025/src/days/day04.rs diff --git a/AoC_2025/src/days/day04.rs b/AoC_2025/src/days/day04.rs new file mode 100644 index 0000000..9d60026 --- /dev/null +++ b/AoC_2025/src/days/day04.rs @@ -0,0 +1,66 @@ +use crate::days::Solution; +use std::cmp::{max, min}; + +pub struct Day04; + +impl Solution for Day04 { + type Input = Vec>; + + fn parse(&self, data: &str) -> Self::Input { + data.split("\n") + .filter(|s| !s.is_empty()) + .map(|s| s.chars().map(|c| c == '@').collect()) + .collect() + } + + fn part1(&self, input: &Self::Input) -> usize { + input + .iter() + .enumerate() + .map(|(x, l)| { + l.iter() + .enumerate() + .filter(move |(y, v)| { + **v && neighbors((x, *y), input.len()) + .iter() + .filter(|(nx, ny)| input[*nx][*ny]) + .count() + < 4 + }) + .count() + }) + .sum() + } + + fn part2(&self, input: &Self::Input) -> usize { + 0 + } +} + +fn coord_in_range((x, y): (i32, i32), size: usize) -> (usize, usize) { + ( + min(max(x, 0) as usize, size - 1), + min(max(y, 0) as usize, size - 1), + ) +} + +fn neighbors((x, y): (usize, usize), size: usize) -> Vec<(usize, usize)> { + (0..=2) + .map(|i| i as i32 - 1) + .flat_map(|dx| { + (0..=2) + .map(|i| i as i32 - 1) + .filter_map(|dy| { + if dx != 0 || dy != 0 { + Some(coord_in_range((x as i32 + dx, y as i32 + dy), size)) + } else { + None + } + }) + .filter(|n| *n != (x, y)) + .collect::>() + }) + .collect::>() + .into_iter() + .collect::>() +} diff --git a/AoC_2025/src/days/mod.rs b/AoC_2025/src/days/mod.rs index 3b80959..0d5fd87 100644 --- a/AoC_2025/src/days/mod.rs +++ b/AoC_2025/src/days/mod.rs @@ -1,6 +1,7 @@ pub mod day01; pub mod day02; pub mod day03; +pub mod day04; pub trait Solution { type Input; diff --git a/AoC_2025/src/main.rs b/AoC_2025/src/main.rs index 01667eb..3c957b5 100644 --- a/AoC_2025/src/main.rs +++ b/AoC_2025/src/main.rs @@ -36,6 +36,7 @@ fn main() { 1 => run(cli.day, days::day01::Day01), 2 => run(cli.day, days::day02::Day02), 3 => run(cli.day, days::day03::Day03), + 4 => run(cli.day, days::day04::Day04), _ => { eprintln!("Day {:02} is not implemented yet!", cli.day); process::exit(1);