diff options
author | j-james | 2022-12-05 10:23:07 +0000 |
---|---|---|
committer | j-james | 2022-12-05 10:23:07 +0000 |
commit | 8ebbb44dd19639cba8e25aaba5ffe00ba957c58f (patch) | |
tree | 0ffcd32e207f89831abaabd52485799c718e53d4 /2022/nim/day05 | |
parent | 82be8473d21c15bc22c46cc4ba5463e3d2213c7d (diff) |
Day Five
Diffstat (limited to '2022/nim/day05')
-rw-r--r-- | 2022/nim/day05/src/main.nim | 36 |
1 files changed, 36 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], "") |