-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.rs
157 lines (129 loc) · 3.32 KB
/
day05.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
use crate::solutions::Solution;
use itertools::Itertools;
use std::cmp::Ordering;
pub struct Day05;
impl Solution for Day05 {
fn part_one(&self, input: &str) -> String {
let (page_ordering_rules, page_updates) = self.parse(input);
page_updates
.iter()
.filter(|&update| update.apply_all(&page_ordering_rules))
.map(|update| update.middle_page())
.sum::<usize>()
.to_string()
}
fn part_two(&self, input: &str) -> String {
let (page_ordering_rules, page_updates) = self.parse(input);
page_updates
.iter()
.filter(|&update| !update.apply_all(&page_ordering_rules))
.map(|update| update.sorted_by_rules(&page_ordering_rules).middle_page())
.sum::<usize>()
.to_string()
}
}
impl Day05 {
fn parse(&self, input: &str) -> (Vec<Rule>, Vec<Update>) {
let (page_ordering_rules, page_updates) = input.split_once("\n\n").unwrap();
let page_ordering_rules = page_ordering_rules.lines().map(Rule::from).collect_vec();
let page_updates = page_updates.lines().map(Update::from).collect_vec();
(page_ordering_rules, page_updates)
}
}
#[derive(Debug, Clone)]
struct Rule {
first: usize,
second: usize,
}
impl From<&str> for Rule {
fn from(value: &str) -> Self {
let (first, second) = value.split_once("|").unwrap();
Self {
first: first.parse().unwrap(),
second: second.parse().unwrap(),
}
}
}
#[derive(Debug)]
struct Update {
#[allow(dead_code)]
pages: Vec<usize>,
}
impl From<&str> for Update {
fn from(value: &str) -> Self {
let pages = value
.split_terminator(',')
.map(|x| x.parse::<usize>().unwrap())
.collect_vec();
Self { pages }
}
}
impl Update {
fn apply_all(&self, rules: &[Rule]) -> bool {
rules.iter().all(|rule| self.apply(rule))
}
fn apply(&self, rule: &Rule) -> bool {
let first_pos = self.pages.iter().position(|&x| x == rule.first);
let second_pos = self.pages.iter().position(|&x| x == rule.second);
match (first_pos, second_pos) {
(Some(first), Some(second)) => first < second,
_ => true,
}
}
fn middle_page(&self) -> usize {
let mid = self.pages.len() / 2;
*self.pages.get(mid).unwrap()
}
fn sorted_by_rules(&self, rules: &[Rule]) -> Self {
let mut pages = self.pages.clone();
pages.sort_by(|a, b| {
let has_rule = rules.iter().any(|r| r.first == *a && r.second == *b);
if has_rule {
Ordering::Less
} else {
Ordering::Greater
}
});
Self { pages }
}
}
#[cfg(test)]
mod tests {
use crate::solutions::year2024::day05::Day05;
use crate::solutions::Solution;
const EXAMPLE: &str = r#"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_one_example_test() {
assert_eq!("143", Day05.part_one(EXAMPLE));
}
#[test]
fn part_second_example_test() {
assert_eq!("123", Day05.part_two(EXAMPLE));
}
}