aboutsummaryrefslogtreecommitdiff
path: root/2022
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2022/10-cathode-ray-tube/example146
-rw-r--r--2022/10-cathode-ray-tube/example_output6
-rw-r--r--2022/10-cathode-ray-tube/first.zig37
-rw-r--r--2022/10-cathode-ray-tube/input143
-rw-r--r--2022/10-cathode-ray-tube/second.zig38
5 files changed, 370 insertions, 0 deletions
diff --git a/2022/10-cathode-ray-tube/example b/2022/10-cathode-ray-tube/example
new file mode 100644
index 0000000..37ee8ee
--- /dev/null
+++ b/2022/10-cathode-ray-tube/example
@@ -0,0 +1,146 @@
+addx 15
+addx -11
+addx 6
+addx -3
+addx 5
+addx -1
+addx -8
+addx 13
+addx 4
+noop
+addx -1
+addx 5
+addx -1
+addx 5
+addx -1
+addx 5
+addx -1
+addx 5
+addx -1
+addx -35
+addx 1
+addx 24
+addx -19
+addx 1
+addx 16
+addx -11
+noop
+noop
+addx 21
+addx -15
+noop
+noop
+addx -3
+addx 9
+addx 1
+addx -3
+addx 8
+addx 1
+addx 5
+noop
+noop
+noop
+noop
+noop
+addx -36
+noop
+addx 1
+addx 7
+noop
+noop
+noop
+addx 2
+addx 6
+noop
+noop
+noop
+noop
+noop
+addx 1
+noop
+noop
+addx 7
+addx 1
+noop
+addx -13
+addx 13
+addx 7
+noop
+addx 1
+addx -33
+noop
+noop
+noop
+addx 2
+noop
+noop
+noop
+addx 8
+noop
+addx -1
+addx 2
+addx 1
+noop
+addx 17
+addx -9
+addx 1
+addx 1
+addx -3
+addx 11
+noop
+noop
+addx 1
+noop
+addx 1
+noop
+noop
+addx -13
+addx -19
+addx 1
+addx 3
+addx 26
+addx -30
+addx 12
+addx -1
+addx 3
+addx 1
+noop
+noop
+noop
+addx -9
+addx 18
+addx 1
+addx 2
+noop
+noop
+addx 9
+noop
+noop
+noop
+addx -1
+addx 2
+addx -37
+addx 1
+addx 3
+noop
+addx 15
+addx -21
+addx 22
+addx -6
+addx 1
+noop
+addx 2
+addx 1
+noop
+addx -10
+noop
+noop
+addx 20
+addx 1
+addx 2
+addx 2
+addx -6
+addx -11
+noop
+noop
+noop
diff --git a/2022/10-cathode-ray-tube/example_output b/2022/10-cathode-ray-tube/example_output
new file mode 100644
index 0000000..c973789
--- /dev/null
+++ b/2022/10-cathode-ray-tube/example_output
@@ -0,0 +1,6 @@
+##..##..##..##..##..##..##..##..##..##..
+###...###...###...###...###...###...###.
+####....####....####....####....####....
+#####.....#####.....#####.....#####.....
+######......######......######......####
+#######.......#######.......#######.....
diff --git a/2022/10-cathode-ray-tube/first.zig b/2022/10-cathode-ray-tube/first.zig
new file mode 100644
index 0000000..105903b
--- /dev/null
+++ b/2022/10-cathode-ray-tube/first.zig
@@ -0,0 +1,37 @@
+const std = @import("std");
+
+const example = @embedFile("example");
+const input = @embedFile("input");
+
+pub fn main() anyerror!void {
+ try std.testing.expectEqual(solve(example), 13140);
+ const result = solve(input);
+ try std.io.getStdOut().writer().print("{}\n", .{result});
+}
+
+fn solve(puzzle: []const u8) i64 {
+ var it = std.mem.tokenize(u8, puzzle, "\n");
+ var tot: i64 = 0;
+ var cycle: i64 = 1;
+ var x: i64 = 1;
+ var line: []const u8 = undefined;
+ var prev: ?i64 = null;
+ // process input
+ while (cycle <= 220) : (cycle += 1) {
+ if (@mod(cycle - 20, 40) == 0) {
+ tot += x * cycle;
+ }
+ if (prev) |p| { // cpu is busy adding
+ x += p;
+ prev = null;
+ } else { // read another instruction
+ line = it.next() orelse break;
+ if (line[0] == 'a') {
+ var elts = std.mem.split(u8, line, " ");
+ _ = elts.next() orelse unreachable;
+ prev = std.fmt.parseInt(i64, elts.next() orelse unreachable, 10) catch unreachable;
+ }
+ }
+ }
+ return tot;
+}
diff --git a/2022/10-cathode-ray-tube/input b/2022/10-cathode-ray-tube/input
new file mode 100644
index 0000000..1f07fb5
--- /dev/null
+++ b/2022/10-cathode-ray-tube/input
@@ -0,0 +1,143 @@
+addx 2
+addx 3
+noop
+noop
+addx 1
+addx 5
+addx -1
+addx 5
+addx 1
+noop
+noop
+addx 4
+noop
+noop
+addx 5
+addx -5
+addx 6
+addx 3
+addx 1
+addx 5
+addx 1
+noop
+addx -38
+addx 41
+addx -22
+addx -14
+addx 7
+noop
+noop
+addx 3
+addx -2
+addx 2
+noop
+addx 17
+addx -12
+addx 5
+addx 2
+addx -16
+addx 17
+addx 2
+addx 5
+addx 2
+addx -30
+noop
+addx -6
+addx 1
+noop
+addx 5
+noop
+noop
+noop
+addx 5
+addx -12
+addx 17
+noop
+noop
+noop
+noop
+addx 5
+addx 10
+addx -9
+addx 2
+addx 5
+addx 2
+addx -5
+addx 6
+addx 4
+noop
+noop
+addx -37
+noop
+noop
+addx 17
+addx -12
+addx 30
+addx -23
+addx 2
+noop
+addx 3
+addx -17
+addx 22
+noop
+noop
+noop
+addx 5
+noop
+addx -10
+addx 11
+addx 4
+noop
+addx 5
+addx -2
+noop
+addx -6
+addx -29
+addx 37
+addx -30
+addx 27
+addx -2
+addx -22
+noop
+addx 3
+addx 2
+noop
+addx 7
+addx -2
+addx 2
+addx 5
+addx -5
+addx 6
+addx 2
+addx 2
+addx 5
+addx -25
+noop
+addx -10
+noop
+addx 1
+noop
+addx 2
+noop
+noop
+noop
+noop
+addx 7
+addx 1
+addx 4
+addx 1
+noop
+addx 2
+noop
+addx 3
+addx 5
+addx -1
+noop
+addx 3
+addx 5
+addx 2
+addx 1
+noop
+noop
+noop
+noop
diff --git a/2022/10-cathode-ray-tube/second.zig b/2022/10-cathode-ray-tube/second.zig
new file mode 100644
index 0000000..6d1315a
--- /dev/null
+++ b/2022/10-cathode-ray-tube/second.zig
@@ -0,0 +1,38 @@
+const std = @import("std");
+
+const input = @embedFile("input");
+
+var result: [6 * 40]u8 = undefined;
+
+pub fn main() anyerror!void {
+ solve(input);
+ var y: usize = 0;
+ while (y < 6) : (y += 1) {
+ std.debug.print("{s}\n", .{result[y * 40 .. (y + 1) * 40]});
+ }
+}
+
+fn solve(puzzle: []const u8) void {
+ var it = std.mem.tokenize(u8, puzzle, "\n");
+ var cycle: usize = 0;
+ var x: i64 = 1;
+ var line: []const u8 = undefined;
+ var prev: ?i64 = null;
+ // process input
+ while (cycle < 40 * 6) : (cycle += 1) {
+ const pos = @mod(cycle, 40);
+ const draw = pos == x - 1 or pos == x or pos == x + 1;
+ result[cycle] = if (draw) '#' else '.';
+ if (prev) |p| { // cpu is busy adding
+ x += p;
+ prev = null;
+ } else { // read another instruction
+ line = it.next() orelse break;
+ if (line[0] == 'a') {
+ var elts = std.mem.split(u8, line, " ");
+ _ = elts.next() orelse unreachable;
+ prev = std.fmt.parseInt(i64, elts.next() orelse unreachable, 10) catch unreachable;
+ }
+ }
+ }
+}