Day 13, Part 2!
This commit is contained in:
parent
6425b969a1
commit
b423f76ddc
1 changed files with 54 additions and 16 deletions
|
@ -1,6 +1,8 @@
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::{cmp, env, fmt, fs};
|
use std::{cmp, env, fmt, fs};
|
||||||
|
|
||||||
|
const VERBOSE: bool = false;
|
||||||
|
|
||||||
type Pair = (Packet, Packet);
|
type Pair = (Packet, Packet);
|
||||||
type Int = u32;
|
type Int = u32;
|
||||||
|
|
||||||
|
@ -28,7 +30,9 @@ impl Datum {
|
||||||
|
|
||||||
impl cmp::PartialOrd for Datum {
|
impl cmp::PartialOrd for Datum {
|
||||||
fn partial_cmp(&self, other: &Datum) -> Option<cmp::Ordering> {
|
fn partial_cmp(&self, other: &Datum) -> Option<cmp::Ordering> {
|
||||||
|
if VERBOSE {
|
||||||
println!(" Compare {} vs {}", self, other);
|
println!(" Compare {} vs {}", self, other);
|
||||||
|
}
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(Datum::Int(self_value), Datum::Int(other_value)) => {
|
(Datum::Int(self_value), Datum::Int(other_value)) => {
|
||||||
self_value.partial_cmp(other_value)
|
self_value.partial_cmp(other_value)
|
||||||
|
@ -70,7 +74,7 @@ impl fmt::Display for Datum {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Clone, Eq, PartialEq)]
|
||||||
struct Packet(Datum);
|
struct Packet(Datum);
|
||||||
|
|
||||||
impl TryFrom<&str> for Packet {
|
impl TryFrom<&str> for Packet {
|
||||||
|
@ -152,23 +156,45 @@ impl TryFrom<&str> for Packet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl cmp::Ord for Packet {
|
||||||
|
fn cmp(&self, other: &Packet) -> cmp::Ordering {
|
||||||
|
self.partial_cmp(other).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl cmp::PartialOrd for Packet {
|
impl cmp::PartialOrd for Packet {
|
||||||
fn partial_cmp(&self, other: &Packet) -> Option<cmp::Ordering> {
|
fn partial_cmp(&self, other: &Packet) -> Option<cmp::Ordering> {
|
||||||
|
if VERBOSE {
|
||||||
println!("Compare {} vs {}", self.0, other.0);
|
println!("Compare {} vs {}", self.0, other.0);
|
||||||
|
}
|
||||||
|
|
||||||
let ordering = self.0.partial_cmp(&other.0);
|
let ordering = self.0.partial_cmp(&other.0);
|
||||||
|
if VERBOSE {
|
||||||
println!(" -> {:?}", ordering);
|
println!(" -> {:?}", ordering);
|
||||||
|
}
|
||||||
|
|
||||||
ordering
|
ordering
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for Packet {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Packet {
|
impl fmt::Display for Packet {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{}", self.0)
|
write!(f, "{}", self.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn last_two_packets(packets: &Vec<Packet>) -> (Packet, Packet) {
|
||||||
|
let (_, last_two) = packets.split_at(packets.len() - 2);
|
||||||
|
|
||||||
|
(last_two[0].clone(), last_two[1].clone())
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
|
@ -182,25 +208,19 @@ fn main() {
|
||||||
|
|
||||||
for line in lines {
|
for line in lines {
|
||||||
if line == "" {
|
if line == "" {
|
||||||
let right = completed_packets.pop().expect("Missing right packet");
|
pairs.push(last_two_packets(&completed_packets));
|
||||||
let left = completed_packets.pop().expect("Missing left packet");
|
|
||||||
println!("Completed pair!");
|
|
||||||
|
|
||||||
assert!(completed_packets.is_empty());
|
|
||||||
|
|
||||||
pairs.push((left, right));
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let packet = Packet::try_from(line).expect("Unable to parse packet!");
|
let packet = Packet::try_from(line).expect("Unable to parse packet!");
|
||||||
|
if VERBOSE {
|
||||||
println!("Completed packet! {}", &packet);
|
println!("Completed packet! {}", &packet);
|
||||||
|
}
|
||||||
|
|
||||||
completed_packets.push(packet);
|
completed_packets.push(packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
let right = completed_packets.pop().expect("Missing right packet");
|
pairs.push(last_two_packets(&completed_packets));
|
||||||
let left = completed_packets.pop().expect("Missing left packet");
|
|
||||||
println!("Completed pair!");
|
|
||||||
pairs.push((left, right));
|
|
||||||
|
|
||||||
let pairs_in_right_order = pairs
|
let pairs_in_right_order = pairs
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -224,6 +244,24 @@ fn main() {
|
||||||
"Sum of indicies of pairs in correct order: {}",
|
"Sum of indicies of pairs in correct order: {}",
|
||||||
&pairs_in_right_order.iter().map(|(i, _)| i).sum::<usize>()
|
&pairs_in_right_order.iter().map(|(i, _)| i).sum::<usize>()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
println!("----- Divider Packets -----");
|
||||||
|
let divider_a = Packet(Datum::List(vec![Datum::List(vec![Datum::Int(2)])]));
|
||||||
|
let divider_b = Packet(Datum::List(vec![Datum::List(vec![Datum::Int(6)])]));
|
||||||
|
|
||||||
|
completed_packets.push(divider_a.clone());
|
||||||
|
completed_packets.push(divider_b.clone());
|
||||||
|
completed_packets.sort();
|
||||||
|
|
||||||
|
dbg!(&completed_packets);
|
||||||
|
|
||||||
|
let product_of_indexes_of_divider_packets: usize = completed_packets
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|(_, p)| **p == divider_a || **p == divider_b)
|
||||||
|
.map(|(i, _)| i + 1)
|
||||||
|
.product();
|
||||||
|
println!("Product of indexes of divider packets: {product_of_indexes_of_divider_packets}");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue