From 1762cb4036c965dbe447b9227957e0ee1a39f9ca Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 9 Dec 2022 10:51:05 +0100 Subject: [PATCH] Solve Part 1 --- Cargo.toml | 1 + src/input.txt | 512 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 107 ++++++++++- 3 files changed, 618 insertions(+), 2 deletions(-) create mode 100644 src/input.txt diff --git a/Cargo.toml b/Cargo.toml index a1cb824..44daa8f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +regex = "1" \ No newline at end of file diff --git a/src/input.txt b/src/input.txt new file mode 100644 index 0000000..aa037dd --- /dev/null +++ b/src/input.txt @@ -0,0 +1,512 @@ +[T] [Q] [S] +[R] [M] [L] [V] [G] +[D] [V] [V] [Q] [N] [C] +[H] [T] [S] [C] [V] [D] [Z] +[Q] [J] [D] [M] [Z] [C] [M] [F] +[N] [B] [H] [N] [B] [W] [N] [J] [M] +[P] [G] [R] [Z] [Z] [C] [Z] [G] [P] +[B] [W] [N] [P] [D] [V] [G] [L] [T] + 1 2 3 4 5 6 7 8 9 + +move 5 from 4 to 9 +move 3 from 5 to 1 +move 12 from 9 to 6 +move 1 from 6 to 9 +move 3 from 2 to 8 +move 6 from 3 to 9 +move 2 from 2 to 9 +move 2 from 3 to 5 +move 9 from 8 to 1 +move 1 from 6 to 9 +move 1 from 8 to 3 +move 14 from 1 to 2 +move 8 from 2 to 6 +move 2 from 2 to 7 +move 2 from 5 to 8 +move 5 from 2 to 6 +move 9 from 7 to 8 +move 1 from 9 to 8 +move 5 from 6 to 9 +move 1 from 3 to 8 +move 1 from 7 to 5 +move 1 from 1 to 5 +move 4 from 1 to 7 +move 15 from 6 to 1 +move 4 from 7 to 6 +move 2 from 5 to 7 +move 9 from 8 to 7 +move 13 from 1 to 3 +move 8 from 6 to 9 +move 1 from 6 to 8 +move 1 from 7 to 5 +move 2 from 1 to 3 +move 4 from 7 to 1 +move 13 from 3 to 6 +move 2 from 1 to 3 +move 1 from 5 to 8 +move 2 from 3 to 4 +move 5 from 7 to 1 +move 4 from 1 to 9 +move 2 from 4 to 5 +move 4 from 6 to 2 +move 3 from 2 to 5 +move 6 from 8 to 1 +move 7 from 6 to 7 +move 1 from 3 to 5 +move 1 from 2 to 4 +move 8 from 1 to 8 +move 4 from 6 to 2 +move 3 from 5 to 3 +move 1 from 4 to 3 +move 2 from 1 to 3 +move 8 from 8 to 5 +move 2 from 3 to 8 +move 4 from 5 to 3 +move 1 from 9 to 2 +move 1 from 8 to 3 +move 1 from 2 to 1 +move 15 from 9 to 3 +move 6 from 7 to 5 +move 1 from 7 to 3 +move 2 from 2 to 8 +move 6 from 9 to 4 +move 22 from 3 to 6 +move 3 from 8 to 6 +move 1 from 1 to 2 +move 2 from 9 to 8 +move 6 from 4 to 7 +move 6 from 7 to 2 +move 16 from 6 to 9 +move 8 from 2 to 1 +move 4 from 6 to 1 +move 2 from 3 to 4 +move 9 from 5 to 4 +move 1 from 7 to 9 +move 1 from 6 to 2 +move 3 from 5 to 7 +move 16 from 9 to 4 +move 2 from 7 to 1 +move 4 from 6 to 3 +move 1 from 9 to 5 +move 1 from 9 to 7 +move 1 from 7 to 6 +move 1 from 7 to 9 +move 2 from 9 to 2 +move 1 from 6 to 1 +move 2 from 8 to 1 +move 11 from 4 to 2 +move 9 from 2 to 6 +move 9 from 6 to 1 +move 15 from 4 to 6 +move 1 from 4 to 2 +move 1 from 5 to 3 +move 6 from 6 to 4 +move 3 from 2 to 1 +move 2 from 4 to 6 +move 3 from 6 to 2 +move 7 from 6 to 2 +move 1 from 4 to 7 +move 1 from 7 to 2 +move 5 from 3 to 6 +move 1 from 5 to 4 +move 1 from 4 to 5 +move 8 from 1 to 6 +move 1 from 4 to 8 +move 12 from 6 to 1 +move 1 from 3 to 4 +move 1 from 4 to 1 +move 1 from 3 to 4 +move 2 from 6 to 5 +move 31 from 1 to 7 +move 2 from 5 to 7 +move 1 from 8 to 2 +move 1 from 5 to 8 +move 1 from 8 to 6 +move 3 from 4 to 9 +move 3 from 9 to 4 +move 2 from 4 to 3 +move 2 from 1 to 6 +move 2 from 3 to 8 +move 1 from 4 to 9 +move 4 from 2 to 9 +move 17 from 7 to 8 +move 3 from 8 to 2 +move 2 from 9 to 4 +move 4 from 2 to 5 +move 1 from 1 to 4 +move 1 from 9 to 3 +move 8 from 8 to 4 +move 1 from 9 to 4 +move 4 from 8 to 3 +move 8 from 2 to 5 +move 2 from 2 to 3 +move 1 from 2 to 1 +move 1 from 8 to 4 +move 2 from 8 to 1 +move 1 from 7 to 2 +move 1 from 8 to 6 +move 3 from 4 to 5 +move 8 from 4 to 7 +move 1 from 2 to 8 +move 1 from 8 to 1 +move 2 from 4 to 7 +move 8 from 5 to 9 +move 7 from 5 to 2 +move 6 from 3 to 1 +move 6 from 1 to 2 +move 9 from 9 to 4 +move 5 from 7 to 4 +move 2 from 1 to 2 +move 9 from 4 to 2 +move 3 from 6 to 2 +move 1 from 6 to 8 +move 1 from 8 to 9 +move 1 from 3 to 5 +move 6 from 7 to 5 +move 4 from 4 to 2 +move 19 from 2 to 3 +move 1 from 4 to 6 +move 7 from 7 to 5 +move 2 from 1 to 8 +move 12 from 3 to 4 +move 3 from 4 to 1 +move 1 from 6 to 3 +move 8 from 5 to 9 +move 3 from 9 to 7 +move 6 from 4 to 3 +move 3 from 1 to 2 +move 13 from 3 to 7 +move 3 from 4 to 6 +move 4 from 9 to 4 +move 14 from 7 to 8 +move 3 from 5 to 2 +move 3 from 2 to 6 +move 1 from 6 to 2 +move 1 from 3 to 9 +move 4 from 4 to 6 +move 11 from 2 to 7 +move 2 from 9 to 6 +move 3 from 5 to 6 +move 1 from 9 to 7 +move 14 from 6 to 5 +move 1 from 5 to 1 +move 4 from 5 to 8 +move 2 from 5 to 6 +move 4 from 2 to 5 +move 1 from 2 to 9 +move 14 from 8 to 5 +move 2 from 8 to 4 +move 3 from 8 to 7 +move 5 from 5 to 4 +move 13 from 5 to 7 +move 5 from 7 to 6 +move 31 from 7 to 9 +move 7 from 6 to 7 +move 6 from 5 to 7 +move 1 from 8 to 9 +move 1 from 5 to 3 +move 1 from 3 to 5 +move 1 from 1 to 8 +move 6 from 4 to 3 +move 1 from 8 to 5 +move 1 from 4 to 1 +move 33 from 9 to 3 +move 13 from 7 to 1 +move 29 from 3 to 2 +move 3 from 3 to 8 +move 1 from 5 to 2 +move 20 from 2 to 6 +move 19 from 6 to 4 +move 1 from 7 to 4 +move 5 from 1 to 7 +move 1 from 8 to 7 +move 2 from 8 to 5 +move 10 from 2 to 8 +move 6 from 3 to 9 +move 4 from 7 to 1 +move 1 from 3 to 5 +move 1 from 1 to 2 +move 1 from 7 to 6 +move 1 from 2 to 8 +move 1 from 8 to 7 +move 4 from 9 to 7 +move 2 from 5 to 2 +move 1 from 8 to 5 +move 1 from 8 to 6 +move 7 from 8 to 3 +move 2 from 9 to 4 +move 3 from 5 to 1 +move 2 from 2 to 5 +move 5 from 7 to 8 +move 10 from 4 to 1 +move 5 from 8 to 5 +move 10 from 1 to 3 +move 2 from 6 to 4 +move 1 from 7 to 3 +move 1 from 8 to 1 +move 3 from 5 to 8 +move 12 from 4 to 7 +move 3 from 5 to 3 +move 16 from 1 to 7 +move 2 from 3 to 7 +move 1 from 5 to 6 +move 3 from 8 to 4 +move 1 from 4 to 7 +move 1 from 6 to 3 +move 14 from 3 to 1 +move 5 from 3 to 8 +move 1 from 3 to 5 +move 1 from 7 to 6 +move 1 from 6 to 2 +move 13 from 7 to 2 +move 1 from 5 to 3 +move 3 from 4 to 2 +move 1 from 3 to 5 +move 3 from 8 to 9 +move 2 from 8 to 9 +move 1 from 6 to 4 +move 5 from 2 to 4 +move 3 from 2 to 5 +move 7 from 7 to 3 +move 7 from 4 to 7 +move 5 from 3 to 7 +move 8 from 2 to 3 +move 5 from 9 to 5 +move 11 from 1 to 9 +move 4 from 3 to 1 +move 1 from 2 to 7 +move 4 from 1 to 7 +move 22 from 7 to 3 +move 5 from 3 to 4 +move 1 from 7 to 1 +move 1 from 1 to 4 +move 3 from 4 to 6 +move 3 from 1 to 3 +move 2 from 6 to 1 +move 2 from 4 to 9 +move 13 from 9 to 1 +move 1 from 6 to 5 +move 4 from 7 to 1 +move 3 from 1 to 6 +move 19 from 3 to 9 +move 5 from 3 to 1 +move 18 from 9 to 8 +move 1 from 9 to 3 +move 11 from 1 to 7 +move 1 from 4 to 5 +move 13 from 8 to 1 +move 7 from 5 to 8 +move 7 from 8 to 5 +move 3 from 6 to 5 +move 2 from 3 to 9 +move 1 from 3 to 7 +move 5 from 5 to 2 +move 10 from 1 to 5 +move 9 from 7 to 9 +move 11 from 5 to 2 +move 2 from 8 to 4 +move 1 from 4 to 3 +move 2 from 7 to 3 +move 1 from 7 to 4 +move 3 from 8 to 3 +move 8 from 5 to 2 +move 2 from 3 to 8 +move 4 from 3 to 8 +move 6 from 2 to 6 +move 5 from 1 to 8 +move 8 from 2 to 7 +move 2 from 4 to 7 +move 9 from 2 to 9 +move 4 from 7 to 8 +move 5 from 1 to 8 +move 3 from 7 to 4 +move 1 from 8 to 3 +move 3 from 7 to 2 +move 3 from 1 to 9 +move 1 from 4 to 9 +move 1 from 6 to 3 +move 18 from 8 to 5 +move 1 from 8 to 2 +move 2 from 4 to 9 +move 3 from 2 to 1 +move 2 from 2 to 3 +move 24 from 9 to 8 +move 3 from 3 to 7 +move 15 from 8 to 2 +move 12 from 2 to 5 +move 1 from 7 to 4 +move 1 from 3 to 1 +move 28 from 5 to 4 +move 1 from 7 to 9 +move 2 from 2 to 1 +move 4 from 6 to 3 +move 1 from 5 to 3 +move 1 from 5 to 9 +move 1 from 2 to 6 +move 5 from 3 to 5 +move 8 from 4 to 2 +move 2 from 6 to 2 +move 1 from 7 to 3 +move 4 from 2 to 8 +move 3 from 1 to 2 +move 5 from 2 to 5 +move 3 from 5 to 4 +move 2 from 1 to 5 +move 2 from 2 to 1 +move 4 from 9 to 2 +move 7 from 8 to 9 +move 1 from 3 to 1 +move 1 from 1 to 7 +move 2 from 8 to 3 +move 4 from 9 to 3 +move 9 from 5 to 7 +move 3 from 3 to 5 +move 1 from 5 to 3 +move 7 from 7 to 9 +move 1 from 7 to 9 +move 1 from 5 to 9 +move 1 from 5 to 1 +move 1 from 8 to 5 +move 9 from 9 to 1 +move 2 from 7 to 2 +move 1 from 5 to 6 +move 4 from 3 to 2 +move 11 from 2 to 4 +move 1 from 8 to 4 +move 1 from 8 to 2 +move 1 from 2 to 8 +move 1 from 6 to 5 +move 1 from 8 to 6 +move 6 from 1 to 7 +move 1 from 5 to 6 +move 1 from 6 to 5 +move 3 from 9 to 8 +move 3 from 8 to 1 +move 3 from 7 to 8 +move 1 from 6 to 9 +move 1 from 2 to 4 +move 1 from 9 to 7 +move 2 from 7 to 9 +move 10 from 1 to 6 +move 2 from 9 to 3 +move 1 from 5 to 7 +move 3 from 7 to 5 +move 3 from 5 to 3 +move 4 from 6 to 3 +move 18 from 4 to 2 +move 3 from 4 to 1 +move 1 from 1 to 3 +move 2 from 1 to 2 +move 8 from 2 to 9 +move 1 from 4 to 7 +move 1 from 7 to 1 +move 3 from 9 to 2 +move 3 from 8 to 6 +move 1 from 4 to 9 +move 7 from 2 to 8 +move 7 from 6 to 7 +move 3 from 9 to 2 +move 3 from 2 to 5 +move 6 from 4 to 6 +move 2 from 5 to 6 +move 3 from 3 to 6 +move 6 from 6 to 3 +move 5 from 7 to 5 +move 2 from 4 to 8 +move 5 from 5 to 2 +move 1 from 7 to 2 +move 4 from 6 to 4 +move 1 from 7 to 8 +move 1 from 6 to 4 +move 1 from 5 to 7 +move 1 from 3 to 4 +move 1 from 6 to 4 +move 2 from 9 to 1 +move 3 from 1 to 3 +move 1 from 3 to 1 +move 9 from 2 to 1 +move 8 from 1 to 5 +move 1 from 7 to 1 +move 1 from 9 to 1 +move 4 from 5 to 7 +move 4 from 7 to 5 +move 1 from 1 to 9 +move 5 from 2 to 4 +move 1 from 9 to 6 +move 8 from 8 to 9 +move 18 from 4 to 9 +move 3 from 5 to 4 +move 2 from 6 to 5 +move 1 from 8 to 5 +move 17 from 9 to 6 +move 2 from 8 to 1 +move 1 from 4 to 6 +move 8 from 6 to 3 +move 1 from 1 to 8 +move 5 from 5 to 3 +move 1 from 1 to 7 +move 1 from 8 to 6 +move 2 from 4 to 5 +move 6 from 9 to 4 +move 1 from 7 to 5 +move 7 from 6 to 8 +move 2 from 6 to 5 +move 6 from 8 to 3 +move 1 from 9 to 6 +move 2 from 9 to 5 +move 1 from 3 to 1 +move 1 from 8 to 6 +move 7 from 5 to 6 +move 7 from 6 to 7 +move 5 from 4 to 9 +move 1 from 4 to 5 +move 2 from 9 to 6 +move 3 from 1 to 7 +move 5 from 6 to 8 +move 1 from 1 to 5 +move 21 from 3 to 6 +move 3 from 7 to 2 +move 2 from 9 to 3 +move 1 from 9 to 7 +move 5 from 5 to 7 +move 7 from 6 to 7 +move 14 from 7 to 1 +move 3 from 2 to 8 +move 12 from 1 to 4 +move 5 from 7 to 6 +move 1 from 7 to 4 +move 8 from 8 to 3 +move 8 from 3 to 5 +move 6 from 5 to 6 +move 1 from 5 to 3 +move 2 from 1 to 8 +move 2 from 8 to 3 +move 10 from 3 to 7 +move 8 from 4 to 3 +move 3 from 4 to 9 +move 3 from 9 to 2 +move 1 from 2 to 5 +move 2 from 2 to 9 +move 13 from 3 to 1 +move 1 from 4 to 1 +move 2 from 1 to 7 +move 1 from 5 to 8 +move 1 from 9 to 6 +move 1 from 9 to 2 +move 1 from 4 to 9 +move 8 from 6 to 2 +move 1 from 9 to 5 +move 1 from 2 to 8 +move 1 from 5 to 9 +move 2 from 2 to 3 +move 12 from 6 to 8 +move 1 from 3 to 7 +move 8 from 8 to 4 +move 1 from 9 to 1 +move 13 from 1 to 3 +move 2 from 4 to 5 +move 12 from 7 to 2 +move 1 from 5 to 8 +move 3 from 3 to 8 +move 2 from 4 to 1 +move 1 from 1 to 9 diff --git a/src/main.rs b/src/main.rs index e7a11a9..104f3bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,106 @@ -fn main() { - println!("Hello, world!"); +use regex::Regex; + +const DATA: &'static str = include_str!("input.txt"); + +fn split_into_crates_and_procedures(data: &str) -> (&str, &str) +{ + return data.split_once("\n\n").expect("Input data is not correctly structured"); +} + +fn process_crates(crates_and_numbers: &str) -> Vec> +{ + let (crates, _) = crates_and_numbers.rsplit_once("\n").expect("Input data is not correctly structured"); + let mut result: Vec> = Vec::new(); + for line in crates.lines() + { + let mut index = 0usize; + let mut whitespace = 0u32; + for c in line.chars() + { + if c.is_whitespace() + { + whitespace += 1; + if whitespace == 4 + { + if result.len() == index { + let inner_vec: Vec = Vec::new(); + result.push(inner_vec); + } + index += 1; + whitespace = 0; + } + continue; + } + + whitespace = 0; + + if c == '[' + { + continue; + } + + if c.is_ascii_uppercase() + { + if let Some(inner_vec) = result.get_mut(index) { + inner_vec.push(c); + } else { + assert!(result.len() == index); + let mut inner_vec: Vec = Vec::new(); + inner_vec.push(c); + result.push(inner_vec); + } + } + + if c == ']' + { + index += 1; + continue; + } + } + } + for vec in result.iter_mut() + { + vec.reverse(); + } + result +} + +fn part1() +{ + let (crates, procedures) = split_into_crates_and_procedures(DATA); + let mut crate_vec = process_crates(crates); + + let procedure_regex = Regex::new(r"move (\d+) from (\d+) to (\d+)").unwrap(); + + for cap in procedure_regex.captures_iter(procedures) + { + let crates_moved: i32 = cap[1].parse().unwrap(); + let starting_stack_index: usize = cap[2].parse().unwrap(); + let ending_stack_index: usize = cap[3].parse().unwrap(); + for _ in 0..crates_moved + { + let current_crate: char; + { + let starting_stack = crate_vec.get_mut(starting_stack_index-1).unwrap(); + current_crate = starting_stack.pop().unwrap(); + } + + let ending_stack = crate_vec.get_mut(ending_stack_index-1).unwrap(); + + ending_stack.push(current_crate); + } + } + + let mut result = String::new(); + + for stack in crate_vec + { + result.push(*stack.last().unwrap()); + } + + println!("Final layout is {}", result.as_str()); +} + +fn main() { + part1(); }