Day 5: Solved part 2

This commit is contained in:
LeMoonStar 2024-12-05 21:11:05 +01:00
parent 852537790f
commit 26a30f0669
2 changed files with 48 additions and 6 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-3.5-red?style=flat-square) ![Days completed](https://img.shields.io/badge/Days%20completed-4.5-red?style=flat-square)
![Stars](https://img.shields.io/badge/Stars-7-yellow?style=flat-square) ![Stars](https://img.shields.io/badge/Stars-9-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

@ -28,7 +28,7 @@ pub struct UpdateSequence(Vec<u64>);
impl UpdateSequence { impl UpdateSequence {
fn matches_rules(&self, rules: &Vec<PageOrderingRule>) -> bool { fn matches_rules(&self, rules: &Vec<PageOrderingRule>) -> bool {
let mut contained: BTreeSet<u64> = self.0.iter().map(|v| *v).collect(); let contained: BTreeSet<u64> = self.0.iter().map(|v| *v).collect();
let mut seen = BTreeSet::new(); let mut seen = BTreeSet::new();
for page_number in &self.0 { for page_number in &self.0 {
@ -39,7 +39,6 @@ impl UpdateSequence {
&& !seen.contains(&v.first) && !seen.contains(&v.first)
&& contained.contains(&v.first) && contained.contains(&v.first)
}) })
.inspect(|v| println!("{:?}", v))
.count() .count()
> 0 > 0
{ {
@ -54,6 +53,42 @@ impl UpdateSequence {
fn get_middle_page_number(&self) -> u64 { fn get_middle_page_number(&self) -> u64 {
self.0[self.0.len() / 2] self.0[self.0.len() / 2]
} }
fn get_rule_conforming(&self, rules: &Vec<PageOrderingRule>) -> Self {
let mut sorted: Vec<u64> = Vec::with_capacity(self.0.len());
let contained: BTreeSet<u64> = self.0.iter().map(|v| *v).collect();
let mut numbers_with_edges = self
.0
.iter()
.map(|v| {
(
*v,
rules
.iter()
.filter(|r| r.second == *v)
.filter(|r| contained.contains(&r.first))
.map(|r| r.first)
.collect::<Vec<u64>>(),
)
})
.collect::<Vec<(u64, Vec<u64>)>>();
numbers_with_edges.sort_by(|a, b| a.1.len().cmp(&b.1.len()));
for (number, edges) in numbers_with_edges {
for edge in edges {
if !sorted.contains(&edge) {
sorted.push(edge);
}
}
if !sorted.contains(&number) {
sorted.push(number);
}
}
Self(sorted)
}
} }
impl From<&str> for UpdateSequence { impl From<&str> for UpdateSequence {
@ -86,7 +121,7 @@ impl DayImpl<Data> for Day<CURRENT_DAY> {
} }
fn expected_results() -> (Answer, Answer) { fn expected_results() -> (Answer, Answer) {
(Answer::Number(143), Answer::Number(0)) (Answer::Number(143), Answer::Number(123))
} }
fn init(input: &str) -> (Self, Data) { fn init(input: &str) -> (Self, Data) {
@ -104,6 +139,13 @@ impl DayImpl<Data> for Day<CURRENT_DAY> {
} }
fn two(&self, data: &mut Data) -> Answer { fn two(&self, data: &mut Data) -> Answer {
Answer::Number(0) data.updates[0].get_rule_conforming(&data.rules);
Answer::Number(
data.updates
.iter()
.filter(|v| !v.matches_rules(&data.rules))
.map(|v| v.get_rule_conforming(&data.rules).get_middle_page_number())
.sum(),
)
} }
} }