Solved day 5
This commit is contained in:
1381
input/day05.txt
Normal file
1381
input/day05.txt
Normal file
File diff suppressed because it is too large
Load Diff
28
input/day05_example.txt
Normal file
28
input/day05_example.txt
Normal file
@@ -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
|
||||||
100
src/day5.rs
Normal file
100
src/day5.rs
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
use std::cmp::Ordering;
|
||||||
|
use crate::day_solver::DaySolver;
|
||||||
|
#[cfg(test)]
|
||||||
|
use crate::util::read_file;
|
||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
use itertools::Itertools;
|
||||||
|
use num::ToPrimitive;
|
||||||
|
|
||||||
|
pub struct Day5 {
|
||||||
|
rule_map: HashMap<u8, HashSet<u8>>,
|
||||||
|
updates: Vec<Vec<u8>>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Day5 {
|
||||||
|
|
||||||
|
pub fn create(input: String) -> Self {
|
||||||
|
let mut rule_map: HashMap<u8, HashSet<u8>> = HashMap::new();
|
||||||
|
let mut updates = Vec::new();
|
||||||
|
input.lines()
|
||||||
|
.filter(|l| !l.is_empty())
|
||||||
|
.for_each(|line| {
|
||||||
|
if let Some(rule) = line.split_once("|") {
|
||||||
|
let first = rule.0.parse::<u8>().unwrap();
|
||||||
|
let second = rule.1.parse::<u8>().unwrap();
|
||||||
|
if let Some(set) = rule_map.get_mut(&first) {
|
||||||
|
set.insert(second);
|
||||||
|
} else {
|
||||||
|
rule_map.insert(first, HashSet::from([second]));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
updates.push(line.split(",").map(|n| n.parse::<u8>().unwrap()).collect::<Vec<u8>>())
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// Put the input into the day struct
|
||||||
|
Day5 {
|
||||||
|
rule_map,
|
||||||
|
updates,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_update(&self, update: &Vec<u8>) -> bool {
|
||||||
|
let mut prev = HashSet::new();
|
||||||
|
for n in update {
|
||||||
|
if let Some(rule) = self.rule_map.get(n) {
|
||||||
|
if rule.iter().any(|n2| prev.contains(n2)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prev.insert(n);
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DaySolver for Day5 {
|
||||||
|
|
||||||
|
|
||||||
|
fn solve_part1(&mut self) -> String {
|
||||||
|
|
||||||
|
self.updates.iter()
|
||||||
|
.filter(|u| self.check_update(u))
|
||||||
|
.map(|u| u.get(u.len( ) / 2).unwrap().to_u32().unwrap())
|
||||||
|
.sum::<u32>()
|
||||||
|
.to_string()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_part2(&mut self) -> String {
|
||||||
|
self.updates.iter()
|
||||||
|
.filter(|u| !self.check_update(u))
|
||||||
|
.map(|u| u.iter().sorted_by(|a, b| {
|
||||||
|
if let Some(rule) = self.rule_map.get(a) {
|
||||||
|
if rule.iter().contains(b) {
|
||||||
|
return Ordering::Less
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(rule) = self.rule_map.get(b) {
|
||||||
|
if rule.iter().contains(a) {
|
||||||
|
return Ordering::Greater
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ordering::Equal
|
||||||
|
}).collect::<Vec<&u8>>())
|
||||||
|
.map(|u| u.get(u.len( ) / 2).unwrap().to_u32().unwrap())
|
||||||
|
.sum::<u32>()
|
||||||
|
.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part1() {
|
||||||
|
let mut day = Day5::create(read_file("input/day05_example.txt"));
|
||||||
|
assert_eq!("143", day.solve_part1());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part2() {
|
||||||
|
let mut day = Day5::create(read_file("input/day05_example.txt"));
|
||||||
|
assert_eq!("123", day.solve_part2());
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ use crate::day1::Day1;
|
|||||||
use crate::day2::Day2;
|
use crate::day2::Day2;
|
||||||
use crate::day3::Day3;
|
use crate::day3::Day3;
|
||||||
use crate::day4::Day4;
|
use crate::day4::Day4;
|
||||||
|
use crate::day5::Day5;
|
||||||
use crate::day_solver::DaySolver;
|
use crate::day_solver::DaySolver;
|
||||||
use crate::util::read_file;
|
use crate::util::read_file;
|
||||||
|
|
||||||
@@ -14,6 +15,7 @@ mod day1;
|
|||||||
mod day2;
|
mod day2;
|
||||||
mod day3;
|
mod day3;
|
||||||
mod day4;
|
mod day4;
|
||||||
|
mod day5;
|
||||||
|
|
||||||
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
||||||
|
|
||||||
@@ -89,7 +91,7 @@ fn build_day_solver(day: u8, input: String) -> Option<Box<dyn DaySolver>> {
|
|||||||
2 => Some(Box::new(Day2::create(input))),
|
2 => Some(Box::new(Day2::create(input))),
|
||||||
3 => Some(Box::new(Day3::create(input))),
|
3 => Some(Box::new(Day3::create(input))),
|
||||||
4 => Some(Box::new(Day4::create(input))),
|
4 => Some(Box::new(Day4::create(input))),
|
||||||
// 5 => Some(Box::new(Day5::create(input))),
|
5 => Some(Box::new(Day5::create(input))),
|
||||||
// 6 => Some(Box::new(Day6::create(input))),
|
// 6 => Some(Box::new(Day6::create(input))),
|
||||||
// 7 => Some(Box::new(Day7::create(input))),
|
// 7 => Some(Box::new(Day7::create(input))),
|
||||||
// 8 => Some(Box::new(Day8::create(input))),
|
// 8 => Some(Box::new(Day8::create(input))),
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ impl<T: Clone + Sized> Grid<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate(&self) {
|
pub fn validate(&self) {
|
||||||
debug_assert!(&self.data.len() % self.width == 0)
|
debug_assert!(self.data.len() % self.width == 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user