Compare commits
No commits in common. "84344d12e51c5be8e1227fcff1f1598745b1fe0f" and "300ceb885ff804f5409dbe4d63c40f19b747829d" have entirely different histories.
84344d12e5
...
300ceb885f
104
src/day08.zig
104
src/day08.zig
@ -1,104 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
const util = @import("util.zig");
|
|
||||||
const mem = std.mem;
|
|
||||||
|
|
||||||
pub fn main() !void {
|
|
||||||
const input = @embedFile("data/day08.txt");
|
|
||||||
const sln = try solve(util.gpa, input);
|
|
||||||
std.debug.print("{d}\n", .{sln.a});
|
|
||||||
std.debug.print("{d}\n", .{sln.b});
|
|
||||||
}
|
|
||||||
|
|
||||||
const Solution = struct {
|
|
||||||
a: usize,
|
|
||||||
b: usize,
|
|
||||||
};
|
|
||||||
|
|
||||||
const Step = struct {
|
|
||||||
left: [3]u8,
|
|
||||||
right: [3]u8,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn gcd(a: usize, b: usize) usize {
|
|
||||||
return if (b == 0) a else gcd(b, a % b);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lcm(ns: []u32) usize {
|
|
||||||
var acc: usize = ns[0];
|
|
||||||
for (ns[1..]) |n| {
|
|
||||||
const nsize = @as(usize, n);
|
|
||||||
acc = acc / gcd(acc, nsize) * nsize;
|
|
||||||
}
|
|
||||||
return acc;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
|
|
||||||
var states = std.AutoHashMap([3]u8, Step).init(alloc);
|
|
||||||
defer states.deinit();
|
|
||||||
|
|
||||||
var it = util.splitLines(input);
|
|
||||||
const steps = it.next().?;
|
|
||||||
while (it.next()) |line| {
|
|
||||||
if (line.len == 0) continue;
|
|
||||||
try states.put(line[0..3].*, .{ .left = line[7..10].*, .right = line[12..15].* });
|
|
||||||
}
|
|
||||||
|
|
||||||
var a_nodes = std.AutoHashMap([3]u8, Step).init(alloc);
|
|
||||||
defer a_nodes.deinit();
|
|
||||||
var state_it = states.iterator();
|
|
||||||
while (state_it.next()) |entry| {
|
|
||||||
if (entry.key_ptr[2] == 'A') {
|
|
||||||
try a_nodes.put(entry.key_ptr.*, entry.value_ptr.*);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var all_steps = std.ArrayList(u32).init(alloc);
|
|
||||||
defer all_steps.deinit();
|
|
||||||
var a_it = a_nodes.iterator();
|
|
||||||
while (a_it.next()) |start| {
|
|
||||||
std.debug.print("{s}\n", .{start.key_ptr});
|
|
||||||
var position: [3]u8 = start.key_ptr.*;
|
|
||||||
var count: u32 = 0;
|
|
||||||
find: while (position[2] != 'Z') : (count += 1) {
|
|
||||||
const curr_state = states.get(position) orelse break :find; // ???????
|
|
||||||
position = if (steps[count % steps.len] == 'L') curr_state.left else curr_state.right;
|
|
||||||
}
|
|
||||||
try all_steps.append(count);
|
|
||||||
}
|
|
||||||
|
|
||||||
return .{ .a = 2, .b = lcm(all_steps.items) };
|
|
||||||
}
|
|
||||||
|
|
||||||
test "silver" {
|
|
||||||
const input =
|
|
||||||
\\RL
|
|
||||||
\\
|
|
||||||
\\AAA = (BBB, CCC)
|
|
||||||
\\BBB = (DDD, EEE)
|
|
||||||
\\CCC = (ZZZ, GGG)
|
|
||||||
\\DDD = (DDD, DDD)
|
|
||||||
\\EEE = (EEE, EEE)
|
|
||||||
\\GGG = (GGG, GGG)
|
|
||||||
\\ZZZ = (ZZZ, ZZZ)
|
|
||||||
;
|
|
||||||
_ = input;
|
|
||||||
// const sln = try solve(std.testing.allocator, input);
|
|
||||||
// try std.testing.expectEqual(@as(usize, 2), sln.a);
|
|
||||||
}
|
|
||||||
|
|
||||||
test "gold" {
|
|
||||||
const input =
|
|
||||||
\\LR
|
|
||||||
\\
|
|
||||||
\\11A = (11B, XXX)
|
|
||||||
\\11B = (XXX, 11Z)
|
|
||||||
\\11Z = (11B, XXX)
|
|
||||||
\\22A = (22B, XXX)
|
|
||||||
\\22B = (22C, 22C)
|
|
||||||
\\22C = (22Z, 22Z)
|
|
||||||
\\22Z = (22B, 22B)
|
|
||||||
\\XXX = (XXX, XXX)
|
|
||||||
;
|
|
||||||
const sln = try solve(std.testing.allocator, input);
|
|
||||||
try std.testing.expectEqual(@as(usize, 6), sln.b);
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user