generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.rs
107 lines (89 loc) · 2.73 KB
/
05.rs
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::{
cmp::Ordering,
collections::HashMap,
};
advent_of_code::solution!(5);
fn valid_update(update: &Vec<u32>, rules: &HashMap<u32, Vec<u32>>) -> bool {
let mut nono_list = vec![];
for page in update {
if nono_list.contains(page) {
return false;
}
if let Some(v) = rules.get(page) {
let mut v = v.clone();
nono_list.append(&mut v);
}
}
true
}
pub fn part_one(input: &str) -> Option<u32> {
let (rules_input, updates) = input.split_once("\n\n").unwrap();
let mut rules: HashMap<u32, Vec<u32>> = HashMap::new();
let mut ans = 0;
for line in rules_input.lines() {
let before: u32 = line[0..2].parse().unwrap();
let after: u32 = line[3..5].parse().unwrap();
rules
.entry(after)
.and_modify(|v| v.push(before))
.or_insert(vec![before]);
}
for update_line in updates.lines() {
let update: Vec<u32> = (0..update_line.len())
.step_by(3)
.map(|i| update_line[i..i + 2].parse().unwrap())
.collect();
if valid_update(&update, &rules) {
ans += update[update.len() / 2];
}
}
Some(ans)
}
fn sort_update(x: &u32, y: &u32, rules: &HashMap<u32, Vec<u32>>) -> Ordering {
if rules.get(x).is_some_and(|v| v.contains(y)) {
Ordering::Greater
} else if rules.get(y).is_some_and(|v| v.contains(x)) {
Ordering::Less
} else {
Ordering::Equal
}
}
pub fn part_two(input: &str) -> Option<u32> {
let (rules_input, updates) = input.split_once("\n\n").unwrap();
let mut rules: HashMap<u32, Vec<u32>> = HashMap::new();
let mut ans = 0;
for line in rules_input.lines() {
let before: u32 = line[0..2].parse().unwrap();
let after: u32 = line[3..5].parse().unwrap();
rules
.entry(after)
.and_modify(|v| v.push(before))
.or_insert(vec![before]);
}
for update_line in updates.lines() {
let mut update: Vec<u32> = (0..update_line.len())
.step_by(3)
.map(|i| update_line[i..i + 2].parse().unwrap())
.collect();
if valid_update(&update, &rules) {
continue;
}
update.sort_by(|x, y| sort_update(x, y, &rules));
ans += update[update.len() / 2];
}
Some(ans)
}
#[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));
}
}