From f29dd5efa081f7a583d54b9e180e37f953c9bd65 Mon Sep 17 00:00:00 2001 From: Parnic Date: Fri, 24 Dec 2021 15:19:49 -0600 Subject: [PATCH] Day 24 I solved this by hand, so there's no code solving it programmatically. Maybe I'll come back later and solve it with code... --- advent-of-code-2021.csproj | 3 + inputs/24.txt | 255 +++++++++++++++++++++++++++++++++++++ src/24.cs | 176 +++++++++++++++++++++++++ src/main.cs | 2 +- 4 files changed, 435 insertions(+), 1 deletion(-) create mode 100644 inputs/24.txt create mode 100644 src/24.cs diff --git a/advent-of-code-2021.csproj b/advent-of-code-2021.csproj index 051fe34..47df67a 100644 --- a/advent-of-code-2021.csproj +++ b/advent-of-code-2021.csproj @@ -87,6 +87,9 @@ PreserveNewest + + PreserveNewest + diff --git a/inputs/24.txt b/inputs/24.txt new file mode 100644 index 0000000..78fe59a --- /dev/null +++ b/inputs/24.txt @@ -0,0 +1,255 @@ +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 11 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 6 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 13 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 14 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 15 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 14 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -8 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 10 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 13 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 9 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 15 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 12 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -11 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 8 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -4 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 13 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -15 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 12 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 14 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 6 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 14 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 9 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -1 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 15 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -8 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 4 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -14 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 10 +mul y x +add z y + + 101011000001011100001001101 +10001011110100101011011101110101 \ No newline at end of file diff --git a/src/24.cs b/src/24.cs new file mode 100644 index 0000000..232f64a --- /dev/null +++ b/src/24.cs @@ -0,0 +1,176 @@ +using System.Diagnostics; + +namespace aoc2021; + +internal class Day24 : Day +{ + public record struct Instruction(string Opcode, char Op1, string? Op2); + + [DebuggerDisplay("Input: {InputStr}, w: {w}, x: {x}, y: {y}, z: {z}")] + public class Machine + { + public string InputStr; + private int InputStrIdx = 0; + public long w = 0; + public long x = 0; + public long y = 0; + public long z = 0; + + public Machine(string input) + { + InputStr = input; + } + + public void ProcessInstruction(Instruction inst) + { + switch (inst.Opcode) + { + case "inp": + StoreValue(inst.Op1, (long)char.GetNumericValue(InputStr[InputStrIdx])); + InputStrIdx++; + break; + + default: + var val = RetrieveValue(inst.Op1); + switch (inst.Opcode) + { + case "add": + val += GetValue(inst.Op2!); + break; + + case "mul": + val *= GetValue(inst.Op2!); + break; + + case "div": + val /= GetValue(inst.Op2!); + break; + + case "mod": + val %= GetValue(inst.Op2!); + break; + + case "eql": + var b = GetValue(inst.Op2!); + val = val == b ? 1 : 0; + break; + + default: + throw new Exception(); + } + StoreValue(inst.Op1, val); + break; + } + } + + public long GetValue(string op2) + { + if (long.TryParse(op2, out long iop2)) + { + return iop2; + } + + return RetrieveValue(op2[0]); + } + + public long RetrieveValue(char var) => var switch + { + 'w' => w, + 'x' => x, + 'y' => y, + 'z' => z, + _ => throw new Exception(), + }; + + public void StoreValue(char var, long value) + { + switch (var) + { + case 'w': + w = value; + break; + + case 'x': + x = value; + break; + + case 'y': + y = value; + break; + + case 'z': + z = value; + break; + + default: + throw new Exception(); + } + } + } + + internal override void Go() + { + var lines = Util.ReadAllLines("inputs/24.txt"); + List instructions = new(); + foreach (var line in lines) + { + if (string.IsNullOrEmpty(line)) + { + break; + } + + var inst = line.Split(' ', 2); + switch (inst[0]) + { + case "inp": + instructions.Add(new Instruction(inst[0], inst[1][0], null)); + break; + + default: + { + var operands = inst[1].Split(' '); + instructions.Add(new Instruction(inst[0], operands[0][0], operands[1])); + } + break; + } + } + Part1(instructions); + Part2(instructions); + } + + private static void Part1(IList instructions) + { + using var t = new Timer(); + + Machine m = new("99394899891971"); + foreach (var inst in instructions) + { + m.ProcessInstruction(inst); + } + if (m.z != 0) + { + throw new Exception(); + } + + t.Stop(); + Logger.Log($"<+black>> part1: <+white>99394899891971"); + } + + private static void Part2(IList instructions) + { + using var t = new Timer(); + + Machine m = new("92171126131911"); + foreach (var inst in instructions) + { + m.ProcessInstruction(inst); + } + if (m.z != 0) + { + throw new Exception(); + } + + t.Stop(); + Logger.Log($"<+black>> part2: <+white>92171126131911"); + } +} diff --git a/src/main.cs b/src/main.cs index be74a7c..7680527 100644 --- a/src/main.cs +++ b/src/main.cs @@ -20,7 +20,7 @@ else Day? day = null; if (string.IsNullOrEmpty(arg)) { - day = new Day23(); + day = new Day24(); } else {