Skip to content

Commit

Permalink
2024 day 19
Browse files Browse the repository at this point in the history
  • Loading branch information
agagniere committed Dec 26, 2024
1 parent 38b355e commit ba916d3
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 3 deletions.
42 changes: 42 additions & 0 deletions 2024/19/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseFast });

const source = b.path("second.zig");
const utils = b.dependency("utils", .{ .target = target, .optimize = optimize }).module("utils");

const exe = b.addExecutable(.{
.name = "day19",
.root_source_file = source,
.target = target,
.optimize = optimize,
});

exe.root_module.addImport("utils", utils);
b.installArtifact(exe);

{ // Run
const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe);

run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
run_step.dependOn(&run_cmd.step);
}
{ // Test
const test_step = b.step("test", "Run unit tests");
const unit_tests = b.addTest(.{
.root_source_file = source,
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);

unit_tests.root_module.addImport("utils", utils);
test_step.dependOn(&run_unit_tests.step);
}
}
11 changes: 11 additions & 0 deletions 2024/19/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.{
.name = "day19",
.version = "0.1.0",
.dependencies = .{
.utils = .{
.url = "../utils/",
.hash = "122059863bba3b73097c2905eacfa772e5c9ad7cd6616270b08dfdf41e3d81900420",
},
},
.paths = .{ "build.zig", "build.zig.zon", "second.zig" },
}
7 changes: 7 additions & 0 deletions 2024/19/first.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from utils import lines
import re

towels = input().split(', ')
expression = re.compile('(' + '|'.join(towels) + ')+')
input()
print(sum(1 for line in lines() if expression.fullmatch(line)))
117 changes: 117 additions & 0 deletions 2024/19/second.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const std = @import("std");
const utils = @import("utils");

const Allocator = std.mem.Allocator;
const Stack = std.ArrayListUnmanaged(*Trie);

const Color = enum(u3) {
white,
blue,
black,
red,
green,

pub fn from_char(c: u8) ?Color {
return switch (c) {
'w' => .white,
'u' => .blue,
'b' => .black,
'r' => .red,
'g' => .green,
else => null,
};
}
};

const Trie = struct {
is_leaf: bool,
children: [5]?*Trie,

const empty: Trie = .{ .is_leaf = false, .children = .{null} ** 5 };

pub fn insert(self: *Trie, allocator: Allocator, word: []const u8) !void {
if (word.len == 0) {
self.is_leaf = true;
} else {
const color = @intFromEnum(Color.from_char(word[0]).?);
if (self.children[color] == null) {
self.children[color] = try allocator.create(Trie);
self.children[color].?.* = Trie.empty;
}
try self.children[color].?.insert(allocator, word[1..]);
}
}
};

const Solver = struct {
root: *const Trie,
cache: std.StringHashMap(u64),

pub fn init(allocator: Allocator, towels: *const Trie) Solver {
return .{ .root = towels, .cache = std.StringHashMap(u64).init(allocator) };
}

pub fn count(self: *Solver, word: []const u8) Allocator.Error!u64 {
if (self.cache.get(word)) |res| {
return res;
}
const res = try self._count(self.root, word);
try self.cache.put(word, res);
return res;
}

fn _count(self: *Solver, current: *const Trie, word: []const u8) !u64 {
var result: u64 = 0;

if (word.len == 0) {
return if (current.is_leaf) 1 else 0;
}
const color = @intFromEnum(Color.from_char(word[0]).?);
if (current.is_leaf) {
result += try self.count(word);
}
if (current.children[color]) |child| {
result += try self._count(child, word[1..]);
}
return result;
}
};

fn parseFirstLine(allocator: Allocator, line: []const u8) !Trie {
var root: Trie = Trie.empty;
var towelIterator = std.mem.splitSequence(u8, line, ", ");

while (towelIterator.next()) |towel| {
try root.insert(allocator, towel);
}
return root;
}

pub fn main() !void {
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .{};
defer _ = gpa.deinit();

var arena = std.heap.ArenaAllocator.init(gpa.allocator());
defer arena.deinit();
var stdin = std.io.bufferedReader(std.io.getStdIn().reader());

var lines = utils.lineIteratorSize(3000, stdin.reader());
const towels = try parseFirstLine(arena.allocator(), lines.next().?);
var solver = Solver.init(gpa.allocator(), &towels);
defer solver.cache.deinit();

var part1: u32 = 0;
var part2: u64 = 0;

_ = lines.next();
while (lines.next()) |line| {
const ways = try solver.count(line);
std.log.debug("{s:<20} {}", .{ line, ways });
if (ways > 0)
part1 += 1;
part2 += ways;
solver.cache.clearRetainingCapacity();
}
std.debug.print("Number of possible patterns: {}\n", .{part1});
std.debug.print("Number of arrangements: {}\n", .{part2});
}
6 changes: 6 additions & 0 deletions 2024/19/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def lines():
while True:
try:
yield input()
except:
break
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ My solutions for the [Advent of Code](https://adventofcode.com), a challenge sta
| 16 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 17 |||||:star::star:|:star::star:|:star::star:|:star::star:|
| 18 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 19 |||||:star::star:||:star::star:|:star::star:|
| 19 |||||:star::star:||:star::star:|:star::star:|:star::star:
| 20 |||||:star:|:star::star:|:star::star:|:star:|
| 21 |||||:star::star:|:star::star:|:star::star:|:star:|
| 22 |||||:star::star:|:star:|:star::star:|:star::star:|
| 23 |||||||:star::star:|:star:|
| 24 |||||:star::star:|:star::star:|:star::star:|:star:|
| 25 |||||:star:|:star:|:star::star:||:star:
| Total | 10 | 14 | 4 | 12 | 46 | 44 | 50 | 44 | 31
| Total | 10 | 14 | 4 | 12 | 46 | 44 | 50 | 44 | 33

Total stars: 255
Total stars: 257

0 comments on commit ba916d3

Please sign in to comment.