Day 7: Part 2

This commit is contained in:
LeMoonStar 2024-12-08 00:15:18 +01:00
parent 0ff7e66083
commit 28e2a563c0
2 changed files with 14 additions and 15 deletions

View file

@ -3,8 +3,8 @@
[![About](https://img.shields.io/badge/Advent%20of%20Code-2024-brightgreen?style=flat-square)](https://adventofcode.com/2024/about) [![About](https://img.shields.io/badge/Advent%20of%20Code-2024-brightgreen?style=flat-square)](https://adventofcode.com/2024/about)
[![Language: Rust](https://img.shields.io/badge/Language-Rust-orange.svg?style=flat-square)](https://en.wikipedia.org/wiki/Rust_(programming_language)) [![Language: Rust](https://img.shields.io/badge/Language-Rust-orange.svg?style=flat-square)](https://en.wikipedia.org/wiki/Rust_(programming_language))
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://mit-license.org/) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://mit-license.org/)
![Days completed](https://img.shields.io/badge/Days%20completed-4.5-red?style=flat-square) ![Days completed](https://img.shields.io/badge/Days%20completed-5%20%2B%202%20half-red?style=flat-square)
![Stars](https://img.shields.io/badge/Stars-9-yellow?style=flat-square) ![Stars](https://img.shields.io/badge/Stars-12-yellow?style=flat-square)
> ⚠️ This README is copied from my previous years solution. It is not fully adopted to 2024 yet. > ⚠️ This README is copied from my previous years solution. It is not fully adopted to 2024 yet.

View file

@ -6,6 +6,7 @@ const CURRENT_DAY: u8 = 7;
enum Operator { enum Operator {
Add, Add,
Multiply, Multiply,
Concat,
} }
impl Operator { impl Operator {
@ -13,6 +14,7 @@ impl Operator {
match self { match self {
Operator::Multiply => a * b, Operator::Multiply => a * b,
Operator::Add => a + b, Operator::Add => a + b,
Operator::Concat => format!("{}{}", a, b).parse().unwrap(),
} }
} }
} }
@ -84,11 +86,8 @@ impl CalibrationEquation {
value value
} }
fn can_be_valid(&self) -> bool { fn can_be_valid(&self, operations: &[Operator]) -> bool {
CombinationIterator::new( CombinationIterator::new(operations, (self.parts.len() - 1) as u32)
&[Operator::Add, Operator::Multiply],
(self.parts.len() - 1) as u32,
)
.map(|o| self.calculate(o)) .map(|o| self.calculate(o))
.collect::<Vec<_>>() .collect::<Vec<_>>()
.contains(&self.result) .contains(&self.result)
@ -116,10 +115,10 @@ pub struct Calibrator {
} }
impl Calibrator { impl Calibrator {
pub fn get_valid_sum(&self) -> u64 { fn get_valid_sum(&self, operators: &[Operator]) -> u64 {
self.equations self.equations
.iter() .iter()
.filter(|v| v.can_be_valid()) .filter(|v| v.can_be_valid(operators))
.map(|v| v.result) .map(|v| v.result)
.sum() .sum()
} }
@ -140,7 +139,7 @@ impl DayImpl<Data> for Day<CURRENT_DAY> {
} }
fn expected_results() -> (Answer, Answer) { fn expected_results() -> (Answer, Answer) {
(Answer::Number(3749), Answer::Number(0)) (Answer::Number(3749), Answer::Number(11387))
} }
fn init(input: &str) -> (Self, Data) { fn init(input: &str) -> (Self, Data) {
@ -148,10 +147,10 @@ impl DayImpl<Data> for Day<CURRENT_DAY> {
} }
fn one(&self, data: &mut Data) -> Answer { fn one(&self, data: &mut Data) -> Answer {
Answer::Number(data.get_valid_sum()) Answer::Number(data.get_valid_sum(&[Operator::Add, Operator::Multiply]))
} }
fn two(&self, data: &mut Data) -> Answer { fn two(&self, data: &mut Data) -> Answer {
Answer::Number(0) Answer::Number(data.get_valid_sum(&[Operator::Add, Operator::Multiply, Operator::Concat]))
} }
} }