diff options
| author | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2023-12-08 19:39:29 +0200 |
|---|---|---|
| committer | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2025-10-28 23:20:45 +0200 |
| commit | b961ad7dcf9aa0e2d2b09730704877eed2da68e7 (patch) | |
| tree | 43ae0da1254bb4a6d54df4a70380c19731adb05b | |
| parent | improving day7 solution (diff) | |
| download | aoc23-b961ad7dcf9aa0e2d2b09730704877eed2da68e7.tar.gz aoc23-b961ad7dcf9aa0e2d2b09730704877eed2da68e7.zip | |
day8
| -rw-r--r-- | day08/example1.txt | 9 | ||||
| -rw-r--r-- | day08/example2.txt | 5 | ||||
| -rw-r--r-- | day08/example3.txt | 10 | ||||
| -rw-r--r-- | day08/solution.zig | 131 |
4 files changed, 155 insertions, 0 deletions
diff --git a/day08/example1.txt b/day08/example1.txt new file mode 100644 index 0000000..9029a1b --- /dev/null +++ b/day08/example1.txt | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | RL | ||
| 2 | |||
| 3 | AAA = (BBB, CCC) | ||
| 4 | BBB = (DDD, EEE) | ||
| 5 | CCC = (ZZZ, GGG) | ||
| 6 | DDD = (DDD, DDD) | ||
| 7 | EEE = (EEE, EEE) | ||
| 8 | GGG = (GGG, GGG) | ||
| 9 | ZZZ = (ZZZ, ZZZ) | ||
diff --git a/day08/example2.txt b/day08/example2.txt new file mode 100644 index 0000000..7d1b58d --- /dev/null +++ b/day08/example2.txt | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | LLR | ||
| 2 | |||
| 3 | AAA = (BBB, BBB) | ||
| 4 | BBB = (AAA, ZZZ) | ||
| 5 | ZZZ = (ZZZ, ZZZ) | ||
diff --git a/day08/example3.txt b/day08/example3.txt new file mode 100644 index 0000000..5b3fa58 --- /dev/null +++ b/day08/example3.txt | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | LR | ||
| 2 | |||
| 3 | 11A = (11B, XXX) | ||
| 4 | 11B = (XXX, 11Z) | ||
| 5 | 11Z = (11B, XXX) | ||
| 6 | 22A = (22B, XXX) | ||
| 7 | 22B = (22C, 22C) | ||
| 8 | 22C = (22Z, 22Z) | ||
| 9 | 22Z = (22B, 22B) | ||
| 10 | XXX = (XXX, XXX) | ||
diff --git a/day08/solution.zig b/day08/solution.zig new file mode 100644 index 0000000..75e6e73 --- /dev/null +++ b/day08/solution.zig | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const print = std.debug.print; | ||
| 3 | const assert = std.debug.assert; | ||
| 4 | const ArrayList = std.ArrayList; | ||
| 5 | const HashMap = std.HashMap; | ||
| 6 | const mem = std.mem; | ||
| 7 | |||
| 8 | const fin = mem.trim(u8, @embedFile("./input.txt"), &std.ascii.whitespace); | ||
| 9 | var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 10 | const allocator = gpa.allocator(); | ||
| 11 | |||
| 12 | const Rule = struct { | ||
| 13 | key: u32, | ||
| 14 | left: u32, | ||
| 15 | right: u32, | ||
| 16 | |||
| 17 | pub fn endsWith(key: u32, e: u8) bool { | ||
| 18 | return key & 0b1111_1111 == e; | ||
| 19 | } | ||
| 20 | |||
| 21 | pub fn hash(s: []const u8) u32 { | ||
| 22 | assert(s.len == 3); | ||
| 23 | return s[2] | (@as(u32, s[1]) << 8) | (@as(u32, s[0]) << 16); | ||
| 24 | } | ||
| 25 | |||
| 26 | pub fn init(text: []const u8) Rule { | ||
| 27 | var splitTokens = mem.splitAny(u8, text, " =(),"); | ||
| 28 | var nonEmpty = [3]u32{ undefined, undefined, undefined }; | ||
| 29 | var i: usize = 0; | ||
| 30 | |||
| 31 | while (splitTokens.next()) |tok| { | ||
| 32 | if (tok.len != 3) { | ||
| 33 | continue; | ||
| 34 | } | ||
| 35 | nonEmpty[i] = hash(tok); | ||
| 36 | i += 1; | ||
| 37 | } | ||
| 38 | |||
| 39 | assert(i == 3); | ||
| 40 | |||
| 41 | return Rule{ | ||
| 42 | .key = nonEmpty[0], | ||
| 43 | .left = nonEmpty[1], | ||
| 44 | .right = nonEmpty[2], | ||
| 45 | }; | ||
| 46 | } | ||
| 47 | }; | ||
| 48 | |||
| 49 | const RuleCtx = struct { | ||
| 50 | pub fn hash(_: @This(), K: u32) u64 { | ||
| 51 | return K; | ||
| 52 | } | ||
| 53 | |||
| 54 | pub fn eql(_: @This(), K1: u32, K2: u32) bool { | ||
| 55 | return K1 == K2; | ||
| 56 | } | ||
| 57 | }; | ||
| 58 | |||
| 59 | const RuleMap = HashMap(u32, Rule, RuleCtx, 80); | ||
| 60 | |||
| 61 | fn evaluate(instructions: []const u8, rules: RuleMap, start: u32, isPart1: bool) u64 { | ||
| 62 | const zzz = Rule.hash("ZZZ"); | ||
| 63 | var curr = start; | ||
| 64 | var steps: u64 = 0; | ||
| 65 | |||
| 66 | while (if (isPart1) (curr != zzz) else !Rule.endsWith(curr, 'Z')) : (steps += 1) { | ||
| 67 | const in = instructions[steps % instructions.len]; | ||
| 68 | |||
| 69 | curr = switch (in) { | ||
| 70 | 'L' => rules.get(curr).?.left, | ||
| 71 | 'R' => rules.get(curr).?.right, | ||
| 72 | else => unreachable, | ||
| 73 | }; | ||
| 74 | } | ||
| 75 | |||
| 76 | return steps; | ||
| 77 | } | ||
| 78 | |||
| 79 | pub fn part1(instructions: []const u8, rules: RuleMap) void { | ||
| 80 | const ans = evaluate(instructions, rules, Rule.hash("AAA"), true); | ||
| 81 | |||
| 82 | print("{d}\n", .{ans}); | ||
| 83 | } | ||
| 84 | |||
| 85 | fn gcd(a_: u64, b_: u64) u64 { | ||
| 86 | var a = a_; | ||
| 87 | var b = b_; | ||
| 88 | while (a != b) { | ||
| 89 | if (a > b) { | ||
| 90 | a -= b; | ||
| 91 | } else { | ||
| 92 | b -= a; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | return a; | ||
| 96 | } | ||
| 97 | |||
| 98 | fn lcm(a: u64, b: u64) u64 { | ||
| 99 | return a / gcd(a, b) * b; | ||
| 100 | } | ||
| 101 | |||
| 102 | pub fn part2(instructions: []const u8, rules: RuleMap) void { | ||
| 103 | var ans: u64 = 1; | ||
| 104 | var keyIt = rules.keyIterator(); | ||
| 105 | |||
| 106 | while (keyIt.next()) |k| { | ||
| 107 | if (Rule.endsWith(k.*, 'A')) { | ||
| 108 | const steps = evaluate(instructions, rules, k.*, false); | ||
| 109 | |||
| 110 | ans = lcm(ans, steps); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | print("{d}\n", .{ans}); | ||
| 115 | } | ||
| 116 | |||
| 117 | pub fn main() !void { | ||
| 118 | var splitLines = mem.splitScalar(u8, fin, '\n'); | ||
| 119 | var rules = RuleMap.init(allocator); | ||
| 120 | |||
| 121 | var instructions = splitLines.next().?; | ||
| 122 | _ = splitLines.next(); | ||
| 123 | |||
| 124 | while (splitLines.next()) |line| { | ||
| 125 | const R = Rule.init(line); | ||
| 126 | try rules.put(R.key, R); | ||
| 127 | } | ||
| 128 | |||
| 129 | part1(instructions, rules); | ||
| 130 | part2(instructions, rules); | ||
| 131 | } | ||
