Deel 1 van dag 5 aoc af, verder op pc
This commit is contained in:
7
advent_of_code/2024/5/Cargo.toml
Normal file
7
advent_of_code/2024/5/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
nom = "7.1.3"
|
||||||
1359
advent_of_code/2024/5/input.txt
Normal file
1359
advent_of_code/2024/5/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
102
advent_of_code/2024/5/src/main.rs
Normal file
102
advent_of_code/2024/5/src/main.rs
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
|
use nom::{
|
||||||
|
bytes::complete::tag,
|
||||||
|
character::complete::{multispace1, u32},
|
||||||
|
multi::separated_list1,
|
||||||
|
sequence::separated_pair,
|
||||||
|
IResult,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn parse(input: &str) -> IResult<&str, (Vec<(u32, u32)>, Vec<Vec<u32>>)> {
|
||||||
|
let (input, result) = separated_pair(
|
||||||
|
separated_list1(multispace1, separated_pair(u32, tag("|"), u32)),
|
||||||
|
multispace1,
|
||||||
|
separated_list1(multispace1, separated_list1(tag(","), u32)),
|
||||||
|
)(input)?;
|
||||||
|
|
||||||
|
Ok((input, result))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_update(updates: &Vec<u32>, rules: &HashMap<u32, HashSet<u32>>) -> bool {
|
||||||
|
let mut forbidden: HashSet<u32> = HashSet::new();
|
||||||
|
|
||||||
|
updates.iter().all(|page| {
|
||||||
|
let lookup = forbidden.contains(page);
|
||||||
|
|
||||||
|
match rules.get(page) {
|
||||||
|
None => {}
|
||||||
|
Some(s) => {
|
||||||
|
for excl in s {
|
||||||
|
forbidden.insert(*excl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
!lookup
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_1(input: &str) -> u32 {
|
||||||
|
let (_, (rules, updates)) = parse(input).unwrap();
|
||||||
|
|
||||||
|
let mut exclusions: HashMap<u32, HashSet<u32>> = HashMap::new();
|
||||||
|
for (before, after) in rules.into_iter() {
|
||||||
|
match exclusions.get_mut(&after) {
|
||||||
|
None => {
|
||||||
|
exclusions.insert(after, HashSet::from([before]));
|
||||||
|
}
|
||||||
|
Some(s) => {
|
||||||
|
s.insert(before);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
updates.into_iter().fold(0, |sum, update| {
|
||||||
|
sum + if check_update(&update, &exclusions) {
|
||||||
|
*update.get(update.len() / 2).unwrap()
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_2(input: &str) -> u32 {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, this is Patrick!");
|
||||||
|
|
||||||
|
let input = include_str!("../input.txt");
|
||||||
|
|
||||||
|
let result_1 = solve_1(input);
|
||||||
|
println!(
|
||||||
|
"The sum of correctly ordered middle page updates is {}",
|
||||||
|
result_1
|
||||||
|
);
|
||||||
|
|
||||||
|
let result_2 = solve_2(input);
|
||||||
|
println!(
|
||||||
|
"The sum of the incorrectly ordered middle page updates is {}",
|
||||||
|
result_2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_1() {
|
||||||
|
let test_input = include_str!("../test.txt");
|
||||||
|
assert_eq!(solve_1(test_input), 143);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_2() {
|
||||||
|
let test_input = include_str!("../test.txt");
|
||||||
|
assert_eq!(solve_2(test_input), 123);
|
||||||
|
}
|
||||||
|
}
|
||||||
28
advent_of_code/2024/5/test.txt
Normal file
28
advent_of_code/2024/5/test.txt
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user