aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2021-12-06 15:09:46 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2021-12-06 15:25:30 +0700
commit231cd753962156adb2ba42e198b4b13f84fadfb4 (patch)
tree0d31711532beade879b4312b363b3f12995a14b2
parent85bd7ec1bd9cdc7ec53692fce5cae3118b7357a0 (diff)
downloadcp-231cd753962156adb2ba42e198b4b13f84fadfb4.tar.gz
[aoc] Add day 6
-rw-r--r--aoc/2021/06/part-one.py9
-rw-r--r--aoc/2021/06/part-two.zig22
2 files changed, 31 insertions, 0 deletions
diff --git a/aoc/2021/06/part-one.py b/aoc/2021/06/part-one.py
new file mode 100644
index 0000000..f1ad2fb
--- /dev/null
+++ b/aoc/2021/06/part-one.py
@@ -0,0 +1,9 @@
+from collections import deque
+from sys import stdin
+
+timer = deque([0] * 9)
+for i in map(int, stdin.read().strip().split(',')): timer[i] += 1
+for i in range(80):
+ timer.rotate(-1)
+ timer[6] += timer[-1]
+print(sum(timer))
diff --git a/aoc/2021/06/part-two.zig b/aoc/2021/06/part-two.zig
new file mode 100644
index 0000000..72bf877
--- /dev/null
+++ b/aoc/2021/06/part-two.zig
@@ -0,0 +1,22 @@
+const parseUnsigned = std.fmt.parseUnsigned;
+const print = std.debug.print;
+const rotate = std.mem.rotate;
+const std = @import("std");
+const tokenize = std.mem.tokenize;
+
+const input = @embedFile("input");
+
+pub fn main() !void {
+ var timer = [_]usize{ 0 } ** 9;
+ defer print("{}\n", .{ @reduce(.Add, @as(@Vector(9, usize), timer)) });
+
+ var list = tokenize(input[0..input.len-1], ",");
+ while (list.next()) |i|
+ timer[try parseUnsigned(usize, i, 10)] += 1;
+
+ var i: usize = 0;
+ while (i < 256) : (i += 1) {
+ rotate(usize, timer[0..], 1);
+ timer[6] += timer[8];
+ }
+}