diff options
author | Julien Dessaux | 2022-12-10 14:23:31 +0100 |
---|---|---|
committer | Julien Dessaux | 2022-12-10 14:23:31 +0100 |
commit | 31ba31c887ff054de82e83000bf37a847377a62e (patch) | |
tree | 4c2cfab496ffe61ab677ea42cd5e9b385b6ff2ce /2022/10-cathode-ray-tube/second.zig | |
parent | 2022-09 in zig (diff) | |
download | advent-of-code-31ba31c887ff054de82e83000bf37a847377a62e.tar.gz advent-of-code-31ba31c887ff054de82e83000bf37a847377a62e.tar.bz2 advent-of-code-31ba31c887ff054de82e83000bf37a847377a62e.zip |
2022-10 in zig
Diffstat (limited to '2022/10-cathode-ray-tube/second.zig')
-rw-r--r-- | 2022/10-cathode-ray-tube/second.zig | 38 |
1 files changed, 38 insertions, 0 deletions
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; + } + } + } +} |