Skip to content

Commit

Permalink
Day 19
Browse files Browse the repository at this point in the history
  • Loading branch information
jongeorge1 committed Feb 26, 2019
1 parent 01935ae commit bfd2f74
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 1 deletion.
3 changes: 3 additions & 0 deletions AoC2018.Runner/AoC2018.Runner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
<None Update="Input\day16.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Input\day19.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Input\day18.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
37 changes: 37 additions & 0 deletions AoC2018.Runner/Input/day19.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion AoC2018.Runner/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"AoC2018.Runner": {
"commandName": "Project",
"commandLineArgs": "18 1"
"commandLineArgs": "19 1"
}
}
}
19 changes: 19 additions & 0 deletions AoC2018.Solutions/Day19/Instruction.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
37 changes: 37 additions & 0 deletions AoC2018.Solutions/Day19/Operation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace AoC2018.Solutions.Day19
{
using System;

public class Operation
{
public Operation(string name, Action<int[], Instruction> 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<int[], Instruction> Execute { get; }
}
}
32 changes: 32 additions & 0 deletions AoC2018.Solutions/Day19/Parser.cs
Original file line number Diff line number Diff line change
@@ -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<Instruction> 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]),
};
}
}
}
44 changes: 44 additions & 0 deletions AoC2018.Solutions/Day19/Part01.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
164 changes: 164 additions & 0 deletions AoC2018.Solutions/Day19/Translated Program.txt
Original file line number Diff line number Diff line change
@@ -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?
1 change: 1 addition & 0 deletions AoC2018.Tests/AoCTestCases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bfd2f74

Please sign in to comment.