aoc-2020-zig

git clone https://git.sinitax.com/sinitax/aoc-2020-zig
Log | Files | Refs | README | sfeed.txt

commit 8ef4f7d2a088937384a42d8b145d6822a874b3a7
parent 8438782e64c0eb5848531bf6d8bc3435e7fecb8e
Author: Louis Burda <quent.burda@gmail.com>
Date:   Sat, 19 Dec 2020 22:50:08 +0100

added day 19

Diffstat:
Asrc/day18/input | 373+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day18/input-test | 1+
Asrc/day18/input-test2 | 1+
Asrc/day18/main.zig | 173+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day18/part1 | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day18/part2 | 31+++++++++++++++++++++++++++++++
Asrc/day19/input | 573+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day19/input-test | 12++++++++++++
Asrc/day19/input-test2 | 9+++++++++
Asrc/day19/input-test3 | 47+++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day19/main.zig | 177+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day19/part1 | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day19/part2 | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/day19/trace | 38++++++++++++++++++++++++++++++++++++++
14 files changed, 1656 insertions(+), 0 deletions(-)

diff --git a/src/day18/input b/src/day18/input @@ -0,0 +1,373 @@ +3 + 8 +7 * (7 * 6 + 7 * 5 + (9 + 2 * 2 * 4)) + 7 * 3 * 5 +3 * 4 + 2 + 7 + (4 * (9 + 9 + 2 + 3 * 8) + 9 + (6 + 4) + 7) + 3 +(7 * 9 + 2 + (5 + 4)) + 5 + 9 +4 + 5 * 5 + 8 + (3 * (7 * 7 + 5) * 7) * 5 +2 + 8 * (5 + 4 * (8 * 3 * 6 * 7 * 8 * 8) + 8 * (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+7 * 2 * 6 + 9 + 5 * 6 +3 + 3 * 7 * ((3 * 3 + 2 + 7 * 5 + 2) + 7 * 6 * 2 * 5 * 9) + (4 * 5) + 7 +9 + ((5 + 5 * 7) + 7 * 2) + 7 + 6 +4 * 9 * 9 + ((9 + 3 + 7 * 4) * 4 + 2 + 9 + 8 + 6) * 6 +(6 * 2 * 5 + 4 + 5) + (8 + (8 * 5 + 7 * 7 + 6 * 2) * 6 + 6) + 4 * 8 +(5 + 6 * 2) * 5 * 6 * (5 * 5) + 9 +(7 + (9 * 4)) * ((4 + 3 + 5) + 7 * 5) +2 * 5 + ((9 * 6 * 3 * 6 + 4) * 9 * 4) * 2 * 3 * (8 + 6) +3 * 6 * 5 + 9 * (6 + 4 * 2 + 5 * 6) +((7 + 8) + 3 + 7 + (8 * 3 * 6 + 8) * 2) * ((3 * 5 * 9) + 5) * 8 + 8 +((3 * 4 + 5 + 2) + 6 * 6) * 9 +4 + 6 * (9 * 6 * 7 + (2 * 5 + 5 * 9 + 6)) * 8 +4 * (2 + 2 + 9) + 5 + 5 * ((5 + 3) + 4 * 7) +6 + 8 * 3 + 4 +2 + 8 + (8 + (7 + 5 * 3 * 8 * 7 + 2) * 6) + 3 diff --git a/src/day18/input-test b/src/day18/input-test @@ -0,0 +1 @@ +1 + 2 * 3 + 4 * 5 + 6 diff --git a/src/day18/input-test2 b/src/day18/input-test2 @@ -0,0 +1 @@ +2 * 3 + (4 * 5) diff --git a/src/day18/main.zig b/src/day18/main.zig @@ -0,0 +1,173 @@ +// Own twist on https://en.wikipedia.org/wiki/Shunting-yard_algorithm + +const std = @import("std"); +const aoc = @import("aoc"); + +const OpType = enum { ADD, MULT, COUNT }; +const Op = struct { + prio: u32, optype: OpType +}; +const Item = union(enum) { + num: i64, op: OpType +}; + +const op_names = [_][]const u8{ "ADD", "MULT" }; + +fn printStack(stack: []Item) void { + for (stack) |item| { + switch (item) { + .op => std.debug.print("{} ", .{op_names[@enumToInt(item.op)]}), + .num => std.debug.print("{} ", .{item.num}), + } + } + std.debug.print("\n", .{}); +} + +fn parseToken(line: []const u8, i: *usize, depth: *u32, op_prio: []const u32, mathstack: *std.ArrayList(Item), opstack: *std.ArrayList(Op)) !bool { + if (i.* > line.len) { + return false; + } else if (i.* == line.len) { + while (opstack.items.len > 0) + try mathstack.append(Item{ .op = opstack.pop().optype }); + } else { + const c = line[i.*]; + switch (c) { + '+', '*' => { + const optype = switch (c) { + '+' => OpType.ADD, + '*' => OpType.MULT, + else => unreachable, + }; + const prio = depth.* * @enumToInt(OpType.COUNT) + op_prio[@enumToInt(optype)]; + while (opstack.items.len > 0 and + opstack.items[opstack.items.len - 1].prio >= prio) + { + try mathstack.append(Item{ .op = opstack.pop().optype }); + } + try opstack.append(Op{ .optype = optype, .prio = prio }); + }, + '(' => depth.* += 1, + ')' => depth.* -= 1, + ' ' => {}, + else => { + // in this case we know that every number is one digit.. + // crappy zig parseInt requires span of exact int size + // before we even know the format >:((( + const val = try std.fmt.parseInt(i64, line[i.* .. i.* + 1], 10); + try mathstack.append(Item{ .num = val }); + }, + } + } + i.* += 1; + return true; +} + +fn reduceStack(mathstack: *std.ArrayList(Item), opstack: *std.ArrayList(Op), tmpstack: *std.ArrayList(Item)) !void { + while (mathstack.items.len > 0) { + while (mathstack.items[mathstack.items.len - 1] != .op and + tmpstack.items.len > 0) + { + try mathstack.append(tmpstack.pop()); + } + if (mathstack.items.len < 3 or mathstack.items[mathstack.items.len - 1] != .op) + break; + const op = mathstack.pop(); + + // get first arg + if (mathstack.items[mathstack.items.len - 1] != .num) { + try tmpstack.append(op); + continue; + } + const a = mathstack.pop(); + + // get second arg + if (mathstack.items[mathstack.items.len - 1] != .num) { + try tmpstack.append(op); + try tmpstack.append(a); + continue; + } + const b = mathstack.pop(); + + const result: i64 = switch (op.op) { + OpType.ADD => a.num + b.num, + OpType.MULT => a.num * b.num, + else => unreachable, + }; + try mathstack.append(Item{ .num = result }); + } +} + +fn printRPN(allocator: *std.mem.Allocator, input: []u8, prios: []const u32) !void { + var mathstack = std.ArrayList(Item).init(allocator); + defer mathstack.deinit(); + + var opstack = std.ArrayList(Op).init(allocator); + defer opstack.deinit(); + + var lineit = std.mem.tokenize(input, "\n"); + while (lineit.next()) |line| { + var depth: u32 = 0; + var i: usize = 0; + while (try parseToken(line, &i, &depth, prios, &mathstack, &opstack)) {} + } + + printStack(mathstack.items); +} + +fn partProto(allocator: *std.mem.Allocator, input: []u8, prios: []const u32) !void { + var answer: i64 = 0; + + if (aoc.debug) { + std.debug.print("Reverse Polish Notation:\n", .{}); + try printRPN(allocator, input, prios); + } + + var mathstack = std.ArrayList(Item).init(allocator); + defer mathstack.deinit(); + + var opstack = std.ArrayList(Op).init(allocator); + defer opstack.deinit(); + + var tmpstack = std.ArrayList(Item).init(allocator); + defer tmpstack.deinit(); + + var lineit = std.mem.tokenize(input, "\n"); + while (lineit.next()) |line| { + var depth: u32 = 0; + var i: usize = 0; + while (try parseToken(line, &i, &depth, prios, &mathstack, &opstack)) { + try reduceStack(&mathstack, &opstack, &tmpstack); + } + + if (mathstack.items.len != 1) return aoc.Error.InvalidInput; + const last = mathstack.items[mathstack.items.len - 1]; + if (last != .num) return aoc.Error.InvalidInput; + answer += last.num; + + try mathstack.resize(0); + try opstack.resize(0); + try tmpstack.resize(0); + } + + std.debug.print("{}\n", .{answer}); +} + +fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { + const prios = [_]u32{ 1, 1 }; + try partProto(allocator, input, prios[0..]); +} + +fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { + const prios = [_]u32{ 2, 1 }; + try partProto(allocator, input, prios[0..]); +} + +test "unions" { + const item1 = Item{ .op = OpType.MULT }; + const item2 = Item{ .num = 1000 }; + const expect = std.testing.expect; + expect(item1 == .op); + expect(item2 == .num); +} + +pub const main = aoc.gen_main(part1, part2); diff --git a/src/day18/part1 b/src/day18/part1 @@ -0,0 +1,50 @@ +--- Day 18: Operation Order --- + +As you look out the window and notice a heavily-forested continent slowly appear over the horizon, +you are interrupted by the child sitting next to you. They're curious if you could help them with +their math homework. + +Unfortunately, it seems like this "math" follows different rules than you remember. + +The homework (your puzzle input) consists of a series of expressions that consist of addition (+), +multiplication (*), and parentheses ((...)). Just like normal math, parentheses indicate that the +expression inside must be evaluated before it can be used by the surrounding expression. Addition +still finds the sum of the numbers on both sides of the operator, and multiplication still finds the +product. + +However, the rules of operator precedence have changed. Rather than evaluating +multiplication before addition, the operators have the same precedence, and are +evaluated left-to-right regardless of the order in which they appear. + +For example, the steps to evaluate the expression 1 + 2 * 3 + 4 * 5 + 6 are as follows: + +1 + 2 * 3 + 4 * 5 + 6 + 3 * 3 + 4 * 5 + 6 + 9 + 4 * 5 + 6 + 13 * 5 + 6 + 65 + 6 + 71 + +Parentheses can override this order; for example, here is what happens if parentheses are added to +form 1 + (2 * 3) + (4 * (5 + 6)): + +1 + (2 * 3) + (4 * (5 + 6)) +1 + 6 + (4 * (5 + 6)) + 7 + (4 * (5 + 6)) + 7 + (4 * 11 ) + 7 + 44 + 51 + +Here are a few more examples: + + + - 2 * 3 + (4 * 5) becomes 26. + - 5 + (8 * 3 + 9 + 3 * 4 * 3) becomes 437. + - 5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) becomes 12240. + - ((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 becomes 13632. + + +Before you can help with the homework, you need to understand it yourself. Evaluate the +expression on each line of the homework; what is the sum of the resulting values? + + diff --git a/src/day18/part2 b/src/day18/part2 @@ -0,0 +1,31 @@ +--- Part Two --- + +You manage to answer the child's questions and they finish part 1 of their homework, but get stuck +when they reach the next section: advanced math. + +Now, addition and multiplication have different precedence levels, but they're not the +ones you're familiar with. Instead, addition is evaluated before multiplication. + +For example, the steps to evaluate the expression 1 + 2 * 3 + 4 * 5 + 6 are now as follows: + +1 + 2 * 3 + 4 * 5 + 6 + 3 * 3 + 4 * 5 + 6 + 3 * 7 * 5 + 6 + 3 * 7 * 11 + 21 * 11 + 231 + +Here are the other examples from above: + + + - 1 + (2 * 3) + (4 * (5 + 6)) still becomes 51. + - 2 * 3 + (4 * 5) becomes 46. + - 5 + (8 * 3 + 9 + 3 * 4 * 3) becomes 1445. + - 5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) becomes 669060. + - ((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 becomes 23340. + + +What do you get if you add up the results of evaluating the homework problems using these +new rules? + + diff --git a/src/day19/input b/src/day19/input @@ -0,0 +1,573 @@ +29: 116 82 | 119 24 +45: 116 69 | 119 124 +24: 119 116 +79: 102 119 | 117 116 +52: 94 119 | 25 116 +116: "a" +60: 48 116 | 27 119 +39: 119 41 | 116 132 +14: 119 119 +80: 116 116 | 119 134 +46: 119 14 | 116 61 +129: 30 116 | 61 119 +133: 119 2 | 116 122 +68: 119 3 | 116 33 +105: 119 47 | 116 104 +107: 62 119 | 50 116 +76: 119 100 | 116 127 +125: 119 111 | 116 86 +74: 24 119 | 61 116 +128: 119 76 | 116 99 +59: 87 119 | 7 116 +88: 62 116 | 50 119 +103: 119 97 | 116 118 +17: 116 119 | 119 116 +32: 116 134 | 119 116 +56: 119 106 | 116 60 +71: 115 119 | 10 116 +4: 119 118 | 116 61 +132: 119 17 | 116 97 +12: 67 116 | 78 119 +70: 119 24 | 116 30 +83: 17 116 | 30 119 +90: 116 105 | 119 12 +84: 119 87 | 116 14 +134: 116 | 119 +119: "b" +49: 4 119 | 132 116 +50: 134 119 | 119 116 +99: 119 21 | 116 46 +48: 61 119 | 32 116 +54: 119 85 | 116 58 +122: 116 7 | 119 82 +53: 123 119 | 91 116 +75: 43 119 | 79 116 +66: 116 52 | 119 93 +13: 97 119 | 14 116 +23: 82 119 | 7 116 +95: 90 116 | 28 119 +98: 119 32 | 116 50 +118: 134 134 +2: 116 97 | 119 82 +62: 119 116 | 119 119 +89: 73 116 | 108 119 +96: 24 116 | 17 119 +131: 116 110 | 119 15 +123: 64 116 | 98 119 +78: 81 116 | 15 119 +0: 8 11 +87: 116 119 +18: 116 24 +41: 119 62 | 116 30 +28: 9 116 | 128 119 +33: 24 116 | 97 119 +58: 107 116 | 84 119 +20: 116 130 | 119 45 +109: 116 5 | 119 112 +55: 118 116 | 17 119 +11: 42 31 +30: 116 119 | 116 116 +104: 36 116 | 57 119 +67: 101 119 | 29 116 +106: 46 119 | 38 116 +10: 49 119 | 44 116 +121: 116 103 | 119 96 +111: 116 97 | 119 50 +92: 116 61 | 119 80 +35: 116 37 | 119 107 +26: 119 32 | 116 62 +64: 119 118 | 116 97 +86: 17 116 | 82 119 +130: 119 19 | 116 66 +25: 116 50 +110: 87 116 | 118 119 +61: 116 116 +1: 116 56 | 119 65 +40: 116 118 | 119 61 +22: 116 17 | 119 24 +63: 119 68 | 116 6 +94: 119 24 | 116 14 +100: 119 24 | 116 80 +3: 14 119 | 30 116 +81: 24 119 | 80 116 +126: 30 116 | 17 119 +7: 116 116 | 119 116 +44: 40 119 | 92 116 +16: 116 18 | 119 88 +102: 116 17 | 119 7 +51: 1 119 | 71 116 +5: 119 54 | 116 53 +124: 119 72 | 116 39 +108: 50 119 | 118 116 +65: 119 77 | 116 16 +114: 116 61 | 119 62 +15: 118 119 | 50 116 +19: 116 133 | 119 121 +21: 50 119 | 61 116 +101: 82 119 | 24 116 +31: 95 119 | 51 116 +115: 34 119 | 131 116 +117: 7 119 | 24 116 +27: 17 119 | 32 116 +6: 119 120 | 116 127 +85: 116 55 | 119 94 +93: 119 64 | 116 74 +69: 116 125 | 119 113 +47: 70 119 | 114 116 +42: 109 119 | 20 116 +72: 119 114 | 116 64 +82: 134 119 | 116 116 +9: 35 119 | 89 116 +112: 116 75 | 119 63 +36: 119 32 | 116 87 +57: 119 87 | 116 50 +38: 119 24 | 116 61 +77: 22 119 | 13 116 +91: 73 116 | 92 119 +8: 42 +113: 59 116 | 129 119 +97: 116 119 | 119 119 +73: 97 119 | 30 116 +37: 62 119 | 24 116 +120: 119 61 | 116 61 +34: 126 119 | 83 116 +127: 116 30 | 119 14 +43: 23 116 | 26 119 + +babaaaabbbaaaabbbaaaaaba +bababbaababaabbbabbaabaa +aabbbbaaaaaabbbbaababaab +bbaabbaabbaaaaabbbbbababbbaaabbababaaaababbbbaab +baaaabaabaabbbbbbbbbaaba +bbaabbaabbbbbbbbabbbabbbbbbabbbbaaabaabb +babbaaaaaabaaaaabaabbabbbaabaaaababbbaaa +aababbbbbbbbaaaabbabbaabaabbbaaaaababbaa +bbbbbbbbbbbbaaababbaabbbbbbbbaaa +bbbabbbbbbbabbaaaaaaabababaaaaab +baababbbabbbabbbbaaabbbb +baabbbbaaaabaabbabbbbbaa +bbbabaabbaaaabbabaababaa +aaaabaaaabababbbaabaabbbababaaaa +baababaabbaababbbbbbaabb +abababbababbababbbbabaaabbaababb +abbaababaaaabaaabaaaaaab +ababababababababbabaababbaabaabababbabbbbbabbabbbaabbaab +aabbababbaaaaabaabaaaabb +bbaaaaabaabbaabbaababaaa +aaabbaaaaababbabaababaaabbbbabbababbbbbb +bbbbbbbbabbababbabaaaaab +bbabbaabbbbababbbabbbabaaabaaaabbabbbbaabbbababb +aaabbaabbaaaabbabaabbbbaababababbabbbaabbaaaaaaaababbbaa +baaabbbabaaaabaabaababbbbbaaaaabbaaaabab +bbbbaaabbbbabbaaaabbabbaabbaaaabaaaaabaaaabaaaab +ababaaabababaaabbbaaaabbbaaabbbabbbbaaba +bbabbbbaaabaabbbbaaabbbabbaaaababbbbababbbaababb +aaabbabbabbbabbbaabbbaabbbabbbaaaaabaaba +baabbbbabaaaabaababbaaaabbbaababaaabaaaa +ababbbbbaabbbabbbabaaabaaabbabbaaababbbbaaabaabbaaaababa +aaaaabababbaababaaaaaabb +abbabbabaabbbbabbabaaaaaababaaababbababaabababbbabababaa +abaabbbbbbbbbbaababbabbbbababaababaaaaba +abaabaabbbaaaababaaaabbb +aabbaababbbaabbbabbaabaaabaabbabbbaabbba +baabbbaaabbbbbababbabbbaaaabaabb +aabbbbabbaabbabbababbababaabaaaaabbbaaab +aaababbbbabbbabbaabbbabbaabababbabbbaaaabaababaa +abbaaaaaabbaabababaabababaaaabab +bbabbbaabaabbabaaabababbbaaaabaaababbbbbababbbaababbbaba +babaabbaababbabaababbbababbaabbbaaaababbabbaabbabbaaaaaa +abaabaaababaaababbbbbabaabbaaaaabbbabbbaabbbaabaaabaabba +abaabaaabaaabbababaaaaab +aaaabbbaabbbabbbbaababababababbbabaabababaaaabbababbbaba +bbbabaaabbbbaaaaaaabbaaa +abaabbaaabbbabaabbbaaaab +aabababbabaabaabbbbbbbbbbbabbaba +baabbababaabbabbaabaabba +aabababbaaabbababaaabbabbabbabba +abbabbbabaaaaabababaaabbbbbaababbabbbbbb +baabbbbaabbaabaabaaabbbaabbabbbbabbbbabb +aabbbabbababbaaaaabbabbaaaabaaab +ababbaaabbbabbbaabaaaaaababbbbbb +bbbabaabababbbabbbbbababbbababbb +aabbbaabaabaaaaabbabbaaa +aabaabbbbbaabbaabbaabaab +bbabbbaaababbabaaaaababa +bbbabbaabbabbbaabbababba +bbbaaabaabababbbbbbbbbbaaaabbabbaababaaaaababaaaaababaaa +abababbbbbbabbbbbaabbbab +bbaaaaabaaaaabababbbbbba +bbabaaaabbbbbbbbaaabbabaaabbbbbbabbbabaa +aabbbbaaabaaabaaaababaaa +ababababaabbbabaaababaab +abbaaaaabababbaabbbaabbaaaabbbba +bbbbababaaaabbaabbbbbaba +ababbaabbbabbaabbbbbaaababbaaaabaabbabbaababbbbabaabaaab +aabbbbaaaaaabbbbbbbbbbab +aaaabaaaaaabbbbbbbbabbab +babaaabbabbababbbbbababb +abaabbbabbaaabbaaabbbbbb +baaabbbabbbbaaaabbababbb +abbaaaababbaabbbaaaaaaabaaaaaaabbbbbabababbbaaaa +bbbaabaaaabbbbbbabbbaaabbaaaaaaa +abbabaabbaaabaababaabaabbbbbaabb +aaababbbabbaabaaabababbabbabaaabbbaabbba +babaabbababaaabbabaabaaaabbbabaa +bbaaabbabaaaabbaaaaaaaabbabbaaaaababaabb +ababaabaababbbabababbaaaaaababba +baabbbbaaabbababbaaababbbabbabbbbbaabaaa +babbaaaaabbaaaababbbbbba +bbbabaabaababbbbabaabaababaabaabbaabbaabbbabababbbbbbbab +aaabbabaabbaabababaabaaabaaaabaabaaabaaabbaabbaaaaabaabbaabaabaa +ababbaabbabaababbbbbbbbabaaabaababaabbaa +bbaaaaabaabbbbbaababaabaaabbbaabbbabbabaaaaaabbbaaaabaab +aaaaabaaaabbbabbbaabaabb +aabbabbabababaababbbbabaaabbbbabbaabbbaababbbbba +baaabbbabbbbababbbbabaaaaabbbbaabaaabaabaababbba +abaaabbbbabbaaaabbabaaaabababaabbabaabaabaabaaaa +abaaabaaaaabaaabababbbbaaabbbbbb +abaabbaabaabbaabaababbab +abaaaaaaabbaaaaababbabbbbbababbaabbabbbb +bbbaaabaababaaabbaaababbabaabbbaaaabaaba +babbabaaaababbabbbbabbbbbabbbbbabbaaabaababbbabbabbbaaaaaabababaaaaaabab +aaabbabaabbaabbbbaaaabbb +babaabbabbabbaabbaabababaabbbabbbbbbbaaababbaaba +bbabbaabababaabaababbbabaaaaaaabbbbbbbaaabaababb +aaaabaaaabbbbababaaaabbabbbbbbbbaababababaabaabb +abaaabaaaabaaabbaabaabbbbaabbbab +aaaaabaaaaababbbababaaaa +aabbabbbaabbbbaaaabbbbaabbabbaabababbbbb +aaabbbbabaaaabbbbbababbb +abaaaaaabaababbbabaabbbaababbaabbbabbabaaaaabaab +aabaaaaabbabaaaabaaaabbaaabbbbababaaaaba +baaabaabaaabbabaabbaabaabbaaabaa +bbabbbbabbbabaaaaaaababa +aaaabaaababbabbbaaababab +baabbbbbababbaabbbbbbbbabaababbbbbbababaabbaaabb +baabbbbbaabbbabbabaabbbbabaaabba +bbabaaabababbababbbbbbbbbabaaaaababaabab +baaaabaaaaabaaabbbbbabbb +babbabbbbbbbbbbbababababababaaababbbabbaaaaabbababaaabbb +aaabbaabaabbbaaababbaaab +aabbbabbbaaabaaaabbbabaa +aabbbbaabbabaabaabaaaaab +bbbaabbbabbababaababbbababaaaaaaaaaaabbb +bababbaaababbaabbbbbbbbbaaaabbbbabaabbbaababababaababbabaabbbbbb +aabbbaaaababababaaaabaaabaabbaaa +ababbbbbbbbbbbbbabbbabbaababaabaabaaababbabababbbbbaaabbbbbbbaaaaabbbbab +abbaaaaabbabbaababbaabbbbabaaaabbabbbaba +babbabbbabbaabbababbbbba +abbaabbbaabbabbbbbaabbaabbbaaabbbbbaabab +ababaabbaaabbabbbbaaababbbaaabbabaaaaaab +bbbababaaaaaaaabababababbababbba +aaabbbbbbbbabaabbababbaababbababaabaabaa +babaaaabbbaabbbbababbbbb +bbbaabbbbabbabbbabbbbbabaaaabbaaaaaaaabaaabaaaab +bbaabbbbbaabaabaaaaabbaaaabbabbaaaabaaabbbaabbbbabbbbabbbbbbbbabbbbaaaab +baababbbabbabbabbababbba +ababbabababaabbbababbbabaababaab +baabababbbabaaabbaaabaaaaabbbabbabbbabbbbabaabaaaaaaabbaaaabaaaabbbaabaa +abaabbbbaaaabaaaabbabbbaabbbabab +aaaaabababbbabbbabbaaabaabbbababbbababbb +bababbaaaababbbbababaaababbbabab +bbbbbbbbbbbabbaaabaabaaaabbabababbbababb +abbaababbbbaaabaaabbbbbaaabbbaaabbabaabbabbbbbbb +abbaaaababbbbaaaabbabaabbaaaaaababbbabaa +aabbbaabababbaabbbaababaabbbbabb +babababaaabbbbbaabbababaabbbabbbbabbbaaabbbbaabbbbababba +ababbaabbbaaaaababbaaabb +aabbaabbaabbabbbbbbbaaabbbbbbbbaabaabbbaaaababbaabababaaaaabaabbababbabbaabbaaaa +ababbbabbbabaaababbbabab +bbaabbaaabbababbaabbbbaaabbbbaaaaaabaaabbabbaaabbbbbaababbbbaabaaaaabaab +baaaabaabbbaabbaaaaaaaba +baababbbbbbabbbabbbabbaabaaababbabbaaabaaabaaaab +aabaaaaaabbabbbabaaaabab +abbaaabaaabbaabababaababbbbbaababbbaaabb +aabbbabbababbbbabababababbbbbaba +aaabbabbbababaabbabaabbabababbaabbabbbbb +abbbbbabbaabbbabbababaaabbbbbababbbbaaaaaabaaabbbabaabbbbbbabaaaabbabbbabbabaaaa +aabbaababbaaaaabaaabbbba +aabaabbbabbbabbaabbabbaa +bbaaaaababababbbababbbababbbabbbabbbaaabbaaaaaaabaabaaab +babababaabbaabbabbbaabbbbaaabbbb +babaaaaababaabbbbabaababbbabbaabbbaabbaabbabbbaaaaaabbabbbbbaabb +aaababbbabbaabaaaaabbabaaaaaabbbbbbabbab +aabbababaabbbaaabbbabaabbbaababaaabaabbbababbabb +babbaaaabaaaabbabaabbbbaaabbaabaabababaa +abababbaaababbbbbaabbabaabbbbabb +bbbabaabababaababbbbbbaa +bbbbabababbbbaaabbaaaabaaaaabbbabbbbbabb +aabaabbbbbabaaaabbaaaabbababaabbaaababba +babaaaabbbbbbbaababaabbabbbabababbbbaaba +ababababaabbbbbabbaaabbb +babbabbbbbbabaaabbababbb +babbbabbababaaababaaaaab +abaabbbbabbaabababbbaaaa +bbbabaabbaaababbabbaababbaaaaabaaabbabaabbbbbaaa +aababbaaababaabbaaabbbabbababbaaaabaaaaabbababbaabbaaabbbabaabba +bbabaabaaabaaabbbabaabba +aabbbaabaababbbbbaababbababbbbbbbaaabbbb +aaabbababbaababaaabbbbaababbbbaaabbbaabb +babaabbbababbbabbbbbbbbbbbabbaabaaababba +bbabaaabbbaabaaaaababbba +baaababbabbaabbabaaabbabaabbabaa +bbabaaaaaaabbabababaaabaaabbaabbababbaababbabbabbababbabbbababba +baaabbbbaaaaabaabababababbaababbbbabbbababaaabbbabbaabbb +bbbabaaaaabababbaaabbaabbaabaaabaabbabaa +bbaaaabaaaabaabbaaabaabbabbbbbbabbababbaaabbaaabbaabbaab +babbaaaaaaabbaabbbbbabaa +bbaabbaaabbbbbabaaaababb +aabbbbbabbbababababbbabbbaaabbbabaabbaabaaababba +abaaaabaabbabaaaaaaabbabbbbaababbabaabbbbbbaaababbababab +abbbbaaaaaaabbaabaabaababaabaabaabbbabbaaabbabaabbaababb +bbbbbbaabbaababababaaabbbbabbaaa +babaaabbbaaaaabaabaabaaabbbbbbbabbbbbabb +aaabbaabbbabbaabbbbabbbbaababababbbbbaaa +bbbabaabababbbabbaaabbabbaabbabbabbaaababbbbbaabaaabbbbabbbaaabbbabbbaab +ababbbbabaabababbbabaaaabbbaabbabbababaabbababba +bbabaaaaaaabbabbbabbaabb +ababaababbbbbbbaaaabaaba +bbbabbbbaaaababbbbaaaaaa +bbbababaaabaababbbbabbbbaabababbbabbababbaaaaaaaaaabbbaabaaabbaa +abaabaabbbaaaababbaaabaa +babbabbbbabaababaaaaabaaaaaabaaaabbaaaabaabaabba +bababbbbaabbaababbabbaabbaababbaabbbaabbaaabbbaa +bbbbbbaabbaaaabbabaabaaaaababaab +bbaabaabbaaaaabbababaabbbbbabbaababbaaaababbaabbaabaabaabbabbaabbbababab +bbbabbbabbaabbaabaaabbaa +bbbabbbabaaaabaaabbbbbbb +aabbbabababbaaaaabababbbabbbaaab +baaababbabbbabbbbbaabbbbbbbaabab +aaaabbaaababbaabbbababaa +bbaaaabbbaaababbabbbbabb +babaaabaababababaabbabbaaaabaabbabaaaaab +baaabaabababaabaaaabaaabaabaabaa +abbababbbbbbababbaababbabbaabbbaabbabbbb +aaaaaaabaabaababbbabbbbababaaaaabbbaabab +baabbbbbabaabababbaababaababbbabababaabbabbbbbba +aabbabbaabbaaaaaabbbaaaa +aaaabbbbbaabababbabbabbbababbbbb +babaababbbaabbbbabbaaababaabababbaaabbbbababaabb +babbababbaaaaabaaabaaabbababbaab +bbaabbaaaabbbbabaaabbbbbbaaababa +abbbabbabaabaababaabababbbbbbbaaababbabbbbabbaaa +aabaaaaaaabbaabbaabbaaab +aabaaaaabaaaaabbaaaabbab +ababaabaabbbbaaabbbbaabb +abbaabbbbbbbaaabbaabbbbbbbaabbaaabaabaaaabaaabaabbbbbabbbbababbb +babaabbabaabaabaabbaabbabaaabbaa +bbaabababababbbbbbaabbab +aabbbbaababaaabaaaaaabba +aaabbbbbabaababaabaabaababbabaabbaaaaaaa +bbaababaaaabaaabaabbabababababbbabbaaaabaaaaaaaaaabaabaaaabbaaabbaaabbaa +aabbbbbaaabaaabbbbbaabaa +abbbaabbaaabbabaabbbababaaaaabbaabaabaabbbbaababababaabaaabaabbabbaabbbbbbbbbbabbabbaaaa +baaabaaaabbaabbaabababbbaabaaabbabbaabbaabbababaaaababab +aaaaaaabbbabbbaaababbbabaaabbbaa +baaaaabaabaabababbbaabbb +aababbbbbbaaaabbaabbbbabaaaabbbabbbbabbb +baaaabaabbbabaaaaabaaaba +bbbaaabaababaaababbbabaa +bbaaaabbbbbaaabaaabbbaaaaaabbabbbbbbbaab +baaaabaabaabbabaaaabbbbbaaaabaaababbaaaabababbbabaabbbabbababbab +ababbbaabbabaabaabaabbbbababbaababbababbabbaaabbabbaaaaaabaabbaaabbaaaaaaabaabbb +ababaaabbaabbabaabaabbbbabaaabaaaababbab +baaabaabbabbaaaabbbbbbaabbbbaabb +bbabaaaaaaabbbbbabbabbbaabbaaababbababba +bbbbaaaaabbaababbabbbaaa +bbbbbbbbaababbbbaababaab +aaaaabaabbabbbaabbabaaaaababbababbaaaababaabaabaaaaabbab +babaaaaababbbabbbbaabbaaabbbaaab +bbaaaababbbaabbabaaabbbb +baabbabbbbaabbbbaabbbbbb +aabbaababbaabbbbbaaababbaabaaaba +aaaabbbaababbaabaaabbbaa +bbbaabbbbabbababbaabbabaaabaabbabbabbaaa +abaababaabaaabaaaabaabababaaaaaaababaaabbabababb +bababababaaaaabbaaaaababbbbabbbabababaabbabbbaab +abbaabbbaabaababababbbbb +babaaaabababbababbaaaababaaaaababbbbbbbabbbaaabbaaabaaaa +aabbaabbbabaaaaabaabaaab +aabbbaabbabbabbbbaaababa +baaaaabbabbaababbaabbabbaaabbbaaabaabbab +ababaababbaabbaaabbbbabaaaaabbaaaababbabbbabbaba +aaaaabaaaabbbabbaabbaaaa +abbaaabababaabbaabaaaaab +abaaabaaaabbbababbbbbaaa +bbbbbbaabababbaabbbbbbaababbbaab +bababaabbaababbbbaabbababbbaaabababbaaaaaaabbbab +bbbbaabaaaabababbaabbbab +abaabbbbaabaaaaaabbaaabababbbbbbaaaaaaaa +abbaabbaaaaabbbbbabaaabababbbbba +aabbaabbababababbaaababbabbabbabaabaaaba +bbaaaabbababbbabaabbbbbababbbbaaaaabaaaa +abababbaababbbbaaababbbbababaaaabbbaaaab +abbabbababbaaaababbaabbbbabbbbbb +abbbbbababaabbbabbaaaaabaaaaabbb +abbaabbbbaaabbabbabbabbbabbabbbaaabbababbbbbabaabaababaa +aabbbaaabaabababbababbbbabbbbbaa +bbbaabbbaabbbaaabbaaaaabbaabaabaaaababbbabbabbbaaabbaaaa +baaaabbabaabbabbaaaabbbaaaaaabaabbaaaaabbabbbbba +aabbabababababbababababaababbbbababbaaba +ababbbbaabbabbbabaaaabab +aababaaaaaaabbabbabbaaba +bbaaaaabbabaabbaaaaabbaa +aabaaaaabaaabbbaaaabbabababbbbbb +abbababbaabbbaaababbababaaaaababbbaabbbbbabaaaaaabaaaabb +baababbaabbaaaababbabaaa +abaaaaaababbbabbaabbbbaabbbabbaaabbabbbaabbbbbabaaabaaaabbaabaabaaabbaaabbbaababbaababaa +bbbabbbbabbbabbabaabbababbaabaaaaabaaaab +abbaabbbbbabbbbabaaabbaa +bbbaabbaabbabaabaaabaaba +babbabababababbabababaaa +abababbbbbbabbbbababbbaa +aaababbbabbaaaaaaababaab +aaaabbaabaaaabaabbbababababaaabaabbabaabababbabb +ababbbabbaaabbabbbaaaaaa +abbaaabaaababbbaaabababbbbbbbabb +bbbbbbbaababababababbabaabaabbaa +abababbbbaababbbaabbbabbbbabbaba +baabababaabbbaabbabaaabbbaabaaaaabbbbbbb +aabbbbaababaaababaaababa +bbabbbaababaaabbabbbbbaa +ababbbababbbbaaabbbaabaa +baaaabaaabababbbaaababba +bbbabbbababaaaabaababaaa +babbabbbaaabaaabbbabaabaaaaaabbb +ababaaababbaabaaaabbbbbaabbbbaaaaaabaabaaaaabbab +bbbabaaababbaaaababbababaaaaabbabbbbbaba +babaabbabaabbabaaababbbbababababbaabbbaaaaabaaba +baabbbbaaabaababaaaabaaaaaaabaab +abbbabbaababbaaaaabbaabababbbabbaababbabbbbabbaaabbaaaabbbbbbabb +bbaabbbbaaababbbbaaabaaabaaabbbaabbbbaabbaabbabbabababababbbbbaababbabbbaaaababb +abbaabbabbbabbbaaabbabbaaaabaaabaabaaaba +bbbabaabaabaabbbbabbaaba +abbababbbbabaabababaabaa +aabbbabbbbabaaabbabababbbbbaabaaabaababbbbbabbaa +babaabbaababbaaaaaaabababbbbabaa +abbabaabbbbbbbbbbaabbbbbabababbaaababbba +ababaababbaabbaabbaaaaabbaaaabaaabbbbaabaaabaabb +bbabbbaabbaabbaabaabbbbabaabbaaa +abaabbbbaabbaabbbbbabaaaabbaaabababbbaba +bbaaaabbbabbbbabbbbbabbababbbbbaabbbabab +ababbbbabbabaabababbbbab +abababbaaaabbabbaaaabbbb +abaabbbaabbaaaaabababaabbaaaaabababbbbaabaaaaaab +abbaaaaaababbaabbbbbbaaa +bbbabaabbabaabbbababababbaabbaababababaa +abbabbabbaabbbbaabbaabbabbbbbbababbbbaab +bbbbbbbababababaaabaabaa +bbabbbbabaabbababbababaa +abababaabbababbbbbabbabaabbbaaabaaaabbbabaaaaaabababaabbabbabaabaaabbabb +bbaaaabbbababbbbaaabaaabbaabbbaabbbaabbabbbaaaaababbbbabbabbaabb +aabbbbabaaaabbbabaababab +babaabbabbabaabaaaaaaaabbbaababb +aabbbaabbabaaabbaaabaaabbabbaabb +aabaaabbbbabaaaabbbabbaaababbbabbbbbbbbabaabaaaa +ababaabaabbaabaaabbababbababaabbabaaaabaaabaabaaaaabbbaa +babaaaaaaabbbaabbabaabbbbbaaaabbababaaaabbbaabab +babababaaaaaababbbabbaabababaaabbabbbbba +abbbbaaaababbaaaababbbbb +abababbaaaaaaaabbaaabaabaaababab +aabbbabbbbbaabbbaaababab +aabbbbabbbaabbbbaababbbbbbbaabaa +bbaabbaababaaaaabbaaaaaa +bababbaaabaabbaabbbbbabbbbabaabaababaaaabbbabbbabbabbbbaaaaaabbabaabbabaabaaaaabbbbabbbb +bbbaabbabbabbbbaaaababba +bababbaaaaabaaabbbbbbabb +ababbaabbaaaabbaabbaabbbbaaaabbb +babaabbababaaaaababbaabb +aaabbbbbbbabbbaabbbbbaaa +abaabaaaabbaababbaabaabb +aabbabbbbabaaabbabbabbbabbabbbbabbbabaabaababaab +abaabaabbababaababbbbbaa +aababbbbbbaaaaaaabbaaaaaaabababaababaaaaaababbababbbbabababaabab +abbabbababbbbaaabaaabaabaaabbbba +aababbbbbbbabaabaabbaaaa +aabbbaabbaabbabbbaaababaaabababaabbaaaababaaababaabbbabbbbbbaababbaababb +aabbaabbbaabbababbabaabaabbbabbabaaaaaaa +babaaabbaabbbbaaaabbbbabaaababbaabaaabba +babaaababbaababaabaaabbb +bbabaaaaababbaaaaababbbbbaaaaaab +aaaabbbbbbaaabbababbaaba +abbaabbaaabbbaabbbbaabab +abaabbbaaabbbaaabbababababbbbbbb +bbabbaababbaabbabbabbbbb +abbaabababababbaaabbaaab +aaaaaaabaabababbabbaababbabbabbbbabababbbbbbbabbaaaaaaaa +abaabbbbbbbaabbaababbabb +baabaababbabaababaabaaaabbabbabbabbbbaab +bababbaaaaaabbbaaaabbabbaababbaa +babaaaaaaabaaaaaaababababababbab +bbaababaaabbbaababbaabbbaabbaababaaaaabaabaababb +aabaabbbbaabbababbaabbbbabaaabaabbabbbaaabbbaabb +abaabbbbbaaabbbababbbabbbbaaaabbbaabaabbbbaabaaa +aabaaaaaaabababbbbbbbaba +abbaababababbaabbbbaabbbababaaaababbbaab +bbaaabbababaabbbaababaaa +abbbabbbaabbabbabababababbaaabaa +baabbbbabbabbbaabbaaaaababaaaabb +abaabbbaaabbabababbbbbabbbbabaaabbbabbbaaababbaa +aabaababbabaaabbbbabbbaaaabbbbbbaababaab +abbaaabbaababbabbbbaaaabbbbabbab +aaabbabaaabbbaaaabaaabaaaaabbbbbaaaabbbbaabbaaaa +babbbabbabaabaabaabaaaaaababbabababaabbbaaababbaababbbbb +aaaabbaabbbabbbaabbbaabb +aabababbabbabbbabbbbaaabbbbbaabb +aabaaabbabababbbaabbbaaaaababbbbaaaaabaaaaabaabb +aabbababababbaaabaabbaaabbbaaaaababbbaabbababaaaabaabbaabbbabaab +bbbabaabaabbbaaaababababbbaabaaa +bbbbbbaaaaabbabaabbbabaa +aaaaabbbababbbaabaaaabbbabaaababaaababbbaaaabaaa +aabbbabbabbbbabaaaabbbbbababbbababbbbababbbbbaba +ababaaabbbabaaababababbbabbabbaa +abbaabbababaaaabbabaaaaabbabbbbaaaabaaaaabaaabba +abbaababaabaaabbbbbbabaa +aaabaaabaaabaaabaaaabaaabbbbaaaaaabbababbbbaabaabbbbaaba +ababaabaabaababaaaabbaababbbaaba +aabbbbabbbaaaabaabaabaabbbaabbab +aabbbbaababaabbababbbaba +aaabbaabababbbabbbbbaaabbbaabaababaabbaa +babababaababbbabbaababbabbaaabaaabaaaaab +aaaabbbaaabbaabbaaaaabba +bbaaabaababaaaaabbbaabbbbabaaabbaaabbbbaaabbbabbbbbbabbb +abbbabbbbababbaabbaabababbbbbaba +babbaaaaabababbbbaabababbbbaabab +baaabaaaababaaabaababbbbababababbabaaabaabaaaabaaaabaaaa +baaabbababbbbbabbabaabbabaaababbbbbbbbbbabbabbbbaaaababa +ababbaabaaabbabbabbabbbb +aabbababaaaababbbbabbbbb +babaaababbabaaaababaababbbaaabbababaaabaabbbbabb +bbaaabbbabaaaabbbaaaaaab +aabaaaaabbaaaabbabbaaababbaabbbbbaabbbaaaaaaabba +baabababaabababbaaaaaaaa +abbbaabaaabbaabbbaabbbbabaaabbbbbaaaaabaaababbbaabbababb +aaabbabbbababaabaabbaabaabbabbbaaabbbaaabbabaaaaaababbbaaababbabbaabbaaabbabaabb +ababaaabbbabaaaabaabbbaababaabbbbbaaaabbbabaabbbabbbbbbabbaabbabaaababbabbbbaabb +bbbbbbbbbaabaabababbbaaa +bbaabbaaabbabbbababbabaa +abbaabaabbbbabbabbbaabababbbabaaabbbbabbbbbabaabbbabbbbabaaaabbbbabbababaababbba +aaabaaaaabababbaabaaababaababbabababbbaaaabbaaaabbabaabaaababbaababbabbabbaaaaba +aabababbabaabbbbbbbbaabb +babaabbbbaabbababaaabaaaaabaaabbaababbaa +aaabbbbbaaaabaaaababbbbabaabaaaaaaaabaab +aabbaabbaabababababbbbba +abbbabbabaabbabbbaaaabab +bbabaaaabbbababababaabababbaababbaaaababbabababbabaabbab +babaabbbaabaabbbabaabbbaaaababbbbabbaaababbbaabaabaaaaab +aabbaabaabbaabaaaabbbbaabbaabaaa +aaabbaababbaaaaaabbbbababaaaabbbabaabbab +bbaabbbbabaababaababbaabbaaabbbaaaaabaaaabbabbbb +baaaabaabbabbbbaaaabbabbbababbbbbbababab +ababbaababbaabaaabaabaababaaaabbaaababab +bbbbaaaaaabbbbabbaabaaab +aabbbabababbabbbaababaaa +baaababbbaababbaabaaabaaaabbabbabbababbb +aabaababbabaaaabbabaaabaaaaaaabababbbbaa +abbabbabaabaaaaaaaaabbbbbaabaabaaabbaabbabaaabbaaabaaaababaaabbaabaaabbb +baababbabaabaababaaaaaab +baaaabaaaaaababbabbabbbabbabaabb +bbaaabbaabaabaaaabbababaaabbbbabbabbbbaa +abababbaababbbbababababababababaabaaabba +aabaabababbaabbabbababbb +aabbbbabbabaababaaababab +bbabbbbbbaabbbbaabbbbabbabaaaababaabbbbb diff --git a/src/day19/input-test b/src/day19/input-test @@ -0,0 +1,12 @@ +0: 4 1 5 +1: 2 3 | 3 2 +2: 4 4 | 5 5 +3: 4 5 | 5 4 +4: "a" +5: "b" + +ababbb +bababa +abbbab +aaabbb +aaaabbb diff --git a/src/day19/input-test2 b/src/day19/input-test2 @@ -0,0 +1,9 @@ +0: 4 1 | 5 0 1 +1: "c" +4: "a" +5: "b" + +ac +acb +aaabbb +b diff --git a/src/day19/input-test3 b/src/day19/input-test3 @@ -0,0 +1,47 @@ +42: 9 14 | 10 1 +9: 14 27 | 1 26 +10: 23 14 | 28 1 +1: "a" +11: 42 31 +5: 1 14 | 15 1 +19: 14 1 | 14 14 +12: 24 14 | 19 1 +16: 15 1 | 14 14 +31: 14 17 | 1 13 +6: 14 14 | 1 14 +2: 1 24 | 14 4 +0: 8 11 +13: 14 3 | 1 12 +15: 1 | 14 +17: 14 2 | 1 7 +23: 25 1 | 22 14 +28: 16 1 +4: 1 1 +20: 14 14 | 1 15 +3: 5 14 | 16 1 +27: 1 6 | 14 18 +14: "b" +21: 14 1 | 1 14 +25: 1 1 | 1 14 +22: 14 14 +8: 42 +26: 14 22 | 1 20 +18: 15 15 +7: 14 5 | 1 21 +24: 14 1 + +abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa +bbabbbbaabaabba +babbbbaabbbbbabbbbbbaabaaabaaa +aaabbbbbbaaaabaababaabababbabaaabbababababaaa +bbbbbbbaaaabbbbaaabbabaaa +bbbababbbbaaaaaaaabbababaaababaabab +ababaaaaaabaaab +ababaaaaabbbaba +baabbaaaabbaaaababbaababb +abbbbabbbbaaaababbbbbbaaaababb +aaaaabbaabaaaaababaa +aaaabbaaaabbaaa +aaaabbaabbaaaaaaabbbabbbaaabbaabaaa +babaaabbbaaabaababbaabababaaab +aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba diff --git a/src/day19/main.zig b/src/day19/main.zig @@ -0,0 +1,177 @@ +const std = @import("std"); +const aoc = @import("aoc"); + +const Rule = union(enum) { + c: u8, + combos: []const []const usize, +}; + +fn printSlice(slice: []const usize) void { + for (slice) |v, i| { + if (i > 0) { + std.debug.print(" ", .{}); + } + std.debug.print("{}", .{v}); + } +} + +var stacktrace: std.ArrayList(usize) = undefined; + +fn checkRule(rules: *std.AutoHashMap(usize, Rule), rid: usize, msg: []const u8, nextstack: *std.ArrayList(usize)) anyerror!bool { + if (msg.len == 0) return true; + const rule = rules.get(rid) orelse return aoc.Error.InvalidInput; + + if (rule == .c) { + if (msg[0] != rule.c) return false; + if (msg.len == 1) return (nextstack.items.len == 0); + if (nextstack.items.len == 0) return false; + + const next = nextstack.pop(); + + const valid = try checkRule(rules, next, msg[1..], nextstack); + if (valid) try stacktrace.append(next); + if (valid) return true; + + try nextstack.append(next); + } else { + for (rule.combos) |ruleseq| { + try nextstack.appendSlice(ruleseq[1..]); + std.mem.reverse(usize, nextstack.items[nextstack.items.len - (ruleseq.len - 1) ..]); + + const valid = try checkRule(rules, ruleseq[0], msg, nextstack); + if (valid) try stacktrace.append(ruleseq[0]); + if (valid) return true; + + try nextstack.resize(nextstack.items.len - (ruleseq.len - 1)); + } + } + return false; +} + +fn freeRules(allocator: *std.mem.Allocator, rules: *std.AutoHashMap(usize, Rule), ignore: ?[]const usize) void { + var mapit = rules.iterator(); + while (mapit.next()) |kv| { + if (ignore != null and std.mem.indexOfScalar(usize, ignore.?, kv.key) != null) continue; + if (kv.value == .combos) { + for (kv.value.combos) |rs| { + allocator.free(rs); + } + allocator.free(kv.value.combos); + } + } + rules.deinit(); +} + +fn parseRule(allocator: *std.mem.Allocator, rules: *std.AutoHashMap(usize, Rule), line: []const u8) !void { + const indexsep = try aoc.assertV(std.mem.indexOf(u8, line, ": ")); + const index = try std.fmt.parseInt(usize, line[0..indexsep], 10); + + var chrsep = std.mem.indexOf(u8, line, "\""); + if (chrsep != null) { + if (chrsep.? == line.len - 1) return aoc.Error.InvalidInput; + try rules.put(index, Rule{ .c = line[chrsep.? + 1] }); + } else { + var vals = std.ArrayList([]usize).init(allocator); + errdefer vals.deinit(); + + var ruleit = std.mem.split(line[indexsep + 2 ..], " | "); + while (ruleit.next()) |r| { + var ruleids = std.ArrayList(usize).init(allocator); + errdefer ruleids.deinit(); + + var spaceit = std.mem.tokenize(r, " "); + while (spaceit.next()) |word| { + try ruleids.append(try std.fmt.parseInt(usize, word, 10)); + } + + if (ruleids.items.len == 0) return aoc.Error.InvalidInput; + + try vals.append(ruleids.items); + } + + try rules.put(index, Rule{ .combos = vals.items }); + } +} + +fn part1(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { + var rules = std.AutoHashMap(usize, Rule).init(allocator); + defer freeRules(allocator, &rules, null); + + var partit = std.mem.split(input, "\n\n"); + + var lineit = std.mem.tokenize(try aoc.assertV(partit.next()), "\n"); + while (lineit.next()) |line| { + try parseRule(allocator, &rules, line); + } + + var answer: u32 = 0; + var nextstack = std.ArrayList(usize).init(allocator); + defer nextstack.deinit(); + + stacktrace = std.ArrayList(usize).init(allocator); + defer stacktrace.deinit(); + + lineit = std.mem.tokenize(try aoc.assertV(partit.next()), "\n"); + var count: usize = 0; + while (lineit.next()) |line| { + count += 1; + if (try checkRule(&rules, 0, line, &nextstack)) + answer += 1; + try nextstack.resize(0); + } + + std.debug.print("{} / {} matching\n", .{ answer, count }); +} + +fn part2(allocator: *std.mem.Allocator, input: []u8, args: [][]u8) !void { + var rules = std.AutoHashMap(usize, Rule).init(allocator); + defer freeRules(allocator, &rules, ([_]usize{ 8, 11 })[0..]); + + var partit = std.mem.split(input, "\n\n"); + + var lineit = std.mem.tokenize(try aoc.assertV(partit.next()), "\n"); + while (lineit.next()) |line| { + if (std.mem.eql(u8, line[0..2], "8:")) { + const p1: []const usize = ([_]usize{42})[0..]; + const p2: []const usize = ([_]usize{ 42, 8 })[0..]; + try rules.put(8, Rule{ .combos = ([_][]const usize{ p1, p2 })[0..] }); + } else if (std.mem.eql(u8, line[0..3], "11:")) { + const p1: []const usize = ([_]usize{ 42, 31 })[0..]; + const p2: []const usize = ([_]usize{ 42, 11, 31 })[0..]; + try rules.put(11, Rule{ .combos = ([_][]const usize{ p1, p2 })[0..] }); + } else { + try parseRule(allocator, &rules, line); + } + } + + var answer: u32 = 0; + var nextstack = std.ArrayList(usize).init(allocator); + defer nextstack.deinit(); + + stacktrace = std.ArrayList(usize).init(allocator); + defer stacktrace.deinit(); + + var count: usize = 0; + lineit = std.mem.tokenize(try aoc.assertV(partit.next()), "\n"); + while (lineit.next()) |line| { + count += 1; + const valid = try checkRule(&rules, 0, line, &nextstack); + answer += @boolToInt(valid); + if (aoc.debug) { + std.debug.print("TRACE: [", .{}); + std.mem.reverse(usize, stacktrace.items); + printSlice(stacktrace.items); + std.debug.print("]\n", .{}); + if (valid) { + std.debug.print("MATCH {}\n\n", .{line}); + } else { + std.debug.print("NO MATCH {}\n\n", .{line}); + } + } + try nextstack.resize(0); + } + + std.debug.print("{} / {} matching\n", .{ answer, count }); +} + +pub const main = aoc.gen_main(part1, part2); diff --git a/src/day19/part1 b/src/day19/part1 @@ -0,0 +1,79 @@ +--- Day 19: Monster Messages --- + +You land in an airport surrounded by dense forest. As you walk to your high-speed train, the Elves +at the Mythical Information Bureau contact you again. They think their satellite has collected an +image of a sea monster! Unfortunately, the connection to the satellite is having +problems, and many of the messages sent back from the satellite have been corrupted. + +They sent you a list of the rules valid messages should obey and a list of +received messages they've collected so far (your puzzle input). + +The rules for valid messages (the top part of your puzzle input) are numbered and build +upon each other. For example: + +0: 1 2 +1: "a" +2: 1 3 | 3 1 +3: "b" + +Some rules, like 3: "b", simply match a single character (in this case, b). + +The remaining rules list the sub-rules that must be followed; for example, the rule 0: 1 2 means +that to match rule 0, the text being checked must match rule 1, and the text after the part that +matched rule 1 must then match rule 2. + +Some of the rules have multiple lists of sub-rules separated by a pipe (|). This means that +at least one list of sub-rules must match. (The ones that match might be different each +time the rule is encountered.) For example, the rule 2: 1 3 | 3 1 means that to match rule 2, the +text being checked must match rule 1 followed by rule 3 or it must match rule 3 +followed by rule 1. + +Fortunately, there are no loops in the rules, so the list of possible matches will be finite. Since +rule 1 matches a and rule 3 matches b, rule 2 matches either ab or ba. Therefore, rule 0 matches aab +or aba. + +Here's a more interesting example: + +0: 4 1 5 +1: 2 3 | 3 2 +2: 4 4 | 5 5 +3: 4 5 | 5 4 +4: "a" +5: "b" + +Here, because rule 4 matches a and rule 5 matches b, rule 2 matches two letters that are the same +(aa or bb), and rule 3 matches two letters that are different (ab or ba). + +Since rule 1 matches rules 2 and 3 once each in either order, it must match two pairs of letters, +one pair with matching letters and one pair with different letters. This leaves eight possibilities: +aaab, aaba, bbab, bbba, abaa, abbb, baaa, or babb. + +Rule 0, therefore, matches a (rule 4), then any of the eight options from rule 1, then b (rule 5): +aaaabb, aaabab, abbabb, abbbab, aabaab, aabbbb, abaaab, or ababbb. + +The received messages (the bottom part of your puzzle input) need to be checked against +the rules so you can determine which are valid and which are corrupted. Including the rules and the +messages together, this might look like: + +0: 4 1 5 +1: 2 3 | 3 2 +2: 4 4 | 5 5 +3: 4 5 | 5 4 +4: "a" +5: "b" + +ababbb +bababa +abbbab +aaabbb +aaaabbb + +Your goal is to determine the number of messages that completely match rule 0. In the +above example, ababbb and abbbab match, but bababa, aaabbb, and aaaabbb do not, producing the answer +2. The whole message must match all of rule 0; there can't be extra unmatched +characters in the message. (For example, aaaabbb might appear to match rule 0 above, but it has an +extra unmatched b on the end.) + +How many messages completely match rule 0? + + diff --git a/src/day19/part2 b/src/day19/part2 @@ -0,0 +1,92 @@ +--- Part Two --- + +As you look over the list of messages, you realize your matching rules aren't quite right. To fix +them, completely replace rules 8: 42 and 11: 42 31 with the following: + +8: 42 | 42 8 +11: 42 31 | 42 11 31 + +This small change has a big impact: now, the rules do contain loops, and the list of +messages they could hypothetically match is infinite. You'll need to determine how these changes +affect which messages are valid. + +Fortunately, many of the rules are unaffected by this change; it might help to start by looking at +which rules always match the same set of values and how those rules (especially rules +42 and 31) are used by the new versions of rules 8 and 11. + +(Remember, you only need to handle the rules you have; building a solution that could +handle any hypothetical combination of rules would be significantly more difficult.) + +For example: + +42: 9 14 | 10 1 +9: 14 27 | 1 26 +10: 23 14 | 28 1 +1: "a" +11: 42 31 +5: 1 14 | 15 1 +19: 14 1 | 14 14 +12: 24 14 | 19 1 +16: 15 1 | 14 14 +31: 14 17 | 1 13 +6: 14 14 | 1 14 +2: 1 24 | 14 4 +0: 8 11 +13: 14 3 | 1 12 +15: 1 | 14 +17: 14 2 | 1 7 +23: 25 1 | 22 14 +28: 16 1 +4: 1 1 +20: 14 14 | 1 15 +3: 5 14 | 16 1 +27: 1 6 | 14 18 +14: "b" +21: 14 1 | 1 14 +25: 1 1 | 1 14 +22: 14 14 +8: 42 +26: 14 22 | 1 20 +18: 15 15 +7: 14 5 | 1 21 +24: 14 1 + +abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa +bbabbbbaabaabba +babbbbaabbbbbabbbbbbaabaaabaaa +aaabbbbbbaaaabaababaabababbabaaabbababababaaa +bbbbbbbaaaabbbbaaabbabaaa +bbbababbbbaaaaaaaabbababaaababaabab +ababaaaaaabaaab +ababaaaaabbbaba +baabbaaaabbaaaababbaababb +abbbbabbbbaaaababbbbbbaaaababb +aaaaabbaabaaaaababaa +aaaabbaaaabbaaa +aaaabbaabbaaaaaaabbbabbbaaabbaabaaa +babaaabbbaaabaababbaabababaaab +aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba + +Without updating rules 8 and 11, these rules only match three messages: bbabbbbaabaabba, +ababaaaaaabaaab, and ababaaaaabbbaba. + +However, after updating rules 8 and 11, a total of 12 messages match: + + + - bbabbbbaabaabba + - babbbbaabbbbbabbbbbbaabaaabaaa + - aaabbbbbbaaaabaababaabababbabaaabbababababaaa + - bbbbbbbaaaabbbbaaabbabaaa + - bbbababbbbaaaaaaaabbababaaababaabab + - ababaaaaaabaaab + - ababaaaaabbbaba + - baabbaaaabbaaaababbaababb + - abbbbabbbbaaaababbbbbbaaaababb + - aaaaabbaabaaaaababaa + - aaaabbaabbaaaaaaabbbabbbaaabbaabaaa + - aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba + + +After updating rules 8 and 11, how many messages completely match rule 0? + + diff --git a/src/day19/trace b/src/day19/trace @@ -0,0 +1,38 @@ +STACK TRACE FOR aaaabbaaaabbaaa + +[0] +-> [8] 11 + -> [42] | 42 8 + -> [9] 14 | 10 1 + -> 14 27 | [1] 26 + -> 14 27 | (1) [26] + -> 14 22 | [1] 20 + -> 14 22 | (1) [20] + -> 14 14 | [1] 15 + -> 14 14 | (1) [15] + -> [1] | 14 + -> (9) [14] | 10 1 +-> (8) [11] + -> 42 31 | [42] 11 31 + -> 9 14 | [10] 1 + -> 23 14 | [28] 1 + -> [16] 1 + -> [15] 1 | 14 14 + -> 1 | [14] + -> (15) [1] | 14 14 + -> (16) [1] + -> 23 14 | (28) [1] + -> 9 14 | (10) [1] + -> 42 31 | (42) [11] 31 + -> 42 31 | [42] 11 31 + -> 9 14 | [10] 1 + -> 23 14 | [28] 1 + -> [16] 1 + -> 15 1 | [14] 14 + -> 15 1 | (14) [14] + -> (16) [1] + -> 23 14 | (28) [1] + -> 9 14 | (10) [1] + STOP HERE + +