Skip to content

Commit

Permalink
Added day 5 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
3rfaan committed Dec 6, 2024
1 parent 05222c2 commit c8e4556
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Empty file added data/examples/05.txt
Empty file.
89 changes: 89 additions & 0 deletions src/bin/05.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::collections::HashMap;

advent_of_code::solution!(5);

pub fn part_one(input: &str) -> Option<u32> {
let manual = Manual::from(input);
let mut sum = 0;

manual
.updates
.iter()
.filter(|&update| manual.is_correct_order(&update))
.for_each(|update| {
let mid = update.len() / 2;
sum += update[mid]
});

Some(sum)
}

pub fn part_two(input: &str) -> Option<u32> {
None
}

struct Manual {
rules: Vec<(u32, u32)>,
updates: Vec<Vec<u32>>,
}

impl Manual {
fn is_correct_order(&self, update: &Vec<u32>) -> bool {
let map: HashMap<u32, usize> = update
.iter()
.enumerate()
.map(|(idx, &page)| (page, idx))
.collect();

self.rules
.iter()
.all(|(x, y)| match (map.get(x), map.get(y)) {
(Some(&x), Some(&y)) => x < y,
_ => true,
})
}
}

impl From<&str> for Manual {
fn from(input: &str) -> Self {
let manual: Vec<&str> = input.split("\n\n").collect();
let (rules, updates) = (manual[0], manual[1]);

let rules: Vec<(u32, u32)> = rules
.lines()
.map(|rule| {
let parts: Vec<u32> = rule.split('|').filter_map(|num| num.parse().ok()).collect();
(parts[0], parts[1])
})
.collect();

let updates: Vec<Vec<u32>> = updates
.lines()
.map(|update| {
update
.split(',')
.filter_map(|num| num.parse::<u32>().ok())
.collect()
})
.collect();

Self { rules, updates }
}
}

#[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, None);
}

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

0 comments on commit c8e4556

Please sign in to comment.