Add day 13
This commit is contained in:
parent
c38853b577
commit
3d507bbda4
@ -1,17 +1,37 @@
|
|||||||
use std::fmt::Display;
|
use std::fmt::{Display, Formatter};
|
||||||
use base::read_file;
|
use base::read_file;
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||||
struct List {
|
struct List {
|
||||||
elements: Vec<ListElement>,
|
elements: Vec<ListElement>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||||
enum ListElement {
|
enum ListElement {
|
||||||
Number(u32),
|
Number(u32),
|
||||||
Sublist(List),
|
Sublist(List),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for List {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.write_str("[")?;
|
||||||
|
for (idx, element) in self.elements.iter().enumerate() {
|
||||||
|
match element {
|
||||||
|
ListElement::Number(n) => {
|
||||||
|
write!(f, "{}", n)?;
|
||||||
|
}
|
||||||
|
ListElement::Sublist(sub) => {
|
||||||
|
write!(f, "{}", sub)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if idx != self.elements.len() - 1 {
|
||||||
|
f.write_str(",")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.write_str("]")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn add_to_list(next: &Vec<char>, start: usize) -> (List, usize) {
|
fn add_to_list(next: &Vec<char>, start: usize) -> (List, usize) {
|
||||||
let mut list = List { elements: vec![] };
|
let mut list = List { elements: vec![] };
|
||||||
let mut index = start;
|
let mut index = start;
|
||||||
@ -26,6 +46,7 @@ fn add_to_list(next: &Vec<char>, start: usize) -> (List, usize) {
|
|||||||
let (sublist, new_idx) = add_to_list(next, index + 1);
|
let (sublist, new_idx) = add_to_list(next, index + 1);
|
||||||
index = new_idx;
|
index = new_idx;
|
||||||
list.elements.push(ListElement::Sublist(sublist));
|
list.elements.push(ListElement::Sublist(sublist));
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
']' => {
|
']' => {
|
||||||
return (list, index + 1);
|
return (list, index + 1);
|
||||||
@ -103,18 +124,56 @@ fn are_in_right_order(lhs: &List, rhs: &List) -> Option<bool> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_list(str: &str) -> List {
|
||||||
|
return add_to_list(&mut str.chars().collect::<Vec<_>>(), 0).0;
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let lines = read_file("day13.txt");
|
let lines = read_file("day13.txt");
|
||||||
let mut lists = vec![];
|
let mut lists = vec![];
|
||||||
for line in lines {
|
for line in lines {
|
||||||
if !line.is_empty() {
|
if !line.is_empty() {
|
||||||
lists.push(add_to_list(&mut line.chars().collect::<Vec<_>>(), 0).0);
|
lists.push(create_list(&line));
|
||||||
|
let line = line.trim_end();
|
||||||
|
let last_line = lists.last().unwrap();
|
||||||
|
let test_result = last_line.to_string() == line;
|
||||||
|
if test_result {
|
||||||
|
println!("Compare: {} with {}", line, last_line);
|
||||||
|
} else {
|
||||||
|
panic!("Parser error!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let task1 = lists.chunks_exact(2).enumerate().filter(|(_, chunk)| are_in_right_order(&chunk[0], &chunk[1]).unwrap()).fold(0, |acc, (idx, _)| {
|
let task1 = lists.chunks_exact(2).enumerate().filter(|(_, chunk)| are_in_right_order(&chunk[0], &chunk[1]).unwrap()).fold(0, |acc, (idx, _)| {
|
||||||
println!("Found machting pair at: {idx}");
|
println!("Found matching pair at: {idx}");
|
||||||
acc + idx + 1
|
acc + idx + 1
|
||||||
});
|
});
|
||||||
|
|
||||||
println!("Task1: {task1}");
|
println!("Task1: {task1}");
|
||||||
|
|
||||||
|
let list_2 = create_list("[[2]]");
|
||||||
|
let list_6 = create_list("[[6]]");
|
||||||
|
|
||||||
|
lists.push(list_2.clone());
|
||||||
|
lists.push(list_6.clone());
|
||||||
|
|
||||||
|
for idx in 0..lists.len() {
|
||||||
|
for list in (idx + 1)..lists.len() {
|
||||||
|
if !are_in_right_order(&lists[idx], &lists[list]).unwrap() {
|
||||||
|
lists.swap(idx, list);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut index_2 = 0;
|
||||||
|
let mut index_6 = 0;
|
||||||
|
for (idx, list) in lists.iter().enumerate() {
|
||||||
|
if *list == list_2 {
|
||||||
|
index_2 = idx + 1;
|
||||||
|
} else if *list == list_6 {
|
||||||
|
index_6 = idx + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Found index {} and index {}. Result is {}", index_2, index_6, index_2 * index_6);
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user