1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
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 <f1b717029cf3262c1fa2760124af258924d668da> 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" });
|