2022-10 in zig
This commit is contained in:
parent
6129705885
commit
31ba31c887
5 changed files with 370 additions and 0 deletions
146
2022/10-cathode-ray-tube/example
Normal file
146
2022/10-cathode-ray-tube/example
Normal file
|
@ -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
|
6
2022/10-cathode-ray-tube/example_output
Normal file
6
2022/10-cathode-ray-tube/example_output
Normal file
|
@ -0,0 +1,6 @@
|
|||
##..##..##..##..##..##..##..##..##..##..
|
||||
###...###...###...###...###...###...###.
|
||||
####....####....####....####....####....
|
||||
#####.....#####.....#####.....#####.....
|
||||
######......######......######......####
|
||||
#######.......#######.......#######.....
|
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;
|
||||
}
|
143
2022/10-cathode-ray-tube/input
Normal file
143
2022/10-cathode-ray-tube/input
Normal file
|
@ -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
|
38
2022/10-cathode-ray-tube/second.zig
Normal file
38
2022/10-cathode-ray-tube/second.zig
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue