aboutsummaryrefslogtreecommitdiff
path: root/2022/10-cathode-ray-tube/second.zig
blob: 6d1315ae88af16f7dfb8609fdea45a0ec66fe2b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;
            }
        }
    }
}