From 8da7d3d1a874949aadb7468f6f05145649f47891 Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Tue, 10 Jan 2023 14:48:08 +0100 Subject: [PATCH] Finished day 13 aoc, rust is amazing --- advent_of_code/2022/13/src/main.rs | 50 ++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/advent_of_code/2022/13/src/main.rs b/advent_of_code/2022/13/src/main.rs index 3d4455c..e08387b 100644 --- a/advent_of_code/2022/13/src/main.rs +++ b/advent_of_code/2022/13/src/main.rs @@ -98,9 +98,11 @@ fn check_lists(lists: &(List, List)) -> Option { } } + + fn main() { let input_text = include_str!("../input.txt"); - let (_rest, lists) = parse_input(input_text).unwrap(); + let (_rest, list_pairs) = parse_input(input_text).unwrap(); // Checking the parsing (which worked first try btw like wtf) // for list_pair in &lists { @@ -109,7 +111,7 @@ fn main() { let mut index = 0; let mut result = 0; - for list_pair in &lists { + for list_pair in &list_pairs { index += 1; match check_lists(list_pair) { @@ -120,4 +122,48 @@ fn main() { } println!("Sum of indices is: {result}"); + + let first_divider_packet = vec![NestedList(vec![Num(2)])]; + let second_divider_packet = vec![NestedList(vec![Num(6)])]; + + let mut list = Vec::new(); + for list_pair in list_pairs { + list.push(list_pair.0); + list.push(list_pair.1); + } + + list.push(first_divider_packet.clone()); + list.push(second_divider_packet.clone()); + + list.sort_by(|l, r| match check_lists(&(l.to_vec(), r.to_vec())) { + None => std::cmp::Ordering::Equal, + Some(true) => std::cmp::Ordering::Less, + Some(false) => std::cmp::Ordering::Greater, + }); + + // Checking the sorting (which works :D) + // for l in &list { + // print_list(l); + // println!(); + // } + + let mut first_index = 0; + let mut second_index = 0; + index = 1; + + for l in & list { + if check_lists(&(l.to_vec(), first_divider_packet.clone())) == None { + first_index = index; + } else if check_lists(&(l.to_vec(), second_divider_packet.clone())) == None { + second_index = index; + } + + if first_index != 0 && second_index != 0 { + break; + } + + index += 1; + } + + println!("Divider packets' indices multiplied: {}", first_index*second_index); }