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
|
mod utils;
use std::{
cmp::Ordering,
iter::{once, Peekable},
};
extern crate nom;
use nom::{bytes::complete::tag, character::complete::u32};
use utils::read_input;
fn main() {
let input = read_input();
let mut full: Vec<_> = input.lines().filter(|x| !x.is_empty()).collect();
println!(
"Part 1: {}",
full.iter()
.step_by(2)
.zip(full.iter().skip(1).step_by(2))
.map(|(left, right)| { compare(left, right) })
.enumerate()
.filter_map(|(i, cmp)| match cmp {
Ordering::Greater => None,
_ => Some(i + 1),
})
.sum::<usize>()
);
full.sort_by(|l, r| compare(l, r));
println!(
"Part 2: {}",
full.iter()
.enumerate()
.filter_map(|(i, val)| match *val {
"[[2]]" | "[[6]]" => Some(i + 1),
_ => None,
})
.product::<usize>()
);
}
fn compare(left: &str, right: &str) -> Ordering {
let mut left = Packet(left).peekable();
let mut right = Packet(right).peekable();
assert_eq!(left.next(), Some(PacketContents::StartList));
assert_eq!(right.next(), Some(PacketContents::StartList));
compare_lists(&mut left, &mut right)
}
fn compare_lists(
left: &mut Peekable<impl Iterator<Item = PacketContents>>,
right: &mut Peekable<impl Iterator<Item = PacketContents>>,
) -> Ordering {
loop {
match (left.next().unwrap(), right.next().unwrap()) {
(PacketContents::EndList, PacketContents::EndList) => {
return Ordering::Equal;
}
(PacketContents::EndList, _) => {
while !matches!(right.next(), Some(PacketContents::EndList)) {}
return Ordering::Less;
}
(_, PacketContents::EndList) => {
while !matches!(left.next(), Some(PacketContents::EndList)) {}
return Ordering::Greater;
}
(PacketContents::StartList, PacketContents::StartList) => {
match compare_lists(left, right) {
Ordering::Equal => (),
x => return x,
}
}
(l @ PacketContents::Integer(_), PacketContents::StartList) => {
match compare_lists(
&mut once(l).chain(once(PacketContents::EndList)).peekable(),
right,
) {
Ordering::Equal => (),
x => return x,
}
}
(PacketContents::StartList, r @ PacketContents::Integer(_)) => {
match compare_lists(
left,
&mut once(r).chain(once(PacketContents::EndList)).peekable(),
) {
Ordering::Equal => (),
x => return x,
}
}
(PacketContents::Integer(l), PacketContents::Integer(r)) => match l.cmp(&r) {
Ordering::Equal => (),
x => return x,
},
};
}
}
#[derive(Debug, PartialEq, Eq)]
enum PacketContents {
Integer(usize),
StartList,
EndList,
}
struct Packet<'a>(&'a str);
impl Iterator for Packet<'_> {
type Item = PacketContents;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Ok((i, _)) = tag::<_, _, (&str, nom::error::ErrorKind)>("[")(self.0) {
self.0 = i;
return Some(PacketContents::StartList);
} else if let Ok((i, _)) = tag::<_, _, (&str, nom::error::ErrorKind)>("]")(self.0) {
self.0 = i;
return Some(PacketContents::EndList);
} else if let Ok((i, x)) = u32::<_, (&str, nom::error::ErrorKind)>(self.0) {
self.0 = i;
return Some(PacketContents::Integer(x as usize));
} else if let Ok((i, _)) = tag::<_, _, (&str, nom::error::ErrorKind)>(",")(self.0) {
self.0 = i;
} else {
return None;
}
}
}
}
|