diff options
author | Christian Segundo | 2022-12-10 20:02:13 +0100 |
---|---|---|
committer | Christian Segundo | 2022-12-10 20:02:13 +0100 |
commit | 87ff884521326e4862096fc80baf63dc4fa73031 (patch) | |
tree | 30a555e9d71898350082ad644819176353833b43 | |
parent | cda8d041e5237cb7266c5706bb51f2bcc83953c2 (diff) | |
download | advent-of-zig-2022-87ff884521326e4862096fc80baf63dc4fa73031.tar.gz |
add day 10
-rw-r--r-- | day_10.zig | 73 | ||||
-rw-r--r-- | inputs/day_10 | 140 | ||||
-rw-r--r-- | main.zig | 1 |
3 files changed, 214 insertions, 0 deletions
diff --git a/day_10.zig b/day_10.zig new file mode 100644 index 0000000..b8cacc7 --- /dev/null +++ b/day_10.zig @@ -0,0 +1,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) }; +} diff --git a/inputs/day_10 b/inputs/day_10 new file mode 100644 index 0000000..5fab235 --- /dev/null +++ b/inputs/day_10 @@ -0,0 +1,140 @@ +noop +addx 25 +addx -5 +addx -14 +addx 4 +noop +addx 2 +addx 3 +noop +noop +noop +noop +addx 3 +addx 5 +addx 2 +noop +noop +addx 5 +noop +noop +noop +addx 1 +addx 2 +addx 5 +addx -40 +addx 5 +noop +addx 26 +addx -20 +addx -3 +addx 2 +noop +addx -4 +addx 9 +addx 5 +addx 2 +addx 11 +addx -10 +addx 2 +addx 5 +addx 2 +addx 5 +noop +noop +noop +addx -31 +addx 32 +addx -37 +addx 1 +addx 8 +addx 13 +addx -15 +addx 4 +noop +addx 5 +noop +addx 3 +addx -2 +addx 4 +addx 1 +addx 4 +addx -14 +addx 15 +addx 4 +noop +noop +noop +addx 3 +addx 5 +addx -40 +noop +addx 5 +addx 8 +addx -3 +noop +addx 2 +addx 9 +addx -4 +noop +noop +noop +noop +addx 5 +addx -9 +addx 10 +addx 4 +noop +noop +addx 5 +addx -19 +addx 24 +addx -2 +addx 5 +addx -40 +addx 22 +addx -19 +addx 2 +addx 5 +addx 2 +addx 5 +noop +noop +addx -2 +addx 2 +addx 5 +addx 3 +noop +addx 2 +addx 2 +addx 3 +addx -2 +addx 10 +addx -3 +addx 3 +noop +addx -40 +addx 2 +addx 11 +addx -5 +addx -1 +noop +addx 3 +addx 7 +noop +addx -2 +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx 6 +addx -5 +addx 2 +addx -18 +addx 26 +addx -1 +noop +noop +noop +noop @@ -15,6 +15,7 @@ const t = [_]struct { .{ .day = @import("day_07.zig"), .input = @embedFile("inputs/day_07"), .expect = &[_]Result{ .{ .int = 1427048 }, .{ .int = 2940614 } } }, .{ .day = @import("day_08.zig"), .input = @embedFile("inputs/day_08"), .expect = &[_]Result{ .{ .int = 1859 }, .{ .int = 332640 } } }, .{ .day = @import("day_09.zig"), .input = @embedFile("inputs/day_09"), .expect = &[_]Result{ .{ .int = 6087 }, .{ .int = 2493 } } }, + .{ .day = @import("day_10.zig"), .input = @embedFile("inputs/day_10"), .expect = &[_]Result{ .{ .int = 14620 }, .{ .int = 4210 } } }, }; pub fn main() !void { |