-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_05.rs
125 lines (107 loc) Β· 3.22 KB
/
day_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::collections::{HashMap, HashSet};
use common::{solution, Answer};
solution!("Print Queue", 5);
fn part_a(input: &str) -> Answer {
let problem = PrintQueue::parse(input);
// Sum the middle value of all valid page lists.
(0..problem.updates.len())
.filter(|&x| problem.is_valid(x))
.map(|x| &problem.updates[x])
.map(|x| x[x.len() / 2])
.sum::<u32>()
.into()
}
fn part_b(input: &str) -> Answer {
let problem = PrintQueue::parse(input);
// Sum the middle value of the sorted versions of all page lists that are
// not sorted correctly.
(0..problem.updates.len())
.filter(|&x| !problem.is_valid(x))
.map(|x| problem.sort_pages(x))
.map(|x| x[x.len() / 2])
.sum::<u32>()
.into()
}
struct PrintQueue {
/// Maps a page to the pages that must come before it.
rule_map: HashMap<u32, HashSet<u32>>,
updates: Vec<Vec<u32>>,
}
impl PrintQueue {
fn parse(input: &str) -> Self {
let (rules, updates) = input.split_once("\n\n").unwrap();
// a|b => a comes before b
// For each rule stating that some page a comes before some page b, add
// a to the list of pages that come before b.
let mut rule_map: HashMap<u32, HashSet<u32>> = HashMap::new();
for (a, b) in rules.lines().map(|x| {
let (a, b) = x.split_once('|').unwrap();
(a.parse::<u32>().unwrap(), b.parse::<u32>().unwrap())
}) {
rule_map.entry(b).or_default().insert(a);
}
let updates = updates
.lines()
.map(|x| x.split(',').map(|x| x.parse::<u32>().unwrap()).collect())
.collect();
Self { rule_map, updates }
}
fn is_valid(&self, idx: usize) -> bool {
let line = &self.updates[idx];
// A line is sorted if to the left of every page is a page that should
// be before it. If b is not in the rule map, that means that there are
// no pages that come before it and since b is at least the 2nd item in
// the line, the line is therefore invalid.
line.is_sorted_by(|a, b| self.rule_map.contains_key(b) && self.rule_map[b].contains(a))
}
fn sort_pages(&self, idx: usize) -> Vec<u32> {
let mut line = self.updates[idx].clone();
// Just the same expression from before plugged into a sorting algo to
// put our pages in order!
line.sort_unstable_by(|a, b| {
(self.rule_map.contains_key(b) && self.rule_map[b].contains(a)).cmp(&true)
});
line
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
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
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 143.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 123.into());
}
}