-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.cs
217 lines (172 loc) · 6.67 KB
/
Day16.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Runtime.Serialization;
namespace AdventOfCode2018
{
class Day16 : Day
{
private bool debug = false;
public override bool Test()
{
return Utils.Test(Part1, testInput, "1"); /*&&
Utils.Test(Part2, testInputs, new string[] { "29064", "31284", "3478", "6474", "1140" });*/
}
public override string Part1(string input, dynamic options)
{
var data = RegexDeserializable.Deserialize<TestResult>(input);
return data.Count(t => AllOps.Count(o => t.Evaluate(o)) >= 3).ToString();
}
public override string Part2(string input, dynamic options)
{
var data = RegexDeserializable.Deserialize<TestResult>(input);
Dictionary<int, Op> opcodes = new Dictionary<int, Op>();
var opcodeMap = data.Select(d => new { d.opcode, possibleOps = AllOps.Where(o => d.Evaluate(o)) });
var groupedMap = opcodeMap.GroupBy(o => o.opcode).ToDictionary(g => g.Key, g => g.First().possibleOps.Where(o1 => g.All(o2 => o2.possibleOps.Contains(o1))));
try
{
findMatches(opcodes, groupedMap);
}
catch (CompleteException)
{
}
foreach (var test in data)
{
var op = opcodes[test.opcode];
Debug.Assert(test.Evaluate(op));
}
var input2 = Inputs.ForDay(162);
var program = RegexDeserializable.Deserialize<Statement>(input2);
var registers = new[] { 0, 0, 0, 0 };
foreach (var statement in program)
{
var op = opcodes[statement.opcode];
op.Apply(statement.p0, statement.p1, statement.p2, registers);
}
return registers[0].ToString();
}
List<Dictionary<int, Op>> allsolutions = new List<Dictionary<int, Op>>();
private void findMatches(Dictionary<int, Op> opcodes, Dictionary<int,IEnumerable<Op>> data)
{
var allunused = data.Where(d => !opcodes.ContainsKey(d.Key)).Select(d => new { opcode = d.Key, possibleOps = d.Value.Where(o => !opcodes.ContainsValue(o)) }).OrderBy(d => d.possibleOps.Count());
var unused = allunused.FirstOrDefault();
if (unused != null)
{
foreach (var possibleOp in unused.possibleOps)
{
opcodes[unused.opcode] = possibleOp;
findMatches(opcodes, data);
}
opcodes.Remove(unused.opcode);
}
else
{
var copy = new Dictionary<int, Op>(opcodes);
/*if (!allsolutions.Any(s => s.OrderBy(k => k.Key).SequenceEqual(copy.OrderBy(k => k.Key))))
{
allsolutions.Add(copy);
foreach (var kv in copy)
{
Console.Write(kv.Key + ":" + kv.Value.name + " ");
}
Console.WriteLine();
}*/
throw new CompleteException();
}
}
private Op[] AllOps = new[]
{
new Op("addr", (a,b) => a+b),
new Op("addi", (a,b) => a+b),
new Op("mulr", (a,b) => a*b),
new Op("muli", (a,b) => a*b),
new Op("banr", (a,b) => a&b),
new Op("bani", (a,b) => a&b),
new Op("borr", (a,b) => a|b),
new Op("bori", (a,b) => a|b),
new Op("setr", (a,b) => a),
new Op("seii", (a,b) => a),
new Op("gtir", (a,b) => a > b ? 1 : 0),
new Op("gtrr", (a,b) => a > b ? 1 : 0),
new Op("gtri", (a,b) => a > b ? 1 : 0),
new Op("eqir", (a,b) => a == b ? 1 : 0),
new Op("eqrr", (a,b) => a == b ? 1 : 0),
new Op("eqri", (a,b) => a == b ? 1 : 0),
};
[RegexDeserializable(@"(?<opcode>\d+) (?<p0>\d+) (?<p1>\d+) (?<p2>\d+)")]
public class Statement
{
public int opcode;
public int p0;
public int p1;
public int p2;
}
[RegexDeserializable(@"Before: \[(?<r0>\d+), (?<r1>\d+), (?<r2>\d+), (?<r3>\d+)]\r?\n(?<opcode>\d+) (?<p0>\d+) (?<p1>\d+) (?<p2>\d+)\r?\nAfter: \[(?<a0>\d+), (?<a1>\d+), (?<a2>\d+), (?<a3>\d+)]")]
public class TestResult
{
public int r0;
public int r1;
public int r2;
public int r3;
public int opcode;
public int p0;
public int p1;
public int p2;
public int a0;
public int a1;
public int a2;
public int a3;
public bool Evaluate(Op op)
{
int[] registers = new[] { r0, r1, r2, r3 };
op.Apply(p0, p1, p2, registers);
return registers.SequenceEqual(new[] { a0, a1, a2, a3 });
}
}
public class Op
{
private Func<int, int, int> fn;
private bool r1;
private bool r2;
public string name;
public Op(string name, Func<int, int, int> fn)
{
this.name = name;
this.fn = fn;
r1 = name[2] != 'i';
r2 = name[3] != 'i';
}
public void Apply(int a1, int a2, int a3, int[] registers)
{
a1 = r1 ? registers[a1] : a1;
a2 = r2 ? registers[a2] : a2;
registers[a3] = fn(a1, a2);
}
}
private string testInput =
@"Before: [3, 2, 1, 1]
9 2 1 2
After: [3, 2, 2, 1]";
[Serializable]
private class CompleteException : Exception
{
public CompleteException()
{
}
public CompleteException(string message) : base(message)
{
}
public CompleteException(string message, Exception innerException) : base(message, innerException)
{
}
protected CompleteException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
}