const std = @import("std"); const aoc = @import("aoc"); const row_count = 128; const col_count = 8; // Input is coded in a way that we can do an implicit binary search by replacing // identifiers for the lower half with "0" and for the upper half with "1" // See commit for actual binary search fn code2Id(input: []const u8) !u32 { var value: u32 = 0; for (input) |c| value = value * 2 + @boolToInt(c == 'B' or c == 'R'); return value; } fn part1(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { _ = args; var lineit = std.mem.tokenize(u8, input, "\n"); var answer: u32 = 0; while (lineit.next()) |line| { const id = try code2Id(line[0..10]); answer = std.math.max(answer, id); } return try std.fmt.allocPrint(allocator, "{d}", .{answer}); } fn part2(allocator: std.mem.Allocator, input: []u8, args: [][]u8) !?[]u8 { _ = args; var lineit = std.mem.tokenize(u8, input, "\n"); var seats = [_]u1{0} ** (row_count * col_count); var min: u32 = std.math.inf_u32; while (lineit.next()) |line| { const id = try code2Id(line); min = std.math.min(min, id); seats[id] = 1; } for (seats[min..]) |_, i| { if (seats[min + i] == 0) { return try std.fmt.allocPrint(allocator, "{d}", .{min + i}); } } return null; } pub const main = aoc.main(part1, part2, .{ "850", "599" });