generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.rs
182 lines (149 loc) · 5.17 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
advent_of_code::solution!(5);
pub fn part_one(input: &str) -> Option<u32> {
let mut rules = Vec::new();
let mut good_updates = Vec::new();
let mut parsing_rules = true;
for line in input.lines() {
if line.is_empty() {
parsing_rules = false;
continue;
}
if parsing_rules {
let parts: Vec<&str> = line.split("|").collect();
assert!(parts.len() == 2, "Parts len should be two");
let rule: (u32, u32) = (
parts.first().unwrap().parse().unwrap(),
parts.last().unwrap().parse().unwrap(),
);
rules.push(rule);
} else {
let pages: Vec<u32> = line.split(",").map(|s| s.parse().unwrap()).collect();
let mut seen_pages: Vec<u32> = Vec::new();
let mut is_good = true;
'check_pages: for page in pages.iter() {
// println!("Checking page {}", page);
let matching_rules = rules.iter().filter(|r| r.0 == *page);
for rule in matching_rules {
// println!("Have we seen {}? {}", rule.1, seen_pages.contains(&rule.1));
if seen_pages.contains(&rule.1) {
is_good = false;
break 'check_pages;
}
}
assert!(
!seen_pages.contains(&page),
"Page should have never been seen!"
);
seen_pages.push(*page);
}
if is_good {
good_updates.push(pages);
}
}
}
let mut sum = 0;
for update in good_updates.iter() {
let middle_idx = update.len() / 2;
if let Some(middle_page) = update.get(middle_idx) {
sum += middle_page;
}
}
Some(sum)
}
pub fn part_two(input: &str) -> Option<u32> {
let mut rules = Vec::new();
let mut bad_updates = Vec::new();
let mut parsing_rules = true;
for line in input.lines() {
if line.is_empty() {
parsing_rules = false;
continue;
}
if parsing_rules {
let parts: Vec<&str> = line.split("|").collect();
assert!(parts.len() == 2, "Parts len should be two");
let rule: (u32, u32) = (
parts.first().unwrap().parse().unwrap(),
parts.last().unwrap().parse().unwrap(),
);
rules.push(rule);
} else {
let pages: Vec<u32> = line.split(",").map(|s| s.parse().unwrap()).collect();
let mut seen_pages: Vec<u32> = Vec::new();
let mut is_good = true;
'check_pages: for page in pages.iter() {
// println!("Checking page {}", page);
let matching_rules = rules.iter().filter(|r| r.0 == *page);
for rule in matching_rules {
// println!("Have we seen {}? {}", rule.1, seen_pages.contains(&rule.1));
if seen_pages.contains(&rule.1) {
is_good = false;
break 'check_pages;
}
}
assert!(
!seen_pages.contains(&page),
"Page should have never been seen!"
);
seen_pages.push(*page);
}
if !is_good {
println!("{:?}", pages);
bad_updates.push(pages);
}
}
}
println!("Fixing");
let mut fixed_updates = Vec::new();
for bad_update in bad_updates.iter() {
let mut fixed_pages: Vec<u32> = Vec::new();
loop {
let unfixed_pages: Vec<u32> = bad_update
.iter()
.filter(|page| !fixed_pages.contains(page))
.map(|page| *page)
.collect();
if let Some(page) = unfixed_pages.first() {
let pred = get_predecessor(*page, &unfixed_pages, &rules);
if !fixed_pages.contains(&pred) {
fixed_pages.push(pred);
}
} else {
break;
}
}
println!("{:?}", fixed_pages);
fixed_updates.push(fixed_pages);
}
let mut sum = 0;
for update in fixed_updates.iter() {
let middle_idx = update.len() / 2;
if let Some(middle_page) = update.get(middle_idx) {
sum += middle_page;
}
}
Some(sum)
}
fn get_predecessor(page: u32, pages: &Vec<u32>, rules: &Vec<(u32, u32)>) -> u32 {
let matching_rules: Vec<_> = rules.iter().filter(|r| r.1 == page).collect();
for rule in matching_rules.iter() {
if pages.contains(&rule.0) {
return get_predecessor(rule.0, pages, rules);
}
}
page
}
#[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));
}
}