Skip to content

Commit

Permalink
AoC 2024 Day 5 - rust
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Dec 5, 2024
1 parent 76a0eed commit 7979ee7
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<!-- @BEGIN:ImplementationsTable:2024@ -->
| | 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) | | | | | | | | | | | | | | | | | | | | |
<!-- @END:ImplementationsTable:2024@ -->

## 2023
Expand Down
7 changes: 7 additions & 0 deletions src/main/rust/AoC2024_05/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "AoC2024_05"
version = "0.1.0"
edition = "2021"

[dependencies]
aoc = { path = "../aoc" }
131 changes: 131 additions & 0 deletions src/main/rust/AoC2024_05/src/main.rs
Original file line number Diff line number Diff line change
@@ -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: &<AoC2024_05 as aoc::Puzzle>::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<u32, Vec<u32>>, Vec<Vec<u32>>);
type Output1 = u32;
type Output2 = u32;

aoc::puzzle_year_day!(2024, 5);

fn parse_input(&self, lines: Vec<String>) -> Self::Input {
let blocks = aoc::to_blocks(&lines);
let mut order: HashMap<u32, Vec<u32>> = HashMap::new();
for line in &blocks[0] {
let (sa, sb) = line.split_once("|").unwrap();
let (a, b) =
(sa.parse::<u32>().unwrap(), sb.parse::<u32>().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::<u32>().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();
}
}
7 changes: 7 additions & 0 deletions src/main/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7979ee7

Please sign in to comment.