diff --git a/README.md b/README.md index 4594226f..c243ed64 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,15 @@ ## 2024 -![](https://img.shields.io/badge/stars%20⭐-8-yellow) -![](https://img.shields.io/badge/days%20completed-4-red) +![](https://img.shields.io/badge/stars%20⭐-10-yellow) +![](https://img.shields.io/badge/days%20completed-5-red) | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | | ---| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | python3 | [✓](src/main/python/AoC2024_01.py) | [✓](src/main/python/AoC2024_02.py) | [✓](src/main/python/AoC2024_03.py) | [✓](src/main/python/AoC2024_04.py) | [✓](src/main/python/AoC2024_05.py) | | | | | | | | | | | | | | | | | | | | | | java | [✓](src/main/java/AoC2024_01.java) | [✓](src/main/java/AoC2024_02.java) | [✓](src/main/java/AoC2024_03.java) | [✓](src/main/java/AoC2024_04.java) | [✓](src/main/java/AoC2024_05.java) | | | | | | | | | | | | | | | | | | | | | -| rust | [✓](src/main/rust/AoC2024_01/src/main.rs) | [✓](src/main/rust/AoC2024_02/src/main.rs) | [✓](src/main/rust/AoC2024_03/src/main.rs) | [✓](src/main/rust/AoC2024_04/src/main.rs) | | | | | | | | | | | | | | | | | | | | | | +| rust | [✓](src/main/rust/AoC2024_01/src/main.rs) | [✓](src/main/rust/AoC2024_02/src/main.rs) | [✓](src/main/rust/AoC2024_03/src/main.rs) | [✓](src/main/rust/AoC2024_04/src/main.rs) | [✓](src/main/rust/AoC2024_05/src/main.rs) | | | | | | | | | | | | | | | | | | | | | ## 2023 diff --git a/src/main/rust/AoC2024_05/Cargo.toml b/src/main/rust/AoC2024_05/Cargo.toml new file mode 100644 index 00000000..6c565845 --- /dev/null +++ b/src/main/rust/AoC2024_05/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "AoC2024_05" +version = "0.1.0" +edition = "2021" + +[dependencies] +aoc = { path = "../aoc" } diff --git a/src/main/rust/AoC2024_05/src/main.rs b/src/main/rust/AoC2024_05/src/main.rs new file mode 100644 index 00000000..e935614c --- /dev/null +++ b/src/main/rust/AoC2024_05/src/main.rs @@ -0,0 +1,131 @@ +#![allow(non_snake_case)] + +use aoc::Puzzle; +use std::cmp::Ordering; +use std::collections::HashMap; + +enum Mode { + UseCorrect, + UseIncorrect, +} + +struct AoC2024_05; + +impl AoC2024_05 { + fn solve( + &self, + input: &::Input, + mode: Mode, + ) -> u32 { + let (order, updates) = input; + let mut ans = 0; + for update in updates { + let mut correct = update.to_vec(); + correct.sort_by(|a, b| { + match order.get(a).unwrap_or(&vec![]).contains(b) { + true => Ordering::Less, + false => Ordering::Greater, + } + }); + match mode { + Mode::UseCorrect => { + if *update == correct { + ans += correct[correct.len() / 2] + } + } + Mode::UseIncorrect => { + if *update != correct { + ans += correct[correct.len() / 2] + } + } + } + } + ans + } +} + +impl aoc::Puzzle for AoC2024_05 { + type Input = (HashMap>, Vec>); + type Output1 = u32; + type Output2 = u32; + + aoc::puzzle_year_day!(2024, 5); + + fn parse_input(&self, lines: Vec) -> Self::Input { + let blocks = aoc::to_blocks(&lines); + let mut order: HashMap> = HashMap::new(); + for line in &blocks[0] { + let (sa, sb) = line.split_once("|").unwrap(); + let (a, b) = + (sa.parse::().unwrap(), sb.parse::().unwrap()); + order.entry(a).and_modify(|e| e.push(b)).or_insert(vec![b]); + } + let updates = blocks[1] + .iter() + .map(|line| { + line.split(",").map(|n| n.parse::().unwrap()).collect() + }) + .collect(); + (order, updates) + } + + fn part_1(&self, input: &Self::Input) -> Self::Output1 { + self.solve(input, Mode::UseCorrect) + } + + fn part_2(&self, input: &Self::Input) -> Self::Output2 { + self.solve(input, Mode::UseIncorrect) + } + + fn samples(&self) { + aoc::puzzle_samples! { + self, part_1, TEST, 143, + self, part_2, TEST, 123 + }; + } +} + +fn main() { + AoC2024_05 {}.run(std::env::args()); +} + +const TEST: &str = "\ +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 +"; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + pub fn samples() { + AoC2024_05 {}.samples(); + } +} diff --git a/src/main/rust/Cargo.lock b/src/main/rust/Cargo.lock index f0f61d5c..3118b2bc 100644 --- a/src/main/rust/Cargo.lock +++ b/src/main/rust/Cargo.lock @@ -418,6 +418,13 @@ dependencies = [ "aoc", ] +[[package]] +name = "AoC2024_05" +version = "0.1.0" +dependencies = [ + "aoc", +] + [[package]] name = "aho-corasick" version = "1.0.2"