day 15, part 2

main
Inga 🏳‍🌈 1 year ago
parent 874d5e0e07
commit 1764caea64
  1. 70
      day15-hard/build.zig
  2. 1
      day15-hard/hard.in
  3. 1
      day15-hard/sample.in
  4. 111
      day15-hard/src/main.zig

@ -0,0 +1,70 @@
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "day15-hard",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
}

File diff suppressed because one or more lines are too long

@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7

@ -0,0 +1,111 @@
const std = @import("std");
const SEVEN_BYTES: u64 = (1 << 56) - 1;
fn solveLine(line: []const u8) u64 {
var boxes = std.mem.zeroes([256][1024]usize);
var i: usize = 0;
while (i < line.len) {
var lens_hash: u16 = 0;
var lens_name: u64 = 0;
while (i < line.len and line[i] != '-' and line[i] != '=') : (i += 1) {
lens_hash += line[i];
lens_hash *= 17;
lens_hash %= 256;
lens_name *= 256;
lens_name += line[i];
}
if (lens_name | SEVEN_BYTES != SEVEN_BYTES) {
unreachable;
}
var box = &boxes[lens_hash];
switch (line[i]) {
'-' => {
//std.debug.print("Going to remove {x} from box {d}\n", .{ lens_name, lens_hash });
for (box, 0..) |*slot, slot_index| {
_ = slot_index;
if (slot.* & SEVEN_BYTES == lens_name) {
//std.debug.print("Removed from {d}\n", .{slot_index});
slot.* = 0;
}
}
i += 1;
},
'=' => {
i += 1;
switch (line[i]) {
'0'...'9' => {
const focus = line[i] - '0';
const new_slot_value = lens_name | (@as(u64, focus) << 56);
//std.debug.print("Going to insert {x} into box {d}\n", .{ new_slot_value, lens_hash });
var slot_index: usize = 0;
while (slot_index < boxes.len) : (slot_index += 1) {
if (box[slot_index] & SEVEN_BYTES == lens_name) {
//std.debug.print("Inserted at {d}\n", .{slot_index});
box[slot_index] = new_slot_value;
break;
}
} else while (slot_index > 0) : (slot_index -= 1) {
if (box[slot_index - 1] != 0) {
//std.debug.print("Inserted at {d}\n", .{slot_index});
box[slot_index] = new_slot_value;
break;
}
} else {
//std.debug.print("Inserted at {d}\n", .{0});
box[0] = new_slot_value;
}
i += 1;
},
else => unreachable,
}
},
else => unreachable,
}
if (i < line.len and line[i] != ',') {
unreachable;
}
i += 1;
}
var result: u64 = 0;
for (boxes, 0..) |box, box_number| {
//std.debug.print("Computing result for box {d}\n", .{box_number});
var actual_slot_number: usize = 1;
for (box) |slot_value| {
if (slot_value != 0) {
const power = (box_number + 1) * actual_slot_number * (slot_value >> 56);
//std.debug.print("Found value in slot {d}: {x}; adding {d} to result\n", .{ actual_slot_number, slot_value, power });
result += power;
actual_slot_number += 1;
}
}
}
return result;
}
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const raw_in = std.io.getStdIn();
var buffered_reader = std.io.bufferedReader(raw_in.reader());
var reader = buffered_reader.reader();
var result: u64 = 0;
var line_buffer: [30000]u8 = undefined;
while (try reader.readUntilDelimiterOrEof(&line_buffer, '\n')) |line| {
result += solveLine(line);
}
try stdout.print("{d}\n", .{result});
}
Loading…
Cancel
Save