1
0

day08 part1

This commit is contained in:
xeals 2023-12-08 16:46:26 +11:00
parent 300ceb885f
commit ca84923990
Signed by: xeals
SSH Key Fingerprint: SHA256:pRv+8swQDA+/LuZ7NHj9m006BbKexlNK62OUA01ZZBc

75
src/day08.zig Normal file
View File

@ -0,0 +1,75 @@
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 solve(alloc: mem.Allocator, input: []const u8) !Solution {
var states = std.AutoHashMap([3]u8, Step).init(alloc);
defer states.deinit();
var position: [3]u8 = "AAA".*;
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 count: u16 = 0;
while (!mem.eql(u8, &position, "ZZZ")) : (count += 1) {
const currState = states.get(position) orelse break; // ???????
position = if (steps[count % steps.len] == 'L') currState.left else currState.right;
}
return .{ .a = count, .b = 0 };
}
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)
;
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, 0), sln.b);
}