Solve day 10

This commit is contained in:
Tyler Hallada 2022-12-10 16:52:15 -05:00
parent 2f485e9dbc
commit 701aee2b89
5 changed files with 396 additions and 6 deletions

93
src/day10.zig Normal file
View File

@ -0,0 +1,93 @@
const std = @import("std");
pub const input = @embedFile("input/day10.txt");
const test_input1 = @embedFile("input/day10_test1.txt");
const test_input2 = @embedFile("input/day10_test2.txt");
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
const allocator = gpa.allocator();
fn signal_strength(cycle: usize, x: isize) isize {
if (cycle == 20 or cycle == 60 or cycle == 100 or cycle == 140 or cycle == 180 or cycle == 220) {
return @intCast(isize, cycle) * x;
} else {
return 0;
}
}
pub fn solve_part1(data: []const u8) !isize {
var lines = std.mem.tokenize(u8, data, "\n");
var x: isize = 1;
var cycle: usize = 0;
var sum: isize = 0;
while (lines.next()) |line| {
if (std.mem.eql(u8, line, "noop")) {
cycle += 1;
sum += signal_strength(cycle, x);
} else {
var delta = try std.fmt.parseInt(isize, line[5..], 10);
cycle += 1;
sum += signal_strength(cycle, x);
cycle += 1;
sum += signal_strength(cycle, x);
x += delta;
}
}
return sum;
}
fn emit_ray(cycle: usize, x: isize, crt: *[6][40]bool) void {
if (x >= @mod(@intCast(isize, cycle), 40) - 1 and x <= (cycle % 40) + 1) {
crt[cycle / 40][cycle % 40] = true;
}
}
fn print_screen(crt: [6][40]bool) [246]u8 {
var screen = [_]u8{'.'} ** (6 * 41);
var row: usize = 0;
while (row < 6) : (row += 1) {
var col: usize = 0;
while (col < 40) : (col += 1) {
screen[(row * 41) + col] = if (crt[row][col]) '#' else '.';
}
screen[(row * 41) + col] = '\n';
}
return screen;
}
pub fn solve_part2(data: []const u8) ![246]u8 {
var lines = std.mem.tokenize(u8, data, "\n");
var x: isize = 1;
var cycle: usize = 0;
var crt = [_][40]bool{[_]bool{false} ** 40} ** 6;
while (lines.next()) |line| {
emit_ray(cycle, x, &crt);
if (std.mem.eql(u8, line, "noop")) {
cycle += 1;
} else {
var delta = try std.fmt.parseInt(isize, line[5..], 10);
cycle += 1;
emit_ray(cycle, x, &crt);
cycle += 1;
x += delta;
emit_ray(cycle, x, &crt);
}
}
return print_screen(crt);
}
test "solves part1" {
try std.testing.expectEqual(solve_part1(test_input2), 13140);
}
test "solves part2" {
try std.testing.expectEqualStrings(&try solve_part2(test_input2),
\\##..##..##..##..##..##..##..##..##..##..
\\###...###...###...###...###...###...###.
\\####....####....####....####....####....
\\#####.....#####.....#####.....#####.....
\\######......######......######......####
\\#######.......#######.......#######.....
\\
);
}

145
src/input/day10.txt Executable file
View File

