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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
#![feature(iter_array_chunks)]
/// --- Part Two ---
///
/// Now, you just need to put all of the packets in the right order. Disregard the blank lines in
/// your list of received packets.
///
/// The distress signal protocol also requires that you include two additional divider packets:
///
/// [[2]]
/// [[6]]
///
/// Using the same rules as before, organize all packets - the ones in your list of received
/// packets as well as the two divider packets - into the correct order.
///
/// For the example above, the result of putting the packets in the correct order is:
///
/// ```
/// []
/// [[]]
/// [[[]]]
/// [1,1,3,1,1]
/// [1,1,5,1,1]
/// [[1],[2,3,4]]
/// [1,[2,[3,[4,[5,6,0]]]],8,9]
/// [1,[2,[3,[4,[5,6,7]]]],8,9]
/// [[1],4]
/// [[2]]
/// [3]
/// [[4,4],4,4]
/// [[4,4],4,4,4]
/// [[6]]
/// [7,7,7]
/// [7,7,7,7]
/// [[8,7,6]]
/// [9]
/// ```
///
/// Afterward, locate the divider packets. To find the decoder key for this distress signal, you
/// need to determine the indices of the two divider packets and multiply them together. (The
/// first packet is at index 1, the second packet is at index 2, and so on.) In this example, the
/// divider packets are 10th and 14th, and so the decoder key is 140.
///
/// Organize all of the packets into the correct order. What is the decoder key for the distress
/// signal?
use clap::Parser;
use itertools::Itertools;
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::i8;
use nom::combinator::{map, opt};
use nom::error::{ContextError, ErrorKind as NomErrorKind, ParseError};
use nom::multi::many1;
use nom::sequence::{delimited, terminated};
use nom::IResult;
use std::cmp::Ordering;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::PathBuf;
const FILEPATH: &'static str = "examples/input.txt";
pub type Input<'a> = &'a str;
pub type Result<'a, T> = IResult<Input<'a>, T, Error<Input<'a>>>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ErrorKind {
Nom(NomErrorKind),
Context(&'static str),
Custom(String),
}
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Cli {
#[clap(short, long, default_value = FILEPATH)]
file: PathBuf,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error<I> {
pub errors: Vec<(I, ErrorKind)>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
enum ListEntry {
Value(Option<i8>),
List(Vec<ListEntry>),
}
impl Ord for ListEntry {
fn cmp(&self, other: &Self) -> Ordering {
let res = compare(&self, other);
match res {
Some(x) => match x {
true => Ordering::Less,
false => Ordering::Greater,
},
None => Ordering::Equal,
}
}
}
impl PartialOrd for ListEntry {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<I> ParseError<I> for Error<I> {
fn from_error_kind(input: I, kind: NomErrorKind) -> Self {
let errors = vec![(input, ErrorKind::Nom(kind))];
Self { errors }
}
fn append(input: I, kind: NomErrorKind, mut other: Self) -> Self {
other.errors.push((input, ErrorKind::Nom(kind)));
other
}
}
impl<I> ContextError<I> for Error<I> {
fn add_context(input: I, ctx: &'static str, mut other: Self) -> Self {
other.errors.push((input, ErrorKind::Context(ctx)));
other
}
}
fn parse_list(input: &str) -> Result<ListEntry> {
alt((
map(i8, |v| ListEntry::Value(Some(v))),
map(tag("[]"), |_| ListEntry::Value(None)),
delimited(
tag("["),
map(many1(terminated(parse_list, opt(tag(",")))), |list| {
ListEntry::List(list)
}),
tag("]"),
),
))(input)
}
fn compare(lhs: &ListEntry, rhs: &ListEntry) -> Option<bool> {
use ListEntry::*;
match (lhs, rhs) {
(Value(lv), Value(rv)) => {
if lv == rv {
None
} else {
Some(lv < rv)
}
}
(Value(None), List(_)) => Some(true),
(Value(_), List(_)) => compare(&ListEntry::List(vec![lhs.clone()]), rhs),
(List(_), Value(None)) => Some(false),
(List(_), Value(_)) => compare(lhs, &ListEntry::List(vec![rhs.clone()])),
(List(lv), List(rv)) => lv
.iter()
.zip_longest(rv.iter())
.find_map(|pair| match pair {
itertools::EitherOrBoth::Both(l, r) => compare(l, r),
itertools::EitherOrBoth::Right(_) => Some(true),
itertools::EitherOrBoth::Left(_) => Some(false),
}),
}
}
fn main() {
let divider_packets: [ListEntry; 2] = [
ListEntry::List(vec![ListEntry::Value(Some(2))]),
ListEntry::List(vec![ListEntry::Value(Some(6))]),
];
let args = Cli::parse();
let file = File::open(&args.file).unwrap();
let reader = BufReader::new(file);
let res: usize = reader
.lines()
.filter_map(|line| {
let s = line.unwrap();
if s.is_empty() {
return None;
}
Some(parse_list(s.as_str()).unwrap().1)
})
.chain(divider_packets.clone().into_iter())
.sorted()
.zip(1..)
.filter(|(x, _)| x == ÷r_packets[0] || x == ÷r_packets[1])
.map(|(_, idx)| idx)
.take(2)
.product();
println!("{res}");
}
|