From 379a0229aefaa4f05e7d66a1d49372fb3900a812 Mon Sep 17 00:00:00 2001 From: dolphinau Date: Tue, 9 Dec 2025 23:29:48 +0100 Subject: [PATCH] Add day 6 --- AoC_2025/src/days/day06.rs | 101 +++++++++++++++++++++++++++++++++++++ AoC_2025/src/days/mod.rs | 1 + AoC_2025/src/main.rs | 1 + 3 files changed, 103 insertions(+) create mode 100644 AoC_2025/src/days/day06.rs diff --git a/AoC_2025/src/days/day06.rs b/AoC_2025/src/days/day06.rs new file mode 100644 index 0000000..086cf00 --- /dev/null +++ b/AoC_2025/src/days/day06.rs @@ -0,0 +1,101 @@ +use std::{fmt::Debug, path::Display, str::Chars}; + +use crate::days::Solution; + +pub enum Operation { + Sum, + Mul, + Unk, +} + +impl From<&str> for Operation { + fn from(value: &str) -> Self { + match value { + "*" => Operation::Mul, + "+" => Operation::Sum, + _ => Operation::Unk, + } + } +} + +impl Debug for Operation { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Sum => write!(f, "Sum"), + Self::Mul => write!(f, "Mul"), + Self::Unk => write!(f, "Unk"), + } + } +} + +pub struct Day06; + +impl Solution for Day06 { + type Input = String; + + fn parse(&self, data: &str) -> Self::Input { + data.to_owned() + } + + fn part1(&self, input: &Self::Input) -> usize { + let input: Vec> = input + .trim() + .split("\n") + .map(|l| l.split(" ").filter(|s| !s.is_empty()).collect()) + .collect(); + + let problem_count = input[0].len(); + let problem_size = input.len() - 1; + + (0..problem_count) + .map(|i| { + ( + input + .iter() + .take(problem_size) + .map(|l| l[i].parse::().unwrap()) + .collect::>(), + Operation::from(input[problem_size][i]), + ) + }) + .map(|(l, op)| match op { + Operation::Mul => l.iter().fold(1, |acc, x| x * acc), + Operation::Sum => l.iter().sum(), + Operation::Unk => 0, + }) + .sum() + } + + fn part2(&self, input: &Self::Input) -> usize { + let mut lines: Vec = input.split('\n').map(|s| s.to_string()).collect(); + let mut op_line = lines.pop().unwrap(); + + println!("-{}", op_line); + + let mut res = 0; + let mut buffer = vec![0]; // Set to 0 to simulate separator column + while let Some(op) = op_line.pop() { + let x = lines + .iter_mut() + .map(|l| l.pop().unwrap()) + .collect::(); + + buffer.push(x.trim().parse::().unwrap_or_default()); + + if op != ' ' { + // Remove first element which is the separator column + _ = buffer.remove(0); + + res += if op == '+' { + buffer.iter().sum() + } else { + buffer.iter().fold(1, |acc, x| x * acc) + }; + + buffer.clear(); + } + } + + res + } +} diff --git a/AoC_2025/src/days/mod.rs b/AoC_2025/src/days/mod.rs index 9fe28f7..a54eb1d 100644 --- a/AoC_2025/src/days/mod.rs +++ b/AoC_2025/src/days/mod.rs @@ -3,6 +3,7 @@ pub mod day02; pub mod day03; pub mod day04; pub mod day05; +pub mod day06; pub trait Solution { type Input; diff --git a/AoC_2025/src/main.rs b/AoC_2025/src/main.rs index 80139eb..c15eb7a 100644 --- a/AoC_2025/src/main.rs +++ b/AoC_2025/src/main.rs @@ -38,6 +38,7 @@ fn main() { 3 => run(cli.day, days::day03::Day03), 4 => run(cli.day, days::day04::Day04), 5 => run(cli.day, days::day05::Day05), + 6 => run(cli.day, days::day06::Day06), _ => { eprintln!("Day {:02} is not implemented yet!", cli.day); process::exit(1);