mirror of
https://github.com/LeMoonStar/AoC24.git
synced 2025-07-08 00:10:00 +02:00
✨ Day 5: Part 1 solved (ugly solution!)
This commit is contained in:
parent
d25a9aad57
commit
852537790f
2 changed files with 116 additions and 11 deletions
|
@ -1,32 +1,109 @@
|
|||
use std::collections::BTreeSet;
|
||||
|
||||
use super::{Answer, Day, DayImpl};
|
||||
|
||||
const CURRENT_DAY: u8 = 5;
|
||||
|
||||
type Data = Vec<u64>;
|
||||
// This would be much more efficient as a Map<u64, Vec<u64>>
|
||||
// But I don't know what Part 2 will require, so I'll keep this.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PageOrderingRule {
|
||||
first: u64,
|
||||
second: u64,
|
||||
}
|
||||
|
||||
impl From<&str> for PageOrderingRule {
|
||||
fn from(value: &str) -> Self {
|
||||
let (first, second) = value.split_once('|').unwrap();
|
||||
|
||||
Self {
|
||||
first: first.parse().unwrap(),
|
||||
second: second.parse().unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UpdateSequence(Vec<u64>);
|
||||
|
||||
impl UpdateSequence {
|
||||
fn matches_rules(&self, rules: &Vec<PageOrderingRule>) -> bool {
|
||||
let mut contained: BTreeSet<u64> = self.0.iter().map(|v| *v).collect();
|
||||
let mut seen = BTreeSet::new();
|
||||
|
||||
for page_number in &self.0 {
|
||||
if rules
|
||||
.iter()
|
||||
.filter(|v| {
|
||||
v.second == *page_number
|
||||
&& !seen.contains(&v.first)
|
||||
&& contained.contains(&v.first)
|
||||
})
|
||||
.inspect(|v| println!("{:?}", v))
|
||||
.count()
|
||||
> 0
|
||||
{
|
||||
return false;
|
||||
}
|
||||
seen.insert(page_number);
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn get_middle_page_number(&self) -> u64 {
|
||||
self.0[self.0.len() / 2]
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for UpdateSequence {
|
||||
fn from(value: &str) -> Self {
|
||||
Self(value.split(',').map(|v| v.parse().unwrap()).collect())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SafetyManualPrintOrder {
|
||||
rules: Vec<PageOrderingRule>,
|
||||
updates: Vec<UpdateSequence>,
|
||||
}
|
||||
|
||||
impl From<&str> for SafetyManualPrintOrder {
|
||||
fn from(value: &str) -> Self {
|
||||
let (rules_string, updates_string) = value.split_once("\n\n").unwrap();
|
||||
|
||||
Self {
|
||||
rules: rules_string.lines().map(|v| v.into()).collect(),
|
||||
updates: updates_string.lines().map(|v| v.into()).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Data = SafetyManualPrintOrder;
|
||||
impl DayImpl<Data> for Day<CURRENT_DAY> {
|
||||
fn init_test() -> (Self, Data) {
|
||||
Self::init(include_str!("test_inputs/test05.txt"))
|
||||
}
|
||||
|
||||
fn expected_results() -> (Answer, Answer) {
|
||||
(Answer::Number(0), Answer::Number(0))
|
||||
(Answer::Number(143), Answer::Number(0))
|
||||
}
|
||||
|
||||
fn init(input: &str) -> (Self, Data) {
|
||||
(
|
||||
Self {},
|
||||
input
|
||||
.lines()
|
||||
.map(|v| v.parse::<u64>().expect("error while parsing input."))
|
||||
.collect(),
|
||||
)
|
||||
(Self {}, input.into())
|
||||
}
|
||||
|
||||
fn one(&self, data: &mut Data) -> Answer {
|
||||
Answer::Number(data.len() as u64)
|
||||
Answer::Number(
|
||||
data.updates
|
||||
.iter()
|
||||
.filter(|v| v.matches_rules(&data.rules))
|
||||
.map(|v| v.get_middle_page_number())
|
||||
.sum(),
|
||||
)
|
||||
}
|
||||
|
||||
fn two(&self, data: &mut Data) -> Answer {
|
||||
Answer::Number(data.len() as u64)
|
||||
Answer::Number(0)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
47|53
|
||||
97|13
|
||||
97|61
|
||||
97|47
|
||||
75|29
|
||||
61|13
|
||||
75|53
|
||||
29|13
|
||||
97|29
|
||||
53|29
|
||||
61|53
|
||||
97|53
|
||||
61|29
|
||||
47|13
|
||||
75|47
|
||||
97|75
|
||||
47|61
|
||||
75|61
|
||||
47|29
|
||||
75|13
|
||||
53|13
|
||||
|
||||
75,47,61,53,29
|
||||
97,61,53,29,13
|
||||
75,29,13
|
||||
75,97,47,61,53
|
||||
61,13,29
|
||||
97,13,75,29,47
|
Loading…
Add table
Add a link
Reference in a new issue