Add day 6
This commit is contained in:
parent
35baa10471
commit
379a0229ae
3 changed files with 103 additions and 0 deletions
101
AoC_2025/src/days/day06.rs
Normal file
101
AoC_2025/src/days/day06.rs
Normal file
|
|
@ -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<Vec<&str>> = 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::<usize>().unwrap())
|
||||
.collect::<Vec<usize>>(),
|
||||
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<String> = 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::<String>();
|
||||
|
||||
buffer.push(x.trim().parse::<usize>().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
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ pub mod day02;
|
|||
pub mod day03;
|
||||
pub mod day04;
|
||||
pub mod day05;
|
||||
pub mod day06;
|
||||
|
||||
pub trait Solution {
|
||||
type Input;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue