|
@@ -1,5 +1,5 @@
|
|
|
|
|
|
-#[derive(Clone, Debug)]
|
|
|
+#[derive(Clone, Debug, PartialEq)]
|
|
|
pub enum PacketVal {
|
|
|
scalar(u32),
|
|
|
list(Vec<PacketVal>)
|
|
@@ -31,7 +31,6 @@ impl PacketVal {
|
|
|
if left.len() < other.len() {
|
|
|
return Cmp::good
|
|
|
} else if left.len() == other.len() {
|
|
|
- // println!("moo");
|
|
|
return Cmp::idk
|
|
|
} else {
|
|
|
return Cmp::bad
|
|
@@ -60,6 +59,22 @@ impl PacketVal {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+pub fn sorted(packets: &mut Vec<PacketVal>) {
|
|
|
+ let mut idx = 0;
|
|
|
+ let mut curr_min_idx = 0;
|
|
|
+
|
|
|
+ while idx < packets.len() {
|
|
|
+ curr_min_idx = idx;
|
|
|
+ for search_idx in (idx + 1)..packets.len() {
|
|
|
+ if let Cmp::good = packets[search_idx].lt(&packets[curr_min_idx]) {
|
|
|
+ curr_min_idx = search_idx;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ packets.swap(idx, curr_min_idx);
|
|
|
+ idx += 1;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
pub fn parse_line(line: &str) -> PacketVal {
|
|
|
let mut pack_stack: Vec<PacketVal> = Vec::new();
|
|
@@ -121,8 +136,49 @@ pub fn part_one(input: &str) -> Option<u32> {
|
|
|
Some(count)
|
|
|
}
|
|
|
|
|
|
-pub fn part_two(input: &str) -> Option<u32> {
|
|
|
- None
|
|
|
+pub fn part_two(input: &str) -> Option<usize> {
|
|
|
+ let mut lines = input.lines();
|
|
|
+ let mut packets: Vec<PacketVal> = Vec::new();
|
|
|
+
|
|
|
+ loop {
|
|
|
+ let left_line = lines.next().unwrap();
|
|
|
+ let left = parse_line(left_line);
|
|
|
+
|
|
|
+ let right_line = lines.next().unwrap();
|
|
|
+ let right = parse_line(right_line);
|
|
|
+
|
|
|
+
|
|
|
+ packets.push(left);
|
|
|
+ packets.push(right);
|
|
|
+
|
|
|
+
|
|
|
+ match lines.next() {
|
|
|
+ Some(_) => continue,
|
|
|
+ _ => break
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let marker_one = parse_line("[[2]]");
|
|
|
+ let marker_two = parse_line("[[6]]");
|
|
|
+
|
|
|
+ packets.push(marker_one.clone());
|
|
|
+ packets.push(marker_two.clone());
|
|
|
+
|
|
|
+ sorted(&mut packets);
|
|
|
+
|
|
|
+ let mut first_marker_idx = 0;
|
|
|
+ let mut second_marker_idx = 0;
|
|
|
+
|
|
|
+ for (idx, packet) in packets.iter().enumerate() {
|
|
|
+ // println!("{:?}", packet);
|
|
|
+ if *packet == marker_one {
|
|
|
+ first_marker_idx = idx + 1;
|
|
|
+ } else if *packet == marker_two {
|
|
|
+ second_marker_idx = idx + 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Some(first_marker_idx * second_marker_idx)
|
|
|
}
|
|
|
|
|
|
fn main() {
|
|
@@ -144,6 +200,6 @@ mod tests {
|
|
|
#[test]
|
|
|
fn test_part_two() {
|
|
|
let input = advent_of_code::read_file("examples", 13);
|
|
|
- assert_eq!(part_two(&input), None);
|
|
|
+ assert_eq!(part_two(&input), Some(140));
|
|
|
}
|
|
|
}
|