2024 Day 19
This commit is contained in:
parent
ea22f10615
commit
f7551103c6
2 changed files with 96 additions and 2 deletions
|
|
@ -88,8 +88,23 @@ pub fn parseAll(
|
|||
|
||||
var it = std.mem.tokenizeSequence(u8, text, sep);
|
||||
while (it.next()) |s| {
|
||||
const eqn = try T.parse(allocator, s);
|
||||
try all.append(eqn);
|
||||
const v = try T.parse(allocator, s);
|
||||
try all.append(v);
|
||||
}
|
||||
|
||||
return all;
|
||||
}
|
||||
|
||||
pub fn splitAll(
|
||||
allocator: std.mem.Allocator,
|
||||
text: []const u8,
|
||||
sep: []const u8,
|
||||
) !std.ArrayList([]const u8) {
|
||||
var all = std.ArrayList([]const u8).init(allocator);
|
||||
|
||||
var it = std.mem.tokenizeSequence(u8, text, sep);
|
||||
while (it.next()) |s| {
|
||||
try all.append(s);
|
||||
}
|
||||
|
||||
return all;
|
||||
|
|
|
|||
79
2024/src/day19.zig
Normal file
79
2024/src/day19.zig
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
const std = @import("std");
|
||||
const aoc = @import("aoc.zig");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const List = std.ArrayList;
|
||||
const Map = std.AutoHashMap;
|
||||
const Set = aoc.AutoHashSet;
|
||||
const PriorityQueue = std.PriorityQueue;
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .{};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const text = try aoc.readAll(allocator, "input/day19.txt");
|
||||
defer allocator.free(text);
|
||||
|
||||
var blocks = std.mem.tokenizeSequence(u8, text, "\n\n");
|
||||
|
||||
const towels = try aoc.splitAll(allocator, blocks.next().?, ", ");
|
||||
defer towels.deinit();
|
||||
|
||||
const arrangements = try aoc.splitAll(allocator, blocks.next().?, "\n");
|
||||
defer arrangements.deinit();
|
||||
|
||||
if (blocks.next()) |_| unreachable;
|
||||
|
||||
var bw = std.io.bufferedWriter(std.io.getStdOut().writer());
|
||||
const stdout = bw.writer();
|
||||
|
||||
try stdout.print("{}\n{}\n", try options(allocator, towels.items, arrangements.items));
|
||||
try bw.flush();
|
||||
}
|
||||
|
||||
const String = []const u8;
|
||||
|
||||
fn options(
|
||||
allocator: Allocator,
|
||||
towels: []String,
|
||||
arrangements: []String,
|
||||
) !struct { u64, u64 } {
|
||||
var possible: u64 = 0;
|
||||
var total: u64 = 0;
|
||||
|
||||
var memo = std.StringHashMap(u64).init(allocator);
|
||||
defer memo.deinit();
|
||||
|
||||
for (arrangements) |desired| {
|
||||
const count = try arrange(&memo, towels, desired);
|
||||
|
||||
if (count > 0) {
|
||||
possible += 1;
|
||||
total += count;
|
||||
}
|
||||
}
|
||||
|
||||
return .{ possible, total };
|
||||
}
|
||||
|
||||
fn arrange(memo: *std.StringHashMap(u64), alphabet: []String, desired: String) !u64 {
|
||||
if (desired.len == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (memo.get(desired)) |n| {
|
||||
return n;
|
||||
}
|
||||
|
||||
var count: u64 = 0;
|
||||
for (alphabet) |atom| {
|
||||
if (std.mem.startsWith(u8, desired, atom)) {
|
||||
const suffix = desired[atom.len..desired.len];
|
||||
count += try arrange(memo, alphabet, suffix);
|
||||
}
|
||||
}
|
||||
|
||||
try memo.put(desired, count);
|
||||
return count;
|
||||
}
|
||||
Loading…
Reference in a new issue