Day 5 - Supply Stacks (Puzzle 1)

Successfully finished the stack and instruction parsing and execution in Rust.
Took a bit and I've discovered that VecDeque are just more handy than plain Vecs but it works.
main
Peery 2 years ago
parent 76ae30f975
commit 8783562d39

49
day5/Cargo.lock generated

@ -0,0 +1,49 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "advent_of_code_day5"
version = "0.0.1"
dependencies = [
"lazy_static",
"regex",
]
[[package]]
name = "aho-corasick"
version = "0.7.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
dependencies = [
"memchr",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "regex"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"

@ -0,0 +1,9 @@
[package]
name = "advent_of_code_day5"
version = "0.0.1"
authors = ["Peery"]
edition = "2021"
[dependencies]
regex = "1"
lazy_static = "1.4.0"

@ -0,0 +1,9 @@
[D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2

@ -0,0 +1,511 @@
[V] [T] [J]
[Q] [M] [P] [Q] [J]
[W] [B] [N] [Q] [C] [T]
[M] [C] [F] [N] [G] [W] [G]
[B] [W] [J] [H] [L] [R] [B] [C]
[N] [R] [R] [W] [W] [W] [D] [N] [F]
[Z] [Z] [Q] [S] [F] [P] [B] [Q] [L]
[C] [H] [F] [Z] [G] [L] [V] [Z] [H]
1 2 3 4 5 6 7 8 9
move 2 from 1 to 7
move 6 from 2 to 6
move 10 from 7 to 6
move 4 from 3 to 1
move 5 from 6 to 4
move 1 from 1 to 9
move 4 from 6 to 9
move 12 from 4 to 1
move 5 from 1 to 4
move 7 from 9 to 8
move 11 from 8 to 1
move 6 from 6 to 2
move 2 from 5 to 2
move 3 from 6 to 3
move 4 from 9 to 4
move 2 from 2 to 5
move 1 from 6 to 4
move 3 from 3 to 6
move 1 from 8 to 4
move 1 from 6 to 1
move 28 from 1 to 4
move 28 from 4 to 5
move 1 from 9 to 1
move 4 from 4 to 1
move 2 from 6 to 2
move 2 from 1 to 6
move 7 from 4 to 2
move 14 from 2 to 9
move 1 from 4 to 1
move 1 from 1 to 2
move 18 from 5 to 6
move 2 from 2 to 6
move 1 from 9 to 7
move 8 from 9 to 2
move 15 from 6 to 5
move 1 from 6 to 3
move 3 from 2 to 5
move 1 from 7 to 5
move 2 from 1 to 3
move 3 from 2 to 1
move 1 from 6 to 4
move 5 from 6 to 5
move 2 from 2 to 9
move 35 from 5 to 7
move 4 from 9 to 3
move 1 from 4 to 1
move 5 from 1 to 7
move 6 from 5 to 3
move 1 from 9 to 4
move 11 from 7 to 6
move 2 from 9 to 2
move 1 from 4 to 7
move 14 from 7 to 4
move 5 from 6 to 9
move 2 from 2 to 4
move 6 from 7 to 9
move 2 from 9 to 5
move 6 from 9 to 5
move 8 from 4 to 9
move 5 from 4 to 3
move 3 from 5 to 7
move 1 from 3 to 9
move 5 from 3 to 4
move 7 from 9 to 8
move 2 from 7 to 4
move 4 from 5 to 7
move 1 from 5 to 3
move 5 from 6 to 4
move 8 from 4 to 8
move 5 from 7 to 6
move 1 from 4 to 7
move 3 from 6 to 9
move 2 from 6 to 5
move 7 from 8 to 3
move 2 from 5 to 9
move 17 from 3 to 1
move 3 from 1 to 3
move 6 from 8 to 9
move 4 from 4 to 7
move 6 from 3 to 5
move 2 from 8 to 5
move 14 from 7 to 5
move 2 from 4 to 5
move 6 from 9 to 5
move 1 from 7 to 9
move 1 from 6 to 9
move 8 from 1 to 9
move 8 from 5 to 2
move 2 from 1 to 3
move 7 from 2 to 6
move 2 from 3 to 4
move 1 from 2 to 6
move 3 from 1 to 6
move 16 from 9 to 4
move 2 from 9 to 8
move 1 from 1 to 6
move 2 from 9 to 4
move 1 from 6 to 9
move 1 from 6 to 1
move 1 from 1 to 7
move 1 from 6 to 9
move 1 from 9 to 3
move 1 from 3 to 8
move 1 from 9 to 2
move 1 from 2 to 7
move 2 from 5 to 3
move 7 from 5 to 8
move 2 from 7 to 9
move 1 from 6 to 7
move 3 from 6 to 9
move 10 from 8 to 7
move 1 from 4 to 3
move 3 from 3 to 1
move 1 from 7 to 1
move 19 from 4 to 6
move 3 from 9 to 7
move 1 from 9 to 2
move 2 from 1 to 7
move 1 from 9 to 1
move 12 from 6 to 9
move 2 from 7 to 1
move 1 from 2 to 4
move 11 from 6 to 3
move 1 from 4 to 8
move 1 from 6 to 8
move 11 from 7 to 9
move 2 from 8 to 9
move 18 from 9 to 6
move 5 from 3 to 7
move 5 from 3 to 8
move 11 from 5 to 6
move 26 from 6 to 4
move 1 from 6 to 5
move 1 from 3 to 7
move 3 from 8 to 3
move 1 from 8 to 7
move 3 from 3 to 6
move 5 from 9 to 3
move 1 from 4 to 9
move 8 from 4 to 5
move 2 from 7 to 8
move 3 from 3 to 6
move 3 from 4 to 6
move 7 from 7 to 4
move 1 from 9 to 1
move 5 from 5 to 3
move 2 from 9 to 7
move 3 from 8 to 2
move 7 from 3 to 7
move 1 from 7 to 6
move 3 from 5 to 6
move 7 from 4 to 8
move 10 from 4 to 5
move 2 from 4 to 2
move 3 from 7 to 5
move 2 from 4 to 1
move 6 from 8 to 5
move 5 from 1 to 4
move 5 from 4 to 2
move 5 from 7 to 8
move 10 from 2 to 8
move 3 from 8 to 3
move 2 from 5 to 3
move 13 from 6 to 1
move 19 from 5 to 3
move 12 from 3 to 9
move 4 from 8 to 2
move 2 from 6 to 7
move 5 from 8 to 7
move 9 from 3 to 9
move 1 from 5 to 9
move 2 from 7 to 6
move 3 from 2 to 3
move 15 from 9 to 3
move 13 from 3 to 5
move 1 from 6 to 2
move 5 from 5 to 8
move 1 from 2 to 5
move 1 from 7 to 6
move 6 from 9 to 6
move 6 from 6 to 8
move 4 from 7 to 1
move 2 from 3 to 6
move 11 from 1 to 9
move 1 from 2 to 3
move 4 from 5 to 6
move 1 from 1 to 6
move 10 from 9 to 2
move 8 from 2 to 3
move 3 from 1 to 2
move 8 from 3 to 1
move 5 from 5 to 4
move 1 from 9 to 8
move 2 from 3 to 7
move 2 from 4 to 5
move 6 from 1 to 6
move 9 from 8 to 1
move 16 from 1 to 9
move 2 from 7 to 3
move 3 from 3 to 8
move 6 from 9 to 6
move 1 from 5 to 4
move 1 from 3 to 8
move 5 from 2 to 1
move 5 from 1 to 9
move 2 from 4 to 9
move 4 from 8 to 6
move 1 from 8 to 7
move 4 from 8 to 5
move 2 from 8 to 2
move 17 from 9 to 5
move 11 from 5 to 7
move 1 from 2 to 5
move 1 from 2 to 5
move 1 from 9 to 1
move 1 from 1 to 6
move 5 from 7 to 6
move 20 from 6 to 7
move 4 from 6 to 4
move 15 from 7 to 8
move 2 from 3 to 7
move 1 from 6 to 5
move 10 from 8 to 4
move 1 from 3 to 6
move 4 from 6 to 4
move 13 from 7 to 8
move 1 from 7 to 5
move 1 from 6 to 3
move 1 from 6 to 3
move 1 from 6 to 9
move 9 from 4 to 1
move 3 from 8 to 2
move 14 from 5 to 6
move 2 from 2 to 8
move 1 from 3 to 9
move 14 from 6 to 2
move 1 from 3 to 9
move 1 from 9 to 3
move 15 from 2 to 1
move 1 from 3 to 9
move 4 from 4 to 9
move 10 from 8 to 5
move 1 from 9 to 5
move 1 from 1 to 5
move 4 from 8 to 7
move 3 from 9 to 3
move 1 from 8 to 5
move 1 from 4 to 7
move 2 from 8 to 7
move 6 from 5 to 6
move 4 from 1 to 2
move 1 from 2 to 5
move 2 from 2 to 8
move 2 from 8 to 1
move 3 from 7 to 2
move 3 from 4 to 9
move 18 from 1 to 8
move 1 from 7 to 3
move 3 from 9 to 6
move 1 from 1 to 5
move 5 from 6 to 4
move 2 from 1 to 9
move 8 from 4 to 5
move 4 from 3 to 2
move 16 from 5 to 4
move 8 from 8 to 6
move 2 from 2 to 6
move 1 from 7 to 6
move 7 from 8 to 1
move 1 from 2 to 3
move 2 from 8 to 3
move 4 from 4 to 9
move 4 from 1 to 2
move 1 from 7 to 2
move 1 from 5 to 4
move 1 from 3 to 7
move 3 from 4 to 5
move 1 from 9 to 6
move 9 from 2 to 5
move 2 from 3 to 6
move 3 from 5 to 8
move 3 from 1 to 7
move 4 from 5 to 8
move 1 from 4 to 3
move 5 from 9 to 5
move 5 from 5 to 8
move 1 from 3 to 4
move 4 from 5 to 1
move 2 from 5 to 4
move 13 from 6 to 2
move 12 from 2 to 9
move 3 from 9 to 2
move 4 from 1 to 6
move 8 from 6 to 2
move 1 from 4 to 9
move 3 from 7 to 9
move 2 from 9 to 8
move 1 from 7 to 2
move 9 from 9 to 5
move 2 from 8 to 6
move 4 from 2 to 3
move 1 from 7 to 2
move 1 from 6 to 4
move 4 from 3 to 9
move 9 from 5 to 8
move 10 from 4 to 2
move 1 from 4 to 7
move 1 from 6 to 2
move 1 from 6 to 7
move 13 from 2 to 6
move 1 from 2 to 5
move 6 from 6 to 5
move 7 from 5 to 8
move 1 from 4 to 5
move 27 from 8 to 5
move 3 from 6 to 3
move 2 from 8 to 6
move 8 from 9 to 5
move 1 from 7 to 9
move 1 from 6 to 2
move 4 from 5 to 9
move 2 from 3 to 4
move 9 from 2 to 5
move 1 from 4 to 1
move 1 from 4 to 2
move 1 from 2 to 4
move 1 from 3 to 7
move 1 from 1 to 3
move 1 from 3 to 9
move 6 from 9 to 4
move 1 from 7 to 5
move 13 from 5 to 2
move 1 from 9 to 5
move 1 from 7 to 2
move 5 from 2 to 7
move 8 from 5 to 7
move 6 from 4 to 2
move 1 from 4 to 5
move 3 from 2 to 4
move 4 from 2 to 7
move 2 from 4 to 3
move 13 from 7 to 3
move 5 from 2 to 3
move 4 from 7 to 8
move 11 from 3 to 8
move 11 from 5 to 9
move 4 from 6 to 9
move 1 from 6 to 5
move 1 from 4 to 2
move 1 from 3 to 6
move 3 from 2 to 6
move 3 from 6 to 2
move 1 from 6 to 1
move 1 from 3 to 8
move 3 from 3 to 6
move 2 from 2 to 7
move 4 from 3 to 9
move 16 from 9 to 2
move 1 from 7 to 8
move 2 from 2 to 8
move 9 from 2 to 3
move 6 from 2 to 7
move 1 from 6 to 3
move 2 from 9 to 2
move 1 from 9 to 7
move 2 from 6 to 3
move 4 from 3 to 9
move 2 from 2 to 7
move 1 from 2 to 5
move 14 from 5 to 6
move 14 from 6 to 3
move 4 from 9 to 8
move 5 from 8 to 4
move 1 from 1 to 5
move 4 from 8 to 1
move 1 from 5 to 9
move 8 from 7 to 2
move 18 from 3 to 7
move 1 from 1 to 5
move 1 from 1 to 9
move 1 from 4 to 5
move 1 from 8 to 5
move 8 from 2 to 9
move 3 from 5 to 8
move 7 from 7 to 1
move 3 from 4 to 7
move 1 from 3 to 6
move 7 from 8 to 3
move 2 from 9 to 3
move 3 from 8 to 9
move 9 from 1 to 7
move 9 from 3 to 4
move 2 from 3 to 4
move 12 from 7 to 4
move 1 from 3 to 8
move 1 from 8 to 7
move 8 from 4 to 7
move 11 from 4 to 9
move 5 from 4 to 8
move 19 from 7 to 9
move 1 from 6 to 2
move 2 from 7 to 4
move 2 from 8 to 3
move 1 from 7 to 8
move 1 from 3 to 2
move 3 from 8 to 4
move 1 from 8 to 9
move 1 from 3 to 2
move 36 from 9 to 1
move 5 from 9 to 6
move 5 from 4 to 2
move 24 from 1 to 3
move 5 from 6 to 7
move 1 from 1 to 4
move 14 from 3 to 4
move 4 from 7 to 3
move 1 from 8 to 5
move 5 from 2 to 9
move 1 from 1 to 6
move 5 from 9 to 1
move 3 from 2 to 3
move 1 from 5 to 3
move 11 from 4 to 2
move 1 from 7 to 1
move 6 from 1 to 9
move 3 from 4 to 2
move 1 from 6 to 7
move 10 from 1 to 7
move 3 from 2 to 1
move 3 from 3 to 2
move 2 from 1 to 7
move 1 from 4 to 8
move 13 from 3 to 2
move 1 from 8 to 3
move 2 from 7 to 5
move 2 from 3 to 7
move 2 from 5 to 2
move 1 from 1 to 7
move 28 from 2 to 6
move 1 from 2 to 3
move 2 from 8 to 2
move 6 from 9 to 7
move 1 from 3 to 8
move 1 from 9 to 8
move 3 from 6 to 2
move 14 from 7 to 9
move 3 from 2 to 1
move 2 from 2 to 9
move 2 from 1 to 9
move 1 from 9 to 1
move 7 from 6 to 9
move 2 from 1 to 4
move 2 from 4 to 6
move 4 from 8 to 7
move 1 from 7 to 6
move 1 from 8 to 1
move 1 from 3 to 6
move 1 from 1 to 5
move 14 from 9 to 8
move 1 from 5 to 9
move 5 from 7 to 3
move 16 from 6 to 3
move 2 from 7 to 4
move 8 from 9 to 5
move 6 from 6 to 1
move 8 from 5 to 9
move 2 from 7 to 4
move 11 from 9 to 1
move 4 from 4 to 1
move 14 from 8 to 3
move 2 from 1 to 7
move 20 from 3 to 6
move 5 from 3 to 1
move 1 from 3 to 5
move 2 from 7 to 4
move 20 from 6 to 7
move 18 from 7 to 6
move 17 from 6 to 9
move 1 from 5 to 3
move 6 from 3 to 2
move 3 from 3 to 1
move 1 from 6 to 2
move 2 from 7 to 8
move 4 from 1 to 5
move 2 from 4 to 9
move 1 from 3 to 2
move 1 from 8 to 6
move 18 from 1 to 4
move 1 from 2 to 7
move 1 from 6 to 2
move 3 from 4 to 3
move 1 from 8 to 1
move 4 from 1 to 6
move 7 from 2 to 1
move 1 from 5 to 7
move 1 from 4 to 1
move 2 from 6 to 3
move 3 from 5 to 9
move 9 from 9 to 8
move 10 from 9 to 3
move 9 from 3 to 5

@ -0,0 +1,137 @@
#![crate_name = "advent_of_code_day5"]
use std::fs;
use std::path::{Path};
use regex::Regex;
use lazy_static::lazy_static;
use std::collections::VecDeque;
fn get_crates_from_line(line: &str) -> Vec<char> {
lazy_static! {
static ref CRATE_RE: Regex = Regex::new(r"\[[A-Z]\]").unwrap();
}
let mut crates: Vec<char> = Vec::new();
for cap in CRATE_RE.captures_iter(line) {
//println!("Caught with regex: {}", &cap[0]);
crates.push(cap[0].chars().nth(1).unwrap());
}
return crates;
}
fn get_instruction_from_line(line: &str) -> Option<[u32; 3]> {
lazy_static! {
static ref INST_RE: Regex = Regex::new(r"move ([0-9]+) from ([0-9]+) to ([0-9]+)").unwrap();
}
if INST_RE.is_match(line) {
let cap = INST_RE.captures(line).unwrap();
let instruction: [u32; 3] = [cap[1].parse::<u32>().unwrap(), cap[2].parse::<u32>().unwrap(), cap[3].parse::<u32>().unwrap()];
//println!("Instruction: {} from {} to {}", instruction[0], instruction[1], instruction[2]);
return Some(instruction);
}
else {
return None;
}
}
fn vec_contains_char(c: &char, v: &Vec<char>) -> bool {
for i in v {
if c == i {
return true;
}
}
return false;
}
fn parse_file(input: &str) -> (Vec<VecDeque<char>>, Vec<[u32; 3]>){
// Separate string into stack part and instructions
let lines: Vec<&str> = input.split("\n").collect();
let mut stack_column_count: i32 = 0;
let mut stack_part: Vec<VecDeque<char>> = Vec::new();
let mut instruction_part: Vec<[u32; 3]> = Vec::new();
for line in lines {
//println!("Line: {}", line);
// Getting the crate stacks
let crate_result = get_crates_from_line(line);
if crate_result.len() != 0 {
if crate_result.len() > stack_column_count as usize {
stack_column_count = crate_result.len() as i32;
}
for (i, c) in line.chars().enumerate() {
if vec_contains_char(&c, &crate_result) {
//println!("Found character {} in position {} within result {:?} and line {}", c, i / 4, crate_result, line);
while stack_part.len() < (i/4)+1 { // Does stack_part have a stack for slot (i/4)? If not push a new stack
stack_part.push(VecDeque::new());
//println!("Created fresh new stack #{}!", stack_part.len()-1);
}
//println!("Pushing crate {} onto stack slot #{}", c, i/4);
stack_part[i/4].push_front(c);
}
}
continue;
}
// Getting the instructions
let inst_result = get_instruction_from_line(line);
if match inst_result {Some(_x) => true,None => false,} {
//println!("Instruction result: {:?}", inst_result.unwrap());
instruction_part.push(inst_result.unwrap());
continue;
}
}
return (stack_part, instruction_part);
}
fn simulate_stack_operation(mut stacks: Vec<VecDeque<char>>, instruction: [u32; 3]) -> Vec<VecDeque<char>> {
// Simulate the given instruction being executed onto the given stacks and return a new stacks vector with the changes.
let amount: u32 = instruction[0];
let source: u32 = instruction[1]-1;
let target: u32 = instruction[2]-1;
//println!("Executing instruction {:?}", instruction);
for _ in 0..amount { // do the instruction amount times
let c: char = stacks[source as usize].pop_back().unwrap();
//println!("Took crate {} from stack #{} and placed it onto stack #{}", c, source, target);
stacks[target as usize].push_back(c);
}
return stacks;
}
fn main() {
let input_path = Path::new("./input/input.lst");
let contents = fs::read_to_string(input_path).unwrap();
println!("My given file: \n{}\n\n", contents);
let (mut stacks, insts) = parse_file(&contents);
println!("Stack part: {:?}", stacks);
println!("Instr part: {:?}\n", insts);
for instruction in &insts {
let next_stacks = simulate_stack_operation(stacks, *instruction);
//println!("Stack part: {:?}\n", next_stacks);
stacks = next_stacks;
}
println!("Final stacks looks like this: {:?}", stacks);
let mut top_crates: String = "".to_owned();
for stack in &stacks {
let mut tmp = [0;4];
top_crates.push_str(&*stack.back().unwrap().encode_utf8(&mut tmp));
}
println!("Top-most crates are: {:?}", top_crates);
}
Loading…
Cancel
Save