mirror of
https://github.com/LeMoonStar/AoC24.git
synced 2025-07-07 16:05:31 +02:00
✨ Day 7: Part 2
This commit is contained in:
parent
0ff7e66083
commit
28e2a563c0
2 changed files with 14 additions and 15 deletions
|
@ -3,8 +3,8 @@
|
||||||
[](https://adventofcode.com/2024/about)
|
[](https://adventofcode.com/2024/about)
|
||||||
[](https://en.wikipedia.org/wiki/Rust_(programming_language))
|
[](https://en.wikipedia.org/wiki/Rust_(programming_language))
|
||||||
[](https://mit-license.org/)
|
[](https://mit-license.org/)
|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
> ⚠️ 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.
|
||||||
|
|
||||||
|
|
|
@ -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,14 +86,11 @@ 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],
|
.map(|o| self.calculate(o))
|
||||||
(self.parts.len() - 1) as u32,
|
.collect::<Vec<_>>()
|
||||||
)
|
.contains(&self.result)
|
||||||
.map(|o| self.calculate(o))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.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]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue