Day 5 puzzle

master
Wynd 2022-12-05 14:08:57 +02:00
parent b2fed2d674
commit 95ea56079c
6 changed files with 641 additions and 10 deletions

5
Cargo.lock generated
View File

@ -10,6 +10,7 @@ dependencies = [
"day2", "day2",
"day3", "day3",
"day4", "day4",
"day5",
] ]
[[package]] [[package]]
@ -27,3 +28,7 @@ version = "0.1.0"
[[package]] [[package]]
name = "day4" name = "day4"
version = "0.1.0" version = "0.1.0"
[[package]]
name = "day5"
version = "0.1.0"

View File

@ -7,14 +7,12 @@ edition = "2021"
[workspace] [workspace]
members = [ members = [
"day1", "day*"
"day2",
"day3",
"day4"
] ]
[dependencies] [dependencies]
day1 = { path = "./day1" } day1 = { path = "./day1" }
day2 = { path = "./day2" } day2 = { path = "./day2" }
day3 = { path = "./day3" } day3 = { path = "./day3" }
day4 = { path = "./day4" } day4 = { path = "./day4" }
day5 = { path = "./day5" }

8
day5/Cargo.toml 100644
View File

@ -0,0 +1,8 @@
[package]
name = "day5"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

514
day5/src/input.txt 100644
View File

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

108
day5/src/lib.rs 100644
View File

@ -0,0 +1,108 @@
pub fn start() {
let input = include_str!("input.txt").replace("\r", "");
let (crates, moves) = input.split_once("\n\n").unwrap();
let mut stacks = get_stacks(crates);
moves.lines().map(|f| Move::from_line(f)).for_each(|m| {
(0..m.amount).for_each(|_| {
let from = stacks[m.from - 1].pop().unwrap();
stacks[m.to - 1].push(from);
})
});
let res1 = stacks
.iter_mut()
.map(|f| f.pop().unwrap_or(' '))
.collect::<String>();
println!("Part 1: {}", res1);
let mut stacks = get_stacks(crates);
moves.lines().map(|f| Move::from_line(f)).for_each(|m| {
let amount = stacks[m.from - 1].len() - m.amount..;
let crates: Vec<char> = stacks[m.from - 1].drain(amount).collect();
stacks[m.to - 1].extend(crates);
});
let res2 = stacks
.iter_mut()
.map(|s| s.pop().unwrap_or(' '))
.collect::<String>();
println!("Part 2: {}", res2);
}
pub fn get_stacks(crates: &str) -> Vec<Vec<char>> {
let width = crates
.lines()
.rev()
.next()
.unwrap()
.split_whitespace()
.last()
.unwrap()
.parse::<usize>()
.unwrap();
let mut stacks: Vec<Vec<char>> = Vec::with_capacity(width);
let stack_size = crates.lines().count() - 1;
for _ in 0..width {
stacks.push(Vec::with_capacity(stack_size))
}
crates.lines().rev().skip(1).for_each(|f| {
let mut crates = f.chars().skip(1).step_by(4);
stacks.iter_mut().for_each(|f| {
if let Some(c) = crates.next() {
if c.is_alphabetic() {
f.push(c)
}
}
})
});
let width = crates
.lines()
.rev()
.next()
.unwrap()
.split_whitespace()
.last()
.unwrap()
.parse::<usize>()
.unwrap();
let mut stacks: Vec<Vec<char>> = Vec::with_capacity(width);
let stack_size = crates.lines().count() - 1;
for _ in 0..width {
stacks.push(Vec::with_capacity(stack_size))
}
crates.lines().rev().skip(1).for_each(|f| {
let mut crates = f.chars().skip(1).step_by(4);
stacks.iter_mut().for_each(|f| {
if let Some(c) = crates.next() {
if c.is_alphabetic() {
f.push(c)
}
}
})
});
stacks
}
#[derive(Debug)]
struct Move {
amount: usize,
from: usize,
to: usize,
}
impl Move {
fn from_line(line: &str) -> Self {
let mut split = line.split(" ");
Self {
amount: split.nth(1).unwrap().parse::<usize>().unwrap(),
from: split.nth(1).unwrap().parse::<usize>().unwrap(),
to: split.nth(1).unwrap().parse::<usize>().unwrap(),
}
}
}

View File

@ -1,10 +1,8 @@
use day1;
use day2;
use day3;
use day4;
fn main() { fn main() {
// day1::start(); // day1::start();
// day2::start(); // day2::start();
// day3::start(); // day3::start();
day4::start(); // day4::start();
day5::start();
} }