Advent of Code solutions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Day 5

Ben C 90981006 8a3ec5d4

+127 -5
+71 -5
years/2024/src/day_5.rs
··· 1 + 2 + use std::collections::{HashMap, HashSet}; 1 3 2 4 use advent_core::{Day, day_stuff, ex_for_day}; 3 5 4 6 pub struct Day5; 5 7 8 + #[derive(Clone, Debug)] 9 + pub struct Rules(HashMap<i64,Vec<i64>>); 10 + 11 + impl Rules { 12 + pub fn take(&mut self, lhs: i64, rhs: i64) { 13 + self.0.entry(lhs).and_modify(|seen| { seen.push(rhs); }).or_insert(vec![rhs]); 14 + } 15 + 16 + fn good(&self, up: &[i64]) -> bool { 17 + let mut seen = HashSet::new(); 18 + for u in up.iter() { 19 + if self.0.get(u).is_none_or(|should| should.iter().all(|s| !seen.contains(s))) { 20 + seen.insert(*u); 21 + } else { 22 + return false; 23 + } 24 + } 25 + true 26 + } 27 + 28 + fn sort_to_rules(&self, up: &mut[i64]) { 29 + while !self.good(up) { 30 + self.sort_step(up); 31 + } 32 + } 33 + 34 + fn sort_step(&self, up: &mut[i64]) { 35 + for i in 0..up.len()-1 { 36 + for j in i+1..up.len() { 37 + if self.0.get(&up[j]).is_some_and(|shld| shld.contains(&up[i])) { 38 + up.swap(i, j); 39 + } 40 + } 41 + } 42 + } 43 + } 44 + 6 45 impl Day for Day5 { 7 46 8 - day_stuff!(5, "", ""); 47 + day_stuff!(5, "143", "123", (Rules, Vec<Vec<i64>>)); 9 48 10 - fn part_1(_input: Self::Input) -> Option<String> { 11 - None 49 + fn part_1(input: Self::Input) -> Option<String> { 50 + let (rules, updates) = input; 51 + let ans = updates.into_iter().filter_map(|up| { 52 + if rules.good(&up) { 53 + Some(up[up.len() / 2]) 54 + } else { 55 + None 56 + } 57 + }).sum::<i64>(); 58 + Some(ans.to_string()) 12 59 } 13 60 14 - fn part_2(_input: Self::Input) -> Option<String> { 15 - None 61 + fn part_2(input: Self::Input) -> Option<String> { 62 + let (rules, updates) = input; 63 + let ans = updates.into_iter().filter_map(|mut up| { 64 + if !rules.good(&up) { 65 + rules.sort_to_rules(&mut up); 66 + Some(up[up.len() / 2]) 67 + } else { 68 + None 69 + } 70 + }).sum::<i64>(); 71 + Some(ans.to_string()) 72 + } 73 + 74 + fn parse_input(input: &str) -> Self::Input { 75 + let (rules, updates) = input.split_once("\n\n").unwrap(); 76 + let mut rules_o = Rules(HashMap::with_capacity(50)); 77 + for l in rules.lines() { 78 + let (lhs, rhs) = l.split_once('|').unwrap(); 79 + rules_o.take(lhs.parse().unwrap(), rhs.parse().unwrap()); 80 + } 81 + (rules_o, updates.split('\n').map(|l| l.split(',').map(|x| x.parse().unwrap()).collect()).collect()) 16 82 } 17 83 }
+28
years/2024/src/examples/day_5/1.txt
··· 1 + 47|53 2 + 97|13 3 + 97|61 4 + 97|47 5 + 75|29 6 + 61|13 7 + 75|53 8 + 29|13 9 + 97|29 10 + 53|29 11 + 61|53 12 + 97|53 13 + 61|29 14 + 47|13 15 + 75|47 16 + 97|75 17 + 47|61 18 + 75|61 19 + 47|29 20 + 75|13 21 + 53|13 22 + 23 + 75,47,61,53,29 24 + 97,61,53,29,13 25 + 75,29,13 26 + 75,97,47,61,53 27 + 61,13,29 28 + 97,13,75,29,47
+28
years/2024/src/examples/day_5/2.txt
··· 1 + 47|53 2 + 97|13 3 + 97|61 4 + 97|47 5 + 75|29 6 + 61|13 7 + 75|53 8 + 29|13 9 + 97|29 10 + 53|29 11 + 61|53 12 + 97|53 13 + 61|29 14 + 47|13 15 + 75|47 16 + 97|75 17 + 47|61 18 + 75|61 19 + 47|29 20 + 75|13 21 + 53|13 22 + 23 + 75,47,61,53,29 24 + 97,61,53,29,13 25 + 75,29,13 26 + 75,97,47,61,53 27 + 61,13,29 28 + 97,13,75,29,47