generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6872535
commit 1092344
Showing
4 changed files
with
151 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use std::{ | ||
cmp::Ordering, | ||
collections::{HashMap, HashSet, VecDeque}, | ||
}; | ||
|
||
use itertools::Itertools; | ||
|
||
advent_of_code::solution!(5); | ||
|
||
pub fn part_one(input: &str) -> Option<u32> { | ||
let (rules, updates) = input.split_once("\n\n").unwrap(); | ||
|
||
let rule_lookup = | ||
rules | ||
.lines() | ||
.fold(HashMap::new(), |mut acc: HashMap<&str, Vec<&str>>, line| { | ||
let (l, r) = line.split_once("|").unwrap(); | ||
acc.entry(l).or_default().push(r); | ||
acc | ||
}); | ||
|
||
let mut result = 0; | ||
for update in updates.lines() { | ||
let mut already_seen: HashSet<&str> = HashSet::new(); | ||
let mut success = true; | ||
for value in update.split(',') { | ||
let Some(must_come_after) = rule_lookup.get(value) else { | ||
already_seen.insert(value); | ||
continue; | ||
}; | ||
let breaks_rule = must_come_after.iter().any(|val| already_seen.contains(val)); | ||
if breaks_rule { | ||
success = false; | ||
break; | ||
} | ||
|
||
already_seen.insert(value); | ||
} | ||
|
||
if success { | ||
let as_strs = update.split(",").collect_vec(); | ||
let middle = as_strs[(as_strs.len() - 1) / 2].parse::<u32>().unwrap(); | ||
result += middle; | ||
} | ||
} | ||
|
||
Some(result) | ||
} | ||
|
||
pub fn part_two(input: &str) -> Option<u32> { | ||
let (rules, updates) = input.split_once("\n\n").unwrap(); | ||
|
||
let rule_lookup = | ||
rules | ||
.lines() | ||
.fold(HashMap::new(), |mut acc: HashMap<&str, Vec<&str>>, line| { | ||
let (l, r) = line.split_once("|").unwrap(); | ||
acc.entry(l).or_default().push(r); | ||
acc | ||
}); | ||
|
||
let mut incorrect = vec![]; | ||
for update in updates.lines() { | ||
let mut already_seen: HashSet<&str> = HashSet::new(); | ||
for value in update.split(',') { | ||
let Some(must_come_after) = rule_lookup.get(value) else { | ||
already_seen.insert(value); | ||
continue; | ||
}; | ||
let breaks_rule = must_come_after.iter().any(|val| already_seen.contains(val)); | ||
if breaks_rule { | ||
incorrect.push(update); | ||
break; | ||
} | ||
|
||
already_seen.insert(value); | ||
} | ||
} | ||
|
||
let mut res = 0; | ||
for update in incorrect.into_iter() { | ||
let ordered = update | ||
.split(",") | ||
.sorted_by(|a, b| { | ||
if !rule_lookup.contains_key(a) { | ||
return Ordering::Greater; | ||
} | ||
|
||
if rule_lookup[a].contains(b) { | ||
return Ordering::Less; | ||
} | ||
|
||
Ordering::Greater | ||
}) | ||
.collect_vec(); | ||
|
||
let middle = ordered[(ordered.len() - 1) / 2].parse::<u32>().unwrap(); | ||
res += middle; | ||
} | ||
|
||
Some(res) | ||
} | ||
|
||
#[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)); | ||
} | ||
} |