2022-10 in zig
This commit is contained in:
parent
6129705885
commit
31ba31c887
5 changed files with 370 additions and 0 deletions
37
2022/10-cathode-ray-tube/first.zig
Normal file
37
2022/10-cathode-ray-tube/first.zig
Normal file
|
@ -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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue