-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01935ae
commit bfd2f74
Showing
9 changed files
with
338 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]), | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters