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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
const std = @import("std");
const Result = @import("util/aoc.zig").Result;
pub fn puzzle_1(input: []const u8) Result {
var cycle: u16 = 1;
var register: i16 = 1;
var sum: u16 = 0;
var iter = std.mem.split(u8, input, "\n");
while (iter.next()) |line| {
if (cycle % 40 == 20) sum += cycle * @intCast(u16, register);
cycle += 1;
if (line[0] != 'a') continue;
const n = std.fmt.parseInt(i16, line[5..], 0) catch unreachable;
if (cycle % 40 == 20) sum += cycle * @intCast(u16, register);
cycle += 1;
register += n;
}
return .{ .int = sum };
}
fn crt(cycle: u16, register: i16, screen: *[6][40]bool) void {
const pixel_id = (cycle - 1) % 240;
const pixel_row = pixel_id / 40;
const pixel_col = pixel_id % 40;
if (pixel_col == register or pixel_col == (register - 1) or pixel_col == (register + 1)) {
screen.*[pixel_row][pixel_col] = true;
} else {
screen.*[pixel_row][pixel_col] = false;
}
}
pub fn puzzle_2(input: []const u8) Result {
var cycle: u16 = 1;
var register: i16 = 1;
var screen = [_][40]bool{[_]bool{false} ** 40} ** 6;
var iter = std.mem.split(u8, input, "\n");
while (iter.next()) |line| {
crt(cycle, register, &screen);
cycle += 1;
crt(cycle, register, &screen);
if (line[0] != 'a') continue;
const n = std.fmt.parseInt(i16, line[5..], 0) catch unreachable;
cycle += 1;
crt(cycle, register, &screen);
register += n;
}
//for (screen) |row| {
//for (row) |pixel| {
//if (pixel) {
//std.debug.print("@", .{});
//} else {
//std.debug.print(" ", .{});
//}
//}
//std.debug.print("\n", .{});
//}
var r: usize = 0;
for (screen) |row, x| {
for (row) |pixel, y| r += y * x * @boolToInt(pixel);
}
return .{ .int = @intCast(i32, r) };
}
|