Finished day 13 aoc, rust is amazing

This commit is contained in:
2023-01-10 14:48:08 +01:00
parent 8f86b8b046
commit 8da7d3d1a8

View File

@@ -98,9 +98,11 @@ fn check_lists(lists: &(List, List)) -> Option<bool> {
}
}
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);
}