diff --git a/AoC2018.Runner/AoC2018.Runner.csproj b/AoC2018.Runner/AoC2018.Runner.csproj index 2274ce8..a465352 100644 --- a/AoC2018.Runner/AoC2018.Runner.csproj +++ b/AoC2018.Runner/AoC2018.Runner.csproj @@ -73,6 +73,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/AoC2018.Runner/Input/day19.txt b/AoC2018.Runner/Input/day19.txt new file mode 100644 index 0000000..b31a352 --- /dev/null +++ b/AoC2018.Runner/Input/day19.txt @@ -0,0 +1,37 @@ +#ip 1 +addi 1 16 1 +seti 1 1 5 +seti 1 4 2 +mulr 5 2 3 +eqrr 3 4 3 +addr 3 1 1 +addi 1 1 1 +addr 5 0 0 +addi 2 1 2 +gtrr 2 4 3 +addr 1 3 1 +seti 2 7 1 +addi 5 1 5 +gtrr 5 4 3 +addr 3 1 1 +seti 1 8 1 +mulr 1 1 1 +addi 4 2 4 +mulr 4 4 4 +mulr 1 4 4 +muli 4 11 4 +addi 3 1 3 +mulr 3 1 3 +addi 3 3 3 +addr 4 3 4 +addr 1 0 1 +seti 0 3 1 +setr 1 1 3 +mulr 3 1 3 +addr 1 3 3 +mulr 1 3 3 +muli 3 14 3 +mulr 3 1 3 +addr 4 3 4 +seti 0 9 0 +seti 0 4 1 \ No newline at end of file diff --git a/AoC2018.Runner/Properties/launchSettings.json b/AoC2018.Runner/Properties/launchSettings.json index 2b5ac1a..7ec2632 100644 --- a/AoC2018.Runner/Properties/launchSettings.json +++ b/AoC2018.Runner/Properties/launchSettings.json @@ -2,7 +2,7 @@ "profiles": { "AoC2018.Runner": { "commandName": "Project", - "commandLineArgs": "18 1" + "commandLineArgs": "19 1" } } } \ No newline at end of file diff --git a/AoC2018.Solutions/Day19/Instruction.cs b/AoC2018.Solutions/Day19/Instruction.cs new file mode 100644 index 0000000..5b82a89 --- /dev/null +++ b/AoC2018.Solutions/Day19/Instruction.cs @@ -0,0 +1,19 @@ +namespace AoC2018.Solutions.Day19 +{ + using System; + using System.Collections.Generic; + using System.Diagnostics; + using System.Text.RegularExpressions; + + [DebuggerDisplay("{OpName} {A} {B} {C}")] + public class Instruction + { + public string OpName { get; set; } + + public int A { get; set; } + + public int B { get; set; } + + public int C { get; set; } + } +} diff --git a/AoC2018.Solutions/Day19/Operation.cs b/AoC2018.Solutions/Day19/Operation.cs new file mode 100644 index 0000000..597c982 --- /dev/null +++ b/AoC2018.Solutions/Day19/Operation.cs @@ -0,0 +1,37 @@ +namespace AoC2018.Solutions.Day19 +{ + using System; + + public class Operation + { + public Operation(string name, Action instruction) + { + this.Name = name; + this.Execute = instruction; + } + + public static Operation[] All { get; } = new[] + { + new Operation("addr", (registers, instruction) => registers[instruction.C] = registers[instruction.A] + registers[instruction.B]), + new Operation("addi", (registers, instruction) => registers[instruction.C] = registers[instruction.A] + instruction.B), + new Operation("mulr", (registers, instruction) => registers[instruction.C] = registers[instruction.A] * registers[instruction.B]), + new Operation("muli", (registers, instruction) => registers[instruction.C] = registers[instruction.A] * instruction.B), + new Operation("banr", (registers, instruction) => registers[instruction.C] = registers[instruction.A] & registers[instruction.B]), + new Operation("bani", (registers, instruction) => registers[instruction.C] = registers[instruction.A] & instruction.B), + new Operation("borr", (registers, instruction) => registers[instruction.C] = registers[instruction.A] | registers[instruction.B]), + new Operation("bori", (registers, instruction) => registers[instruction.C] = registers[instruction.A] | instruction.B), + new Operation("setr", (registers, instruction) => registers[instruction.C] = registers[instruction.A]), + new Operation("seti", (registers, instruction) => registers[instruction.C] = instruction.A), + new Operation("gtir", (registers, instruction) => registers[instruction.C] = instruction.A > registers[instruction.B] ? 1 : 0), + new Operation("gtri", (registers, instruction) => registers[instruction.C] = registers[instruction.A] > instruction.B ? 1 : 0), + new Operation("gtrr", (registers, instruction) => registers[instruction.C] = registers[instruction.A] > registers[instruction.B] ? 1 : 0), + new Operation("eqir", (registers, instruction) => registers[instruction.C] = instruction.A == registers[instruction.B] ? 1 : 0), + new Operation("eqri", (registers, instruction) => registers[instruction.C] = registers[instruction.A] == instruction.B ? 1 : 0), + new Operation("eqrr", (registers, instruction) => registers[instruction.C] = registers[instruction.A] == registers[instruction.B] ? 1 : 0), + }; + + public string Name { get; set; } + + public Action Execute { get; } + } +} diff --git a/AoC2018.Solutions/Day19/Parser.cs b/AoC2018.Solutions/Day19/Parser.cs new file mode 100644 index 0000000..230e644 --- /dev/null +++ b/AoC2018.Solutions/Day19/Parser.cs @@ -0,0 +1,32 @@ +namespace AoC2018.Solutions.Day19 +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text.RegularExpressions; + + public static class Parser + { + public static Instruction[] Parse(string input) + { + IEnumerable instructions = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) + .Skip(1) + .Select(BuildInstructionFromInputLine); + + return instructions.ToArray(); + } + + public static Instruction BuildInstructionFromInputLine(string input) + { + string[] components = input.Split(' '); + + return new Instruction + { + OpName = components[0], + A = int.Parse(components[1]), + B = int.Parse(components[2]), + C = int.Parse(components[3]), + }; + } + } +} diff --git a/AoC2018.Solutions/Day19/Part01.cs b/AoC2018.Solutions/Day19/Part01.cs new file mode 100644 index 0000000..e8aaaa6 --- /dev/null +++ b/AoC2018.Solutions/Day19/Part01.cs @@ -0,0 +1,44 @@ +namespace AoC2018.Solutions.Day19 +{ + using System; + using System.Linq; + + public class Part01 : ISolution + { + public string Solve(string input) + { + string[] rows = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); + + int instructionPointerRegister = int.Parse(rows[0].Split(' ')[1]); + int instructionPointer = 0; + + Instruction[] instructions = rows + .Skip(1) + .Select(Parser.BuildInstructionFromInputLine) + .ToArray(); + + var operations = Operation.All.ToDictionary(x => x.Name, x => x.Execute); + + int[] registers = new[] { 0, 0, 0, 0, 0, 0 }; + + while (instructionPointer >= 0 && instructionPointer < instructions.Length) + { + ////Console.Write($"{instructionPointer}:{registers[0]}:{registers[1]}:{registers[2]}:{registers[3]}:{registers[4]}:{registers[5]:D4}:"); + + registers[instructionPointerRegister] = instructionPointer; + + Instruction targetInstruction = instructions[instructionPointer]; + ////Console.Write($"{targetInstruction.OpName}:{targetInstruction.A}:{targetInstruction.B}:{targetInstruction.C}:"); + + operations[targetInstruction.OpName](registers, targetInstruction); + + instructionPointer = registers[instructionPointerRegister]; + instructionPointer++; + + ////Console.WriteLine($"{registers[0]:D4}:{registers[1]:D4}:{registers[2]:D4}:{registers[3]:D4},{registers[4]:D4},{registers[5]:D4}"); + } + + return registers[0].ToString(); + } + } +} diff --git a/AoC2018.Solutions/Day19/Translated Program.txt b/AoC2018.Solutions/Day19/Translated Program.txt new file mode 100644 index 0000000..6b8468f --- /dev/null +++ b/AoC2018.Solutions/Day19/Translated Program.txt @@ -0,0 +1,164 @@ +Original program + +#ip 1 +00: addi 1 16 1 +01: seti 1 1 5 +02: seti 1 4 2 +03: mulr 5 2 3 +04: eqrr 3 4 3 +05: addr 3 1 1 +06: addi 1 1 1 +07: addr 5 0 0 +08: addi 2 1 2 +09: gtrr 2 4 3 +10: addr 1 3 1 +11: seti 2 7 1 +12: addi 5 1 5 +13: gtrr 5 4 3 +14: addr 3 1 1 +15: seti 1 8 1 +16: mulr 1 1 1 +17: addi 4 2 4 +18: mulr 4 4 4 +19: mulr 1 4 4 +20: muli 4 11 4 +21: addi 3 1 3 +22: mulr 3 1 3 +23: addi 3 3 3 +24: addr 4 3 4 +25: addr 1 0 1 +26: seti 0 3 1 +27: setr 1 1 3 +28: mulr 3 1 3 +29: addr 1 3 3 +30: mulr 1 3 3 +31: muli 3 14 3 +32: mulr 3 1 3 +33: addr 4 3 4 +34: seti 0 9 0 +35: seti 0 4 1 + +Step 1 - straight conversion from opcodes + +ip = 1 +r0 = 0 +r1 = 0 +r2 = 0 +r3 = 0 +r4 = 0 +r5 = 0 + +00: r1 = r1 + 16 +01: r5 = 1 +02: r2 = 1 +03: r3 = r5 * r2 +04: r3 = r3 == r4 ? 1 : 0 +05: r1 = r3 + r1 +06: r1 = r1 + 1 +07: r0 = r5 + r0 +08: r2 = r2 + 1 +09: r3 = r2 > r4 ? 1 : 0 +10: r1 = r1 + r3 +11: r1 = 2 +12: r5 = r5 + 1 +13: r3 = r5 > r4 ? 1 : 0 +14: r1 = r3 + r1 +15: r1 = 1 +16: r1 = r1 * r1 +17: r4 = r4 + 2 +18: r4 = r4 * r4 +19: r4 = r1 * r4 +20: r4 = r4 * 11 +21: r3 = r3 + 1 +22: r3 = r3 * r1 +23: r3 = r3 + 3 +24: r4 = r4 + r3 +25: r1 = r1 + r0 +26: r1 = 0 +27: r3 = r1 +28: r3 = r3 * r1 +29: r3 = r1 + r3 +30: r3 = r1 * r3 +31: r3 = r3 * 14 +32: r3 = r3 * r1 +33: r4 = r4 + r3 +34: r0 = 0 +35: r1 = 0 + + + + + +Structure: + +Setup: +0 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +1 + + outer loop: + 2 + inner loop: + 3 + 4 + 5 + 6 + 7 (to exit inner loop) + 8 + 9 + 10 + 11 (to continue on inner loop, skipped if exiting) + 12 + 13 + 14 + 15 +16 + + +---- + +Setup: +00: r1 = r1 + 16 +17: r4 = r4 + 2 +18: r4 = r4 * r4 +19: r4 = r1 * r4 +20: r4 = r4 * 11 +21: r3 = r3 + 1 +22: r3 = r3 * r1 +23: r3 = r3 + 3 +24: r4 = r4 + r3 +25: r1 = r1 + r0 +26: r1 = 0 + +for (r5 = 1; r5 <= r4; r5++) + for (r2 = 1; r2 <= r4; r2++) { + 3 : r3 = r5 * r2 // product of inner and outer loop countets + 4 : if (r3 == r4) r3 = 1 else r3 = 0 // do they match the target number? + 5 : r1 = r3 + r1 + 6 : r1 = r1 + 1 + 7 (to exit inner loop): r0 = r5 + r0 + 8 : r2 = r2 + 1 + 9 : if (r2 > r4) r3 = 1 else r3 = 0 + } +} + +looks like it's summing factors? + +1344 + +So, the answer is the sum of the factors of the number in r4 when we hit line 3 for the first time + +for part 2, that number is 10551261 +factors are 1, 3, 7, 21, 502441, 1507323, 3517087, 10551261 +sum is 16078144 + +why do I do this to myself? \ No newline at end of file diff --git a/AoC2018.Tests/AoCTestCases.cs b/AoC2018.Tests/AoCTestCases.cs index 8b40113..8e3e33b 100644 --- a/AoC2018.Tests/AoCTestCases.cs +++ b/AoC2018.Tests/AoCTestCases.cs @@ -57,6 +57,7 @@ public class AoCTestCases [TestCase(17, 1, "x=495, y=2..7\r\ny=7, x=495..501\r\nx=501, y=3..7\r\nx=498, y=2..4\r\nx=506, y=1..2\r\nx=498, y=10..13\r\nx=504, y=10..13\r\ny=13, x=498..504", "57")] [TestCase(17, 2, "x=495, y=2..7\r\ny=7, x=495..501\r\nx=501, y=3..7\r\nx=498, y=2..4\r\nx=506, y=1..2\r\nx=498, y=10..13\r\nx=504, y=10..13\r\ny=13, x=498..504", "29")] [TestCase(18, 1, ".#.#...|#.\r\n.....#|##|\r\n.|..|...#.\r\n..|#.....#\r\n#.#|||#|#|\r\n...#.||...\r\n.|....|...\r\n||...#|.#|\r\n|.||||..|.\r\n...#.|..|.", "1147")] + [TestCase(19, 1, "#ip 0\r\nseti 5 0 1\r\nseti 6 0 2\r\naddi 0 1 0\r\naddr 1 2 3\r\nsetr 1 0 0\r\nseti 8 0 4\r\nseti 9 0 5", "6")] public void Tests(int day, int part, string input, string expectedResult) { ISolution solution = SolutionFactory.GetSolution(day, part);