about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2022-12-06 14:23:44 +0100
committerLeah Neukirchen <leah@vuxu.org>2022-12-06 14:23:44 +0100
commit0ffe55a0d6d5dbcff5cbc19507b336366f7fb40c (patch)
tree58fca570328f59ca08dc92b3cea90dcf8eacc730
parent68ee54cfaa49f438bb7ad93e7c595e72c64dfbbd (diff)
downloadadventofcode2022-0ffe55a0d6d5dbcff5cbc19507b336366f7fb40c.tar.gz
adventofcode2022-0ffe55a0d6d5dbcff5cbc19507b336366f7fb40c.tar.xz
adventofcode2022-0ffe55a0d6d5dbcff5cbc19507b336366f7fb40c.zip
day05
-rw-r--r--day05512
-rw-r--r--day05.rkt29
-rw-r--r--day05.zig60
3 files changed, 601 insertions, 0 deletions
diff --git a/day05 b/day05
new file mode 100644
index 0000000..ec501fd
--- /dev/null
+++ b/day05
@@ -0,0 +1,512 @@
+                    [L]     [H] [W]
+                [J] [Z] [J] [Q] [Q]
+[S]             [M] [C] [T] [F] [B]
+[P]     [H]     [B] [D] [G] [B] [P]
+[W]     [L] [D] [D] [J] [W] [T] [C]
+[N] [T] [R] [T] [T] [T] [M] [M] [G]
+[J] [S] [Q] [S] [Z] [W] [P] [G] [D]
+[Z] [G] [V] [V] [Q] [M] [L] [N] [R]
+ 1   2   3   4   5   6   7   8   9 
+
+move 1 from 3 to 5
+move 2 from 2 to 8
+move 4 from 1 to 3
+move 2 from 1 to 4
+move 1 from 7 to 1
+move 2 from 9 to 7
+move 4 from 5 to 9
+move 7 from 8 to 9
+move 2 from 5 to 2
+move 1 from 2 to 9
+move 1 from 1 to 8
+move 1 from 2 to 7
+move 3 from 8 to 2
+move 6 from 9 to 7
+move 5 from 4 to 1
+move 7 from 9 to 5
+move 1 from 4 to 5
+move 4 from 1 to 7
+move 1 from 8 to 1
+move 4 from 7 to 9
+move 1 from 5 to 8
+move 9 from 9 to 3
+move 1 from 8 to 9
+move 1 from 1 to 5
+move 4 from 3 to 2
+move 10 from 5 to 3
+move 8 from 2 to 8
+move 7 from 8 to 3
+move 9 from 7 to 5
+move 1 from 9 to 3
+move 3 from 6 to 4
+move 3 from 7 to 6
+move 1 from 8 to 7
+move 1 from 1 to 8
+move 1 from 4 to 7
+move 5 from 7 to 6
+move 14 from 3 to 7
+move 16 from 3 to 9
+move 1 from 8 to 4
+move 2 from 4 to 9
+move 1 from 3 to 7
+move 1 from 6 to 8
+move 15 from 7 to 2
+move 10 from 9 to 7
+move 7 from 2 to 4
+move 1 from 2 to 7
+move 11 from 6 to 7
+move 5 from 5 to 9
+move 15 from 7 to 8
+move 1 from 7 to 2
+move 2 from 9 to 7
+move 4 from 5 to 1
+move 5 from 4 to 9
+move 6 from 2 to 4
+move 2 from 2 to 5
+move 2 from 1 to 4
+move 1 from 1 to 5
+move 3 from 5 to 6
+move 8 from 7 to 9
+move 9 from 4 to 9
+move 1 from 4 to 8
+move 11 from 9 to 7
+move 4 from 6 to 1
+move 17 from 8 to 7
+move 26 from 7 to 1
+move 1 from 4 to 8
+move 24 from 1 to 7
+move 22 from 9 to 3
+move 1 from 8 to 2
+move 6 from 3 to 4
+move 2 from 1 to 2
+move 1 from 7 to 9
+move 16 from 7 to 3
+move 1 from 9 to 5
+move 6 from 4 to 1
+move 1 from 2 to 7
+move 6 from 3 to 2
+move 1 from 5 to 4
+move 6 from 3 to 5
+move 1 from 4 to 1
+move 3 from 1 to 4
+move 4 from 5 to 4
+move 7 from 1 to 7
+move 6 from 4 to 3
+move 1 from 1 to 6
+move 1 from 2 to 5
+move 1 from 1 to 7
+move 15 from 3 to 1
+move 2 from 2 to 7
+move 3 from 5 to 8
+move 9 from 7 to 5
+move 8 from 5 to 7
+move 3 from 8 to 5
+move 1 from 6 to 9
+move 5 from 7 to 8
+move 3 from 2 to 4
+move 2 from 2 to 5
+move 4 from 3 to 7
+move 5 from 8 to 3
+move 1 from 5 to 8
+move 5 from 3 to 1
+move 2 from 5 to 7
+move 1 from 9 to 8
+move 1 from 5 to 8
+move 19 from 1 to 4
+move 19 from 7 to 1
+move 7 from 1 to 4
+move 1 from 7 to 4
+move 3 from 3 to 5
+move 22 from 4 to 5
+move 3 from 8 to 3
+move 7 from 1 to 8
+move 3 from 3 to 5
+move 3 from 3 to 6
+move 3 from 6 to 9
+move 3 from 9 to 1
+move 1 from 3 to 4
+move 2 from 8 to 9
+move 25 from 5 to 6
+move 4 from 1 to 5
+move 5 from 5 to 4
+move 2 from 8 to 2
+move 1 from 9 to 2
+move 3 from 5 to 7
+move 12 from 6 to 8
+move 1 from 7 to 3
+move 7 from 8 to 1
+move 1 from 5 to 7
+move 1 from 3 to 8
+move 2 from 7 to 4
+move 6 from 8 to 5
+move 10 from 6 to 3
+move 2 from 6 to 2
+move 1 from 6 to 3
+move 17 from 4 to 6
+move 3 from 3 to 9
+move 3 from 8 to 4
+move 1 from 7 to 5
+move 1 from 3 to 8
+move 1 from 2 to 5
+move 10 from 1 to 7
+move 3 from 2 to 7
+move 2 from 1 to 8
+move 15 from 6 to 3
+move 7 from 5 to 9
+move 9 from 9 to 5
+move 1 from 9 to 3
+move 2 from 3 to 5
+move 3 from 8 to 6
+move 1 from 9 to 3
+move 11 from 5 to 8
+move 9 from 3 to 8
+move 1 from 5 to 6
+move 9 from 8 to 5
+move 10 from 7 to 5
+move 5 from 5 to 3
+move 4 from 6 to 8
+move 2 from 6 to 8
+move 2 from 5 to 6
+move 1 from 2 to 1
+move 9 from 5 to 3
+move 2 from 7 to 5
+move 3 from 5 to 4
+move 1 from 4 to 1
+move 2 from 4 to 3
+move 1 from 7 to 1
+move 2 from 1 to 7
+move 3 from 4 to 5
+move 2 from 7 to 3
+move 14 from 3 to 9
+move 13 from 3 to 1
+move 8 from 1 to 4
+move 6 from 1 to 2
+move 11 from 8 to 6
+move 4 from 3 to 9
+move 2 from 9 to 2
+move 1 from 5 to 2
+move 6 from 4 to 9
+move 6 from 8 to 9
+move 6 from 9 to 4
+move 2 from 4 to 7
+move 4 from 4 to 6
+move 4 from 2 to 9
+move 2 from 7 to 9
+move 2 from 2 to 1
+move 3 from 5 to 3
+move 2 from 1 to 7
+move 1 from 5 to 2
+move 7 from 9 to 7
+move 2 from 2 to 8
+move 10 from 6 to 5
+move 5 from 5 to 6
+move 9 from 7 to 8
+move 3 from 3 to 9
+move 4 from 5 to 1
+move 10 from 9 to 3
+move 7 from 6 to 2
+move 5 from 3 to 9
+move 3 from 1 to 7
+move 1 from 4 to 7
+move 1 from 4 to 9
+move 1 from 3 to 7
+move 1 from 2 to 1
+move 1 from 5 to 1
+move 1 from 1 to 7
+move 3 from 6 to 3
+move 3 from 3 to 4
+move 6 from 7 to 4
+move 3 from 9 to 8
+move 9 from 8 to 1
+move 3 from 8 to 1
+move 13 from 9 to 5
+move 2 from 2 to 8
+move 4 from 8 to 3
+move 11 from 1 to 2
+move 14 from 2 to 6
+move 6 from 3 to 8
+move 4 from 9 to 7
+move 10 from 5 to 3
+move 2 from 7 to 3
+move 1 from 1 to 8
+move 1 from 1 to 7
+move 1 from 7 to 8
+move 1 from 1 to 4
+move 8 from 4 to 2
+move 2 from 5 to 1
+move 1 from 1 to 9
+move 1 from 7 to 3
+move 1 from 9 to 5
+move 1 from 4 to 2
+move 1 from 4 to 6
+move 1 from 7 to 3
+move 11 from 6 to 9
+move 4 from 2 to 5
+move 4 from 2 to 5
+move 10 from 5 to 6
+move 9 from 9 to 5
+move 1 from 9 to 2
+move 2 from 8 to 4
+move 1 from 9 to 6
+move 5 from 2 to 1
+move 5 from 8 to 6
+move 4 from 1 to 9
+move 1 from 8 to 1
+move 3 from 9 to 4
+move 5 from 5 to 1
+move 1 from 9 to 7
+move 11 from 6 to 3
+move 4 from 4 to 9
+move 9 from 6 to 5
+move 2 from 6 to 5
+move 3 from 9 to 1
+move 1 from 4 to 8
+move 4 from 1 to 3
+move 3 from 5 to 4
+move 2 from 4 to 9
+move 2 from 9 to 4
+move 1 from 9 to 8
+move 6 from 5 to 4
+move 1 from 7 to 8
+move 3 from 5 to 2
+move 3 from 8 to 5
+move 1 from 2 to 1
+move 24 from 3 to 9
+move 2 from 2 to 1
+move 10 from 1 to 7
+move 18 from 9 to 8
+move 5 from 3 to 7
+move 5 from 9 to 5
+move 12 from 7 to 2
+move 1 from 7 to 6
+move 8 from 4 to 7
+move 1 from 4 to 5
+move 12 from 5 to 9
+move 1 from 6 to 9
+move 3 from 2 to 8
+move 5 from 7 to 3
+move 21 from 8 to 7
+move 3 from 3 to 8
+move 11 from 9 to 5
+move 10 from 5 to 6
+move 3 from 7 to 2
+move 3 from 6 to 4
+move 2 from 3 to 1
+move 2 from 3 to 5
+move 1 from 1 to 7
+move 1 from 1 to 4
+move 3 from 4 to 1
+move 1 from 9 to 1
+move 1 from 4 to 3
+move 3 from 5 to 8
+move 1 from 9 to 6
+move 4 from 2 to 3
+move 6 from 8 to 6
+move 1 from 9 to 3
+move 7 from 2 to 4
+move 5 from 4 to 5
+move 1 from 2 to 6
+move 3 from 1 to 9
+move 3 from 9 to 4
+move 1 from 1 to 9
+move 2 from 5 to 3
+move 3 from 5 to 2
+move 4 from 7 to 2
+move 2 from 4 to 3
+move 2 from 2 to 3
+move 2 from 4 to 8
+move 5 from 2 to 3
+move 6 from 6 to 4
+move 8 from 7 to 3
+move 4 from 4 to 5
+move 1 from 3 to 1
+move 2 from 8 to 6
+move 7 from 7 to 5
+move 1 from 9 to 1
+move 14 from 3 to 6
+move 4 from 7 to 1
+move 6 from 5 to 3
+move 4 from 1 to 2
+move 9 from 3 to 5
+move 1 from 7 to 2
+move 2 from 3 to 7
+move 1 from 4 to 8
+move 1 from 4 to 9
+move 3 from 3 to 6
+move 9 from 5 to 2
+move 1 from 8 to 9
+move 1 from 1 to 7
+move 1 from 9 to 3
+move 1 from 4 to 8
+move 1 from 9 to 4
+move 3 from 5 to 1
+move 2 from 1 to 9
+move 1 from 4 to 9
+move 15 from 6 to 9
+move 3 from 3 to 5
+move 2 from 1 to 3
+move 2 from 7 to 4
+move 5 from 6 to 5
+move 6 from 2 to 9
+move 1 from 7 to 2
+move 2 from 4 to 6
+move 2 from 3 to 1
+move 1 from 1 to 6
+move 1 from 8 to 3
+move 1 from 3 to 9
+move 3 from 5 to 1
+move 3 from 6 to 2
+move 6 from 5 to 3
+move 6 from 6 to 8
+move 4 from 1 to 6
+move 12 from 9 to 7
+move 4 from 6 to 8
+move 1 from 5 to 1
+move 2 from 8 to 2
+move 2 from 2 to 1
+move 5 from 3 to 6
+move 3 from 1 to 6
+move 5 from 8 to 6
+move 1 from 3 to 6
+move 5 from 2 to 7
+move 8 from 9 to 4
+move 15 from 7 to 8
+move 5 from 6 to 3
+move 1 from 3 to 8
+move 15 from 8 to 3
+move 7 from 2 to 9
+move 1 from 7 to 4
+move 10 from 9 to 5
+move 4 from 6 to 4
+move 3 from 8 to 6
+move 1 from 8 to 6
+move 1 from 7 to 3
+move 10 from 6 to 9
+move 7 from 3 to 2
+move 10 from 9 to 7
+move 8 from 5 to 7
+move 8 from 3 to 7
+move 1 from 5 to 9
+move 1 from 6 to 8
+move 1 from 5 to 4
+move 1 from 8 to 6
+move 5 from 3 to 8
+move 9 from 4 to 2
+move 1 from 9 to 2
+move 4 from 2 to 3
+move 2 from 2 to 9
+move 2 from 4 to 8
+move 4 from 9 to 1
+move 1 from 4 to 9
+move 1 from 7 to 8
+move 9 from 2 to 1
+move 1 from 2 to 5
+move 1 from 5 to 3
+move 1 from 9 to 3
+move 4 from 3 to 6
+move 4 from 8 to 9
+move 2 from 3 to 6
+move 2 from 6 to 9
+move 1 from 4 to 8
+move 3 from 6 to 3
+move 2 from 6 to 5
+move 1 from 5 to 2
+move 2 from 2 to 1
+move 9 from 7 to 3
+move 7 from 3 to 9
+move 9 from 9 to 8
+move 10 from 7 to 1
+move 3 from 9 to 3
+move 3 from 3 to 1
+move 5 from 8 to 3
+move 1 from 9 to 3
+move 1 from 5 to 6
+move 3 from 8 to 4
+move 1 from 8 to 4
+move 2 from 8 to 2
+move 7 from 3 to 8
+move 4 from 4 to 2
+move 1 from 4 to 6
+move 1 from 8 to 1
+move 5 from 7 to 5
+move 2 from 6 to 7
+move 3 from 8 to 7
+move 2 from 2 to 1
+move 23 from 1 to 6
+move 2 from 3 to 5
+move 1 from 3 to 6
+move 1 from 7 to 2
+move 22 from 6 to 4
+move 5 from 2 to 7
+move 6 from 5 to 3
+move 17 from 4 to 1
+move 5 from 8 to 2
+move 23 from 1 to 7
+move 5 from 3 to 1
+move 15 from 7 to 2
+move 2 from 3 to 4
+move 1 from 8 to 4
+move 5 from 1 to 9
+move 6 from 7 to 1
+move 8 from 4 to 6
+move 4 from 9 to 5
+move 3 from 5 to 7
+move 1 from 9 to 1
+move 7 from 7 to 4
+move 7 from 1 to 5
+move 10 from 2 to 3
+move 4 from 2 to 4
+move 6 from 2 to 8
+move 7 from 6 to 7
+move 7 from 3 to 1
+move 3 from 6 to 2
+move 5 from 8 to 7
+move 7 from 5 to 7
+move 1 from 5 to 6
+move 1 from 6 to 2
+move 2 from 3 to 4
+move 1 from 3 to 7
+move 1 from 2 to 6
+move 3 from 7 to 6
+move 1 from 8 to 3
+move 4 from 4 to 2
+move 2 from 4 to 9
+move 2 from 1 to 7
+move 1 from 4 to 9
+move 1 from 3 to 5
+move 4 from 6 to 1
+move 3 from 4 to 5
+move 2 from 4 to 1
+move 8 from 7 to 1
+move 1 from 4 to 1
+move 6 from 2 to 3
+move 1 from 2 to 4
+move 4 from 3 to 2
+move 1 from 4 to 5
+move 3 from 2 to 5
+move 11 from 7 to 5
+move 2 from 9 to 1
+move 8 from 7 to 4
+move 2 from 3 to 5
+move 1 from 2 to 1
+move 8 from 4 to 1
+move 1 from 9 to 4
+move 7 from 5 to 4
+move 22 from 1 to 5
+move 5 from 4 to 2
+move 6 from 1 to 7
+move 4 from 2 to 7
+move 19 from 5 to 4
+move 1 from 7 to 6
+move 3 from 1 to 6
+move 3 from 7 to 9
+move 1 from 2 to 4
+move 20 from 4 to 6
+move 13 from 5 to 9
+move 2 from 1 to 3
+move 10 from 9 to 8
+move 3 from 9 to 4
+move 1 from 8 to 1
+move 1 from 1 to 8
+move 1 from 3 to 1
+move 2 from 9 to 2
diff --git a/day05.rkt b/day05.rkt
new file mode 100644
index 0000000..76d6350
--- /dev/null
+++ b/day05.rkt
@@ -0,0 +1,29 @@
+#lang racket
+
+(let-values ([(stacks steps) (splitf-at (file->lines "day05")
+                                        (lambda (s) (not (equal? s ""))))])
+
+  (let* ([state (for/vector ([x (in-range 1 35 4)])
+                   (dropf (for/list ([y (in-range 0 (sub1 (length stacks)))])
+                            (string-ref (list-ref stacks y) x))
+                          (lambda (c) (equal? c #\space))))]
+         [steps (for/list ([line (cdr steps)])
+                  (remove* '(#f) (map string->number (string-split line " "))))]
+         [solve (lambda (state f)
+                  (for ([step steps])
+                    (let* ([n (first step)]
+                           [from (sub1 (second step))]
+                           [to (sub1 (third step))]
+
+                           [taken (take (vector-ref state from) n)]
+                           [left (drop (vector-ref state from) n)]
+                           [placed (append (f taken) (vector-ref state to))])
+                      (vector-set! state to placed)
+                      (vector-set! state from left)))
+                  (list->string (for/list ([s state])
+                                  (first s))))])
+    (displayln (solve (vector-copy state) reverse))
+    (displayln (solve (vector-copy state) identity))
+    ))
+; MQTPGLLDN
+; LVZPSTTCZ
diff --git a/day05.zig b/day05.zig
new file mode 100644
index 0000000..3a80a39
--- /dev/null
+++ b/day05.zig
@@ -0,0 +1,60 @@
+const std = @import("std");
+const data = @embedFile("day05");
+
+const Stack = std.BoundedArray(u8, 64);
+
+pub fn main() !void {
+    var lines = std.mem.split(u8, data, "\n");
+
+    var stacks = [_]Stack{try Stack.init(0)} ** 9;
+
+    while (lines.next()) |line| {
+        if (line.len == 0)
+            break;
+
+        for (stacks) |*stack, j| {
+            var c = line[1 + (j * 4)];
+            if (c != ' ') {
+                try stack.append(c);
+            }
+        }
+    }
+
+    for (stacks) |*stack| {
+        std.mem.reverse(u8, stack.slice());
+    }
+
+    var stacks2 = stacks;
+
+    while (lines.next()) |line| {
+        if (line.len == 0)
+            break;
+
+        var parts = std.mem.tokenize(u8, line, " ");
+        _ = parts.next();
+        var n = try std.fmt.parseInt(usize, parts.next().?, 10);
+        _ = parts.next();
+        var from = try std.fmt.parseInt(usize, parts.next().?, 10) - 1;
+        _ = parts.next();
+        var to = try std.fmt.parseInt(usize, parts.next().?, 10) - 1;
+
+        var j: usize = 0;
+        while (j < n) : (j += 1) {
+            try stacks[to].append(stacks[from].pop());
+        }
+
+        var f = stacks2[from].slice();
+        try stacks2[to].appendSlice(f[f.len - n .. f.len]);
+        try stacks2[from].resize(f.len - n);
+    }
+
+    for (stacks) |*stack| {
+        std.debug.print("{c}", .{stack.pop()});
+    }
+    std.debug.print("\n", .{});
+
+    for (stacks2) |*stack| {
+        std.debug.print("{c}", .{stack.pop()});
+    }
+    std.debug.print("\n", .{});
+}