@ -0,0 +1,145 @@
addx 1
noop
noop
addx 4
addx 5
addx -2
addx 19
addx -12
addx 3
addx -2
addx 4
noop
noop
noop
addx 3
addx -8
addx 15
addx 1
noop
noop
addx 6
addx -1
noop
addx -38
noop
addx 10
addx -5
noop
addx 3
addx 2
addx 7
noop
noop
addx 3
noop
addx 2
addx 3
addx -2
addx 2
addx 7
noop
noop
addx 9
noop
addx -12
noop
addx 11
addx -38
noop
noop
noop
addx 5
addx 5
noop
noop
noop
addx 3
addx -12
addx 14
noop
addx 1
addx 3
addx 1
addx 5
addx 4
addx 1
noop
noop
noop
noop
noop
addx -9
addx 17
addx -39
addx 38
addx -8
addx -26
addx 3
addx 4
addx 16
noop
addx -11
addx 3
noop
addx 2
addx 3
addx -2
addx 2
noop
addx 13
addx -8
noop
addx 7
addx -5
addx 8
addx -40
addx 16
addx -9
noop
addx -7
addx 8
addx 2
addx 7
noop
noop
addx -15
addx 16
addx 2
addx 5
addx 2
addx -20
addx 12
addx 11
addx 8
addx -1
addx 3
noop
addx -39
addx 2
noop
addx 5
noop
noop
noop
addx 4
addx 1
noop
noop
addx 2
addx 5
addx 2
addx 1
addx 4
addx -1
addx 2
noop
addx 2
noop
addx 8
noop
noop
noop
addx -10
noop
noop

View File

@ -0,0 +1,3 @@
noop
addx 3
addx -5

146
src/input/day10_test2.txt Normal file
View 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

View File

@ -9,6 +9,7 @@ const day06 = @import("day06.zig");
const day07 = @import("day07.zig"); const day07 = @import("day07.zig");
const day08 = @import("day08.zig"); const day08 = @import("day08.zig");
const day09 = @import("day09.zig"); const day09 = @import("day09.zig");
const day10 = @import("day10.zig");
const utils = @import("utils.zig"); const utils = @import("utils.zig");
fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.time.Timer) !void { fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.time.Timer) !void {
@ -18,20 +19,20 @@ fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.ti
var part1 = try day.solve_part1(day.input); var part1 = try day.solve_part1(day.input);
const part1_end = timer.read(); const part1_end = timer.read();
const part1_time = utils.HumanTime.new(part1_end - part1_start); const part1_time = utils.HumanTime.new(part1_end - part1_start);
if (@TypeOf(part1) == []const u8) { if (@TypeOf(part1) == usize or @TypeOf(part1) == isize) {
try stdout.print("Part 1: {s} ({d:.2} {s})\n", .{ part1, part1_time.value, part1_time.unit.abbr() });
} else {
try stdout.print("Part 1: {} ({d:.2} {s})\n", .{ part1, part1_time.value, part1_time.unit.abbr() }); try stdout.print("Part 1: {} ({d:.2} {s})\n", .{ part1, part1_time.value, part1_time.unit.abbr() });
} else {
try stdout.print("Part 1: \n{s} ({d:.2} {s})\n", .{ part1, part1_time.value, part1_time.unit.abbr() });
} }
const part2_start = timer.read(); const part2_start = timer.read();
var part2 = try day.solve_part2(day.input); var part2 = try day.solve_part2(day.input);
const part2_end = timer.read(); const part2_end = timer.read();
const part2_time = utils.HumanTime.new(part2_end - part2_start); const part2_time = utils.HumanTime.new(part2_end - part2_start);
if (@TypeOf(part2) == []const u8) { if (@TypeOf(part2) == usize or @TypeOf(part2) == isize) {
try stdout.print("Part 2: {s} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() });
} else {
try stdout.print("Part 2: {} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() }); try stdout.print("Part 2: {} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() });
} else {
try stdout.print("Part 2: \n{s} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() });
} }
} }
@ -50,6 +51,7 @@ pub fn main() !void {
try solve_day(7, day07, &stdout, &timer); try solve_day(7, day07, &stdout, &timer);
try solve_day(8, day08, &stdout, &timer); try solve_day(8, day08, &stdout, &timer);
try solve_day(9, day09, &stdout, &timer); try solve_day(9, day09, &stdout, &timer);
try solve_day(10, day10, &stdout, &timer);
try bw.flush(); try bw.flush();
} }
@ -66,5 +68,6 @@ test {
_ = day07; _ = day07;
_ = day08; _ = day08;
_ = day09; _ = day09;
_ = day10;
_ = utils; _ = utils;
} }