aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorj-james2022-12-05 10:23:07 +0000
committerj-james2022-12-05 10:23:07 +0000
commit8ebbb44dd19639cba8e25aaba5ffe00ba957c58f (patch)
tree0ffcd32e207f89831abaabd52485799c718e53d4
parent82be8473d21c15bc22c46cc4ba5463e3d2213c7d (diff)
Day Five
-rw-r--r--2022/nim/day05/src/main.nim36
-rw-r--r--2022/rust/day05/Cargo.lock16
-rw-r--r--2022/rust/day05/Cargo.toml9
-rw-r--r--2022/rust/day05/src/main.rs54
4 files changed, 115 insertions, 0 deletions
diff --git a/2022/nim/day05/src/main.nim b/2022/nim/day05/src/main.nim
new file mode 100644
index 0000000..5d7296f
--- /dev/null
+++ b/2022/nim/day05/src/main.nim
@@ -0,0 +1,36 @@
+# Day 5: Supply Stacks
+import std/[os, strutils, sequtils]
+
+let input = paramStr(1).readFile().strip().split("\n\n")
+
+func remove(a: string, chars: varargs[string]): string =
+ result = a
+ for c in chars:
+ result = result.replace(c, "")
+
+func transform(crates: string): seq[string] =
+ var c = 0
+ result = newSeq[string](9)
+ for i, stack in crates.strip().replace(" ", " [ ]").replace("] [", "")
+ .split("\n")[0..^2].mapIt(it.strip(chars = {'[', ']'})):
+ for j, c in stack:
+ result[j] = c & result[j]
+ result.applyIt(it.strip())
+
+let stacks = input[0].transform().mapIt(it.toSeq)
+let rules = input[1].split("\n")
+ .mapIt(it.remove("move ", "from ", "to ").split(" ").map(parseInt))
+
+var stack = stacks
+for rule in rules:
+ for count in 0 ..< rule[0]:
+ stack[rule[2]-1] &= stack[rule[1]-1].pop
+echo stack.foldl(a & b[b.len-1], "")
+
+stack = stacks
+for rule in rules:
+ var toAdd: seq[char]
+ for count in 0 ..< rule[0]:
+ toAdd = stack[rule[1]-1].pop & toAdd
+ stack[rule[2]-1] &= toAdd
+echo stack.foldl(a & b[b.len-1], "")
diff --git a/2022/rust/day05/Cargo.lock b/2022/rust/day05/Cargo.lock
new file mode 100644
index 0000000..92653ff
--- /dev/null
+++ b/2022/rust/day05/Cargo.lock
@@ -0,0 +1,16 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "day05"
+version = "0.1.0"
+dependencies = [
+ "text_io",
+]
+
+[[package]]
+name = "text_io"
+version = "0.1.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d5f0c8eb2ad70c12a6a69508f499b3051c924f4b1cfeae85bfad96e6bc5bba46"
diff --git a/2022/rust/day05/Cargo.toml b/2022/rust/day05/Cargo.toml
new file mode 100644
index 0000000..4cc1d88
--- /dev/null
+++ b/2022/rust/day05/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "day05"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+text_io = "0.1.12"
diff --git a/2022/rust/day05/src/main.rs b/2022/rust/day05/src/main.rs
new file mode 100644
index 0000000..b13d16e
--- /dev/null
+++ b/2022/rust/day05/src/main.rs
@@ -0,0 +1,54 @@
+use std::collections::VecDeque;
+use std::env;
+use std::fs;
+use text_io::scan;
+
+fn main() {
+ let args = env::args().nth(1).expect("missing input");
+ let input = fs::read_to_string(args).unwrap();
+ let mut parts = input.split("\n\n");
+
+ let crates = parts.next().unwrap();
+ // yes, it's gross. yes, it works. no, i couldn't think of anything better.
+ let mut stacks: Vec<Vec<char>> = vec![];
+ for i in 0..9 {
+ let mut stack: Vec<char> = vec![];
+ for j in 0..8 {
+ stack.push(crates.chars().nth((9*4) * (7-j) + 4*i + 1).unwrap());
+ }
+ while *stack.last().unwrap() == ' ' {
+ stack.pop();
+ }
+ stacks.push(stack);
+ }
+
+ let instructions: Vec<[usize; 3]> = parts.next().unwrap().trim().split("\n").map(|x| {
+ let a: usize;
+ let b: usize;
+ let c: usize;
+ scan!(x.bytes() => "move {} from {} to {}", a, b, c);
+ return [a, b, c];
+ }).collect(); // collect() still messes me up
+ let capacity: usize = instructions.iter().fold(0, |x,y| x.max(y[0]));
+
+ let mut stack = stacks.clone();
+ for ins in &instructions {
+ for _ in 0 .. ins[0] {
+ let cargo = stack[ins[1]-1].pop().unwrap();
+ stack[ins[2]-1].push(cargo);
+ }
+ }
+ let answer: String = stack.iter().map(|x| x.last().unwrap()).collect();
+ println!("{}", answer);
+
+ let mut stack = stacks.clone();
+ for ins in instructions {
+ let mut cargo: VecDeque<char> = VecDeque::with_capacity(capacity);
+ for _ in 0 .. ins[0] {
+ cargo.push_front(stack[ins[1]-1].pop().unwrap());
+ }
+ stack[ins[2]-1].append(&mut cargo.into());
+ }
+ let answer: String = stack.iter().map(|x| x.last().unwrap()).collect();
+ println!("{}", answer);
+}