Skip to content

Commit

Permalink
feat: day 5 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
inglor committed Dec 6, 2024
1 parent 646ebfa commit ef307dc
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 5 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `46.9µs` | `115.3µs` |
| [Day 2](./src/bin/02.rs) | `75.9µs` | `124.8µs` |
| [Day 3](./src/bin/03.rs) | `69.4µs` | `90.7µs` |
| [Day 4](./src/bin/04.rs) | `2.7ms` | `54.1µs` |
| [Day 1](./src/bin/01.rs) | `45.6µs` | `89.9µs` |
| [Day 2](./src/bin/02.rs) | `124.5µs` | `219.8µs` |
| [Day 3](./src/bin/03.rs) | `79.5µs` | `80.1µs` |
| [Day 4](./src/bin/04.rs) | `3.9ms` | `62.1µs` |
| [Day 5](./src/bin/05.rs) | `398.2µs` | `501.8µs` |

**Total: 3.28ms**
**Total: 5.50ms**
<!--- benchmarking table --->

---
Expand Down
28 changes: 28 additions & 0 deletions data/examples/05.txt
Original file line number Diff line number Diff line change
@@ -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
110 changes: 110 additions & 0 deletions src/bin/05.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use std::{
cmp::Ordering,
collections::{HashMap, HashSet},
};

advent_of_code::solution!(5);

pub fn part_one(input: &str) -> Option<u32> {
let mut sum = 0;
let (orders, updates) = read_protocols(input);
for update in updates {
let mut valid = true;
for i in 0..update.len() - 1 {
for j in i + 1..update.len() {
if let Some(values) = orders.get(&update[i]) {
if !values.contains(&update[j]) {
valid = false;
}
} else {
valid = false;
break;
}
}
}
if valid {
sum += update[update.len() / 2];
}
}
Some(sum)
}

pub fn part_two(input: &str) -> Option<u32> {
let mut sum = 0;
let (orders, updates) = read_protocols(input);
for update in updates {
let mut valid = true;
for i in 0..update.len() - 1 {
for j in i + 1..update.len() {
if let Some(values) = orders.get(&update[i]) {
if !values.contains(&update[j]) {
valid = false;
}
} else {
valid = false;
break;
}
}
}
if !valid {
sum += reorder(&orders, &update);
}
}
Some(sum)
}

fn read_protocols(content: &str) -> (HashMap<u32, HashSet<u32>>, Vec<Vec<u32>>) {
let (input_order, input_updates) = content.split_once("\n\n").unwrap();
(
input_order.lines().fold(HashMap::new(), |mut map, order| {
let (first, second) = order.trim().split_once('|').unwrap();
map.entry(first.parse().unwrap())
.or_default()
.insert(second.parse().unwrap());
map
}),
input_updates
.lines()
.map(|update| {
update
.trim()
.split(',')
.map(|num| num.parse().unwrap())
.collect()
})
.collect(),
)
}

fn reorder(orders: &HashMap<u32, HashSet<u32>>, update: &[u32]) -> u32 {
let mut sorted = update.to_owned();
sorted.sort_by(|a, b| {
if let Some(b_vals) = orders.get(b) {
if b_vals.contains(a) {
Ordering::Greater
} else {
Ordering::Less
}
} else {
Ordering::Less
}
});
sorted[update.len() / 2]
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(143));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(123));
}
}

0 comments on commit ef307dc

Please sign in to comment.