about summary refs log tree commit diff
path: root/aoc
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-08 16:25:38 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-08 16:25:38 +0900
commitf82f0b1a08f1facebc6c334c6d881d97e403e269 (patch)
treee42094b9621aa81713835e51fc4f4f3c6a84fa19 /aoc
parent38d8920c7d7c4edb9a1ca6b1e42baebe541f3c75 (diff)
downloadcp-f82f0b1a08f1facebc6c334c6d881d97e403e269.tar.gz
[aoc/2022] Finish day 6
Diffstat (limited to 'aoc')
-rw-r--r--aoc/2022/06/part-one.d24
-rw-r--r--aoc/2022/06/part-two.d26
2 files changed, 50 insertions, 0 deletions
diff --git a/aoc/2022/06/part-one.d b/aoc/2022/06/part-one.d
new file mode 100644
index 0000000..c914704
--- /dev/null
+++ b/aoc/2022/06/part-one.d
@@ -0,0 +1,24 @@
+import core.stdc.stdio : getchar, printf;
+
+extern(C) void main()
+{
+    slide: for (auto q = 0u, i = 1u; q & 0xffu ^ '\n'; ++i)
+    {
+        q <<= 8u;
+        q |= getchar();
+        if (i < 4)
+          continue;
+
+        auto p = cast(ubyte*) &q;
+        for (auto s = 0u, j = 0u; j < 4u; ++j)
+        {
+            auto b = 1u << (p[j] & 0x1fu);
+            if (s & b)
+                continue slide;
+            s |= b;
+        }
+
+        printf("%d\n", i);
+        break;
+    }
+}
diff --git a/aoc/2022/06/part-two.d b/aoc/2022/06/part-two.d
new file mode 100644
index 0000000..4465e38
--- /dev/null
+++ b/aoc/2022/06/part-two.d
@@ -0,0 +1,26 @@
+import core.int128 : Cent, and, or, shl, tst, xor;
+import core.stdc.stdio : getchar, printf;
+
+extern(C) void main()
+{
+    slide: for (auto q = cast(Cent) 0u, i = 1u;
+                tst(xor(and(q, cast(Cent) 0xff), cast(Cent) '\n'));
+                ++i)
+    {
+        q = or(shl(q, 8u), cast(Cent) getchar());
+        if (i < 14)
+          continue;
+
+        auto p = cast(ubyte*) &q;
+        for (auto s = 0u, j = 0u; j < 14u; ++j)
+        {
+            auto b = 1u << (p[j] & 0x1fu);
+            if (s & b)
+                continue slide;
+            s |= b;
+        }
+
+        printf("%d\n", i);
+        break;
+    }
+}