-
Notifications
You must be signed in to change notification settings - Fork 0
/
2016_23.cs
234 lines (204 loc) · 7.07 KB
/
2016_23.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
namespace AOC.CSharp;
public class AOC2016_23
{
public static long Solve1(string[] lines)
{
Registers registers = new();
registers.Set("a", 7);
return Solve(registers, lines);
}
public static long Solve2(string[] lines)
{
// Solve 7-10 to get answers we can use to infer a pattern
//for (int i = 7; i <= 10; i++)
//{
// Registers registers = new();
// registers.Set("a", 12);
// long result = Solve(registers, lines);
// Console.WriteLine("{0} -> {1}", i, result);
//}
Registers registers = new();
registers.Set("a", 7);
long solution = Solve(registers, lines);
// Solve a fast and simple case and use the formula we derived to compute
// versions that get MUCH more expensive to solve by going through the motions.
// The solution is easy to compute using the previous solution and a bit of factorial math.
for (int i = 8; i <= 12; i++)
{
long prevFact = Factorial(i - 1);
long fact = Factorial(i);
solution = solution + fact - prevFact;
}
return solution;
}
private static long Factorial(int n)
{
long result = 1;
for (int i = 1; i <= n; i++)
{
result *= i;
}
return result;
}
private static long Solve(Registers registers, string[] lines)
{
List<Command> commands = lines.Select(Parse).ToList();
int j = 0;
int i = 0;
while (i < commands.Count)
{
int iStart = i;
j++;
Command c = commands[i];
switch (c.Type)
{
case CommandType.Copy:
{
int value = c.Arg1IsInt ? int.Parse(c.Arg1) : registers.Get(c.Arg1);
if (c.Arg2IsString)
{
registers.Set(c.Arg2, value);
}
i++;
break;
}
case CommandType.Increment:
{
if (c.Arg1IsString)
{
registers.Transform(c.Arg1, v => v + 1);
}
i++;
break;
}
case CommandType.Decrement:
{
if (c.Arg1IsString)
{
registers.Transform(c.Arg1, v => v - 1);
}
i++;
break;
}
case CommandType.JumpNotZero:
{
int cmpValue = c.Arg1IsInt ? int.Parse(c.Arg1) : registers.Get(c.Arg1);
int jmpAmount =
cmpValue == 0
? 1
: c.Arg2IsInt
? int.Parse(c.Arg2)
: registers.Get(c.Arg2);
i += jmpAmount;
break;
}
case CommandType.Toggle:
{
int offsetValue = c.Arg1IsInt ? int.Parse(c.Arg1) : registers.Get(c.Arg1);
int targetIdx = i + offsetValue;
if (targetIdx >= 0 && targetIdx < commands.Count)
{
Command tgt = commands[targetIdx];
switch (tgt.Type)
{
case CommandType.Increment:
commands[targetIdx] = new Command(CommandType.Decrement, tgt.Arg1);
break;
case CommandType.Decrement:
case CommandType.Toggle:
commands[targetIdx] = new Command(CommandType.Increment, tgt.Arg1);
break;
case CommandType.Copy:
commands[targetIdx] = new Command(
CommandType.JumpNotZero,
tgt.Arg1,
tgt.Arg2
);
break;
case CommandType.JumpNotZero:
commands[targetIdx] = new Command(
CommandType.Copy,
tgt.Arg1,
tgt.Arg2
);
break;
}
}
i++;
break;
}
}
}
return registers.Get("a");
}
private static Command Parse(string line)
{
string[] splits = line.Split(" ");
string inst = splits[0];
return inst switch
{
"cpy" => new Command(CommandType.Copy, splits[1], splits[2]),
"inc" => new Command(CommandType.Increment, splits[1]),
"dec" => new Command(CommandType.Decrement, splits[1]),
"jnz" => new Command(CommandType.JumpNotZero, splits[1], splits[2]),
"tgl" => new Command(CommandType.Toggle, splits[1]),
_ => throw new NotSupportedException(),
};
}
private class Command
{
public Command(CommandType type, string arg1, string arg2 = null)
{
Type = type;
Arg1 = arg1;
Arg2 = arg2;
}
public CommandType Type { get; }
public string Arg1 { get; }
public string Arg2 { get; }
public bool Arg1IsInt => int.TryParse(Arg1, out var _);
public bool Arg2IsInt => int.TryParse(Arg2, out var _);
public bool Arg1IsString => !Arg1IsInt;
public bool Arg2IsString => !Arg2IsInt;
public override string ToString()
{
string cmd = Type switch
{
CommandType.Copy => "cpy",
CommandType.Increment => "inc",
CommandType.Decrement => "dec",
CommandType.JumpNotZero => "jnz",
CommandType.Toggle => "tgl",
_ => throw new NotSupportedException(),
};
return $"{cmd} {Arg1} {Arg2}";
}
}
private enum CommandType
{
Copy,
Increment,
Decrement,
JumpNotZero,
Toggle
}
private class Registers
{
private const int DefaultValue = 0;
private readonly Dictionary<string, int> _dict = new();
public void Set(string reg, int value)
{
_dict[reg] = value;
}
public int Get(string reg)
{
return _dict.TryGetValue(reg, out int existing) ? existing : DefaultValue;
}
public void Transform(string reg, Func<int, int> transformer)
{
int existing = Get(reg);
int transformed = transformer(existing);
Set(reg, transformed);
}
}
}