diff options
Diffstat (limited to 'day4.zig')
-rw-r--r-- | day4.zig | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -2,6 +2,7 @@ const std = @import("std"); pub fn main() !void { try std.fmt.format(std.io.getStdOut().writer(), "day4 pt1: {}\n", .{try solve_pt1(std.heap.page_allocator, puzzle_input)}); + try std.fmt.format(std.io.getStdOut().writer(), "day4 pt2: {}\n", .{try solve_pt2(std.heap.page_allocator, puzzle_input)}); } fn solve_pt1(a: std.mem.Allocator, input: []const u8) !u32 { @@ -26,6 +27,44 @@ fn solve_pt1(a: std.mem.Allocator, input: []const u8) !u32 { return sum; } +fn solve_pt2(a: std.mem.Allocator, input: []const u8) !u32 { + const n_cards = std.mem.count(u8, input, "\n") + 1; + const card_counts = try a.alloc(u32, n_cards); + defer a.free(card_counts); + @memset(card_counts, 1); + + var lines = std.mem.splitScalar(u8, input, '\n'); + var lineix: usize = 0; + while (lines.next()) |line| { + defer lineix += 1; + var s1 = std.mem.splitScalar(u8, line, ':'); + const card_num_str = s1.first(); + _ = card_num_str; // I guess we'll use this next round :) + const card_str = s1.next() orelse return error.NoCard; + var s2 = std.mem.splitScalar(u8, card_str, '|'); + const winning_nos = try read_nums(a, s2.first()); + defer a.free(winning_nos); + const your_nos = try read_nums(a, s2.next() orelse return error.NoNos); + defer a.free(your_nos); + const inter = try intersection(a, winning_nos, your_nos); + defer a.free(inter); + if (inter.len > 0) { + // for however many copies we have of the current card/// + for (0..card_counts[lineix]) |_| { + // add more copies of _subsequent_ cards based on the score of this card + for (lineix + 1..(lineix + 1 + inter.len)) |nix| { + card_counts[nix] += 1; + } + } + } + } + var sum: u32 = 0; + for (card_counts) |cc| { + sum += cc; + } + return sum; +} + fn read_nums(a: std.mem.Allocator, input: []const u8) ![]const u32 { var res = std.ArrayList(u32).init(a); errdefer res.deinit(); @@ -58,6 +97,9 @@ fn intersection(a: std.mem.Allocator, first: []const u32, second: []const u32) ! test "pt1" { try std.testing.expectEqual(@as(u32, 13), try solve_pt1(std.testing.allocator, test_input)); } +test "pt2" { + try std.testing.expectEqual(@as(u32, 30), try solve_pt2(std.testing.allocator, test_input)); +} const test_input = \\Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 \\Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 |