1
0

Compare commits

..

3 Commits

Author SHA1 Message Date
fa7a9aab14
light washing, fix overflows
Some checks failed
Build and test / test (push) Failing after 1m43s
2023-12-13 13:52:57 +11:00
d80a39e7dc
day12 part2
jesus
2023-12-13 13:41:34 +11:00
3e8dca665f
day12 part1 2023-12-13 12:24:28 +11:00

View File

@ -14,21 +14,126 @@ const Solution = struct {
b: usize, b: usize,
}; };
fn solve(alloc: mem.Allocator, input: []const u8) !Solution { fn hash(alloc: mem.Allocator, pattern: []const u8, groups: []const u8, curr_group: u8) ![]u8 {
_ = alloc; var a = try std.ArrayList(u8).initCapacity(alloc, pattern.len + groups.len + 1);
try a.appendSlice(pattern);
try a.appendSlice(groups);
try a.append(curr_group);
return try a.toOwnedSlice();
}
var it = util.splitLines(input); var memo: std.StringHashMap(usize) = undefined;
while (it.next()) |line| {
if (line.len == 0) continue;
var lit = util.splitSpace(line); fn clone(comptime T: type, alloc: mem.Allocator, s: []const T) ![]T {
const record = lit.next().?; var a = try alloc.alloc(T, s.len);
_ = record; @memcpy(a, s);
const groups = lit.next().?; return a;
_ = groups; }
fn solvePattern(alloc: mem.Allocator, pattern: []const u8, groups: []const u8, curr_group: u8) !usize {
const h = try hash(alloc, pattern, groups, curr_group);
if (memo.get(h)) |m| {
alloc.free(h);
return m;
} }
return .{ .a = 0, .b = 0 }; if (pattern.len == 0) {
return if (groups.len == 0) 1 else 0;
}
const res: usize = b: {
switch (pattern[0]) {
'#' => {
// Too many #
if (groups.len == 0 or curr_group >= groups[0]) {
break :b 0;
}
// Special-case last #
if (pattern.len == 1 and groups.len == 1 and groups[0] == curr_group + 1) {
break :b 1;
}
break :b try solvePattern(alloc, pattern[1..], groups, curr_group + 1);
},
'.' => {
if (curr_group > 0) {
if (groups.len == 0 or curr_group != groups[0]) {
// Too many #
break :b 0;
} else {
// Group done
break :b try solvePattern(alloc, pattern[1..], groups[1..], 0);
}
} else {
// Not growing a group, skip.
break :b try solvePattern(alloc, pattern[1..], groups, 0);
}
},
'?' => {
const p1 = try clone(u8, alloc, pattern);
defer alloc.free(p1);
const p2 = try clone(u8, alloc, pattern);
defer alloc.free(p2);
p1[0] = '#';
p2[0] = '.';
break :b try solvePattern(alloc, p1, groups, curr_group) +
try solvePattern(alloc, p2, groups, curr_group);
},
else => unreachable,
}
};
try memo.put(h, res);
return res;
}
fn unfold(alloc: mem.Allocator, input: []const u8, sep: u8) ![]const u8 {
var buf = try std.ArrayList(u8).initCapacity(alloc, input.len * 5 + 5);
for (0..5) |i| {
if (i > 0 and sep > 0) try buf.append(sep);
try buf.appendSlice(input);
}
return try buf.toOwnedSlice();
}
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
memo = std.StringHashMap(usize).init(alloc);
defer memo.deinit();
var sum1: usize = 0;
var sum2: usize = 0;
var it = util.splitLines(input);
var i: usize = 1;
while (it.next()) |line| {
if (line.len == 0) continue;
std.debug.print("line {} ", .{i});
var lit = util.splitSpace(line);
const pattern = lit.next().?;
const raw_groups = lit.next().?;
const groups = try util.parseIntsScalar(u8, alloc, raw_groups, .{ .sep = ',' });
defer alloc.free(groups);
const unfolded_pattern = try unfold(alloc, pattern, '?');
const unfolded_groups = try unfold(alloc, groups, 0);
defer alloc.free(unfolded_pattern);
defer alloc.free(unfolded_groups);
sum1 += try solvePattern(alloc, pattern, groups, 0);
sum2 += try solvePattern(alloc, unfolded_pattern, unfolded_groups, 0);
std.debug.print("{} {}\n", .{ sum1, sum2 });
i += 1;
}
var mkeys = memo.keyIterator();
while (mkeys.next()) |k| {
alloc.free(k.*);
}
return .{ .a = sum1, .b = sum2 };
} }
test "silver" { test "silver" {
@ -46,8 +151,13 @@ test "silver" {
test "gold" { test "gold" {
const input = const input =
\\ \\???.### 1,1,3
\\.??..??...?##. 1,1,3
\\?#?#?#?#?#?#?#? 1,3,1,6
\\????.#...#... 4,1,1
\\????.######..#####. 1,6,5
\\?###???????? 3,2,1
; ;
const sln = try solve(std.testing.allocator, input); const sln = try solve(std.testing.allocator, input);
try std.testing.expectEqual(@as(usize, 0), sln.b); try std.testing.expectEqual(@as(usize, 525152), sln.b);
} }