-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
154 lines (135 loc) · 4.25 KB
/
main.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
use crate::packet::Packet;
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &'static str) -> usize {
data.split("\n\n")
.enumerate()
.map(|(i, part)| (i, part.split_once('\n').unwrap()))
.map(|(i, (p1, p2))| (i, (Packet::new(p1), Packet::new(p2))))
.filter(|(_, (p1, p2))| p1 < p2)
.map(|(i, _)| i + 1)
.sum()
}
fn part_two(data: &'static str) -> usize {
let indexes = data
.split('\n')
.filter(|l| !l.is_empty())
.map(Packet::new)
.filter(|p| p < &Packet::new("[[6]]"))
.fold((1, 2), |acc: (usize, usize), p| {
if p < Packet::new("[[2]]") {
(acc.0 + 1, acc.1 + 1)
} else {
(acc.0, acc.1 + 1)
}
});
indexes.0 * indexes.1
}
mod packet {
use std::cmp::Ordering;
#[derive(PartialEq, Eq, Debug)]
pub struct Packet {
data: &'static str,
}
impl Packet {
pub fn new(data: &'static str) -> Self {
Packet { data }
}
}
impl Ord for Packet {
fn cmp(&self, other: &Self) -> Ordering {
for (elem_1, elem_2) in self.iter().zip(other.iter()) {
match (elem_1.starts_with('['), elem_2.starts_with('[')) {
(false, false) => match compare_elements(elem_1, elem_2) {
Ordering::Equal => (),
Ordering::Greater => return Ordering::Greater,
Ordering::Less => return Ordering::Less,
},
(_, _) => match Packet::new(elem_1).cmp(&Packet::new(elem_2)) {
Ordering::Equal => (),
Ordering::Greater => return Ordering::Greater,
Ordering::Less => return Ordering::Less,
},
}
}
let len_1 = self.data.len() + 2 * usize::from(!self.data.starts_with('['));
let len_2 = other.data.len() + 2 * usize::from(!other.data.starts_with('['));
len_1.cmp(&len_2)
}
}
fn compare_elements(a: &'static str, b: &'static str) -> Ordering {
a.parse::<u32>().unwrap().cmp(&b.parse::<u32>().unwrap())
}
impl PartialOrd for Packet {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
pub struct Iter {
data: &'static str,
position: usize,
end: bool,
}
impl Iterator for Iter {
type Item = &'static str;
fn next(&mut self) -> Option<Self::Item> {
if self.end {
return None;
}
let mut count = 0_u32;
let start = self.position;
for (i, char) in self.data[self.position..].char_indices() {
match (char, count, i) {
('[', _, _) => count += 1,
(']', 0, 0) => return None,
(']', 0, _) => {
self.end = true;
return Some(&self.data[start..start + i]);
}
(']', _, _) => count -= 1,
(',', 0, _) => {
self.position += i + 1;
return Some(&self.data[start..start + i]);
}
(_, _, _) => (),
}
}
self.end = true;
Some(&self.data[start..])
}
}
impl Packet {
fn iter(&self) -> Iter {
let pos = usize::from(self.data.starts_with('['));
Iter {
data: self.data,
position: pos,
end: false,
}
}
}
impl IntoIterator for &Packet {
type Item = &'static str;
type IntoIter = Iter;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(13, part_one(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(140, part_two(data));
}
}