-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
185 lines (157 loc) · 5.58 KB
/
Solution.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode2016.Day12
{
public class Solution
{
private readonly IReadOnlyList<IInstruction> _instructions;
public Solution(IEnumerable<string> input)
{
_instructions = input.Select(ParseInstruction).ToList();
}
public int PartOne()
{
var registers = new Dictionary<Register, int>
{
[Register.A] = 0,
[Register.B] = 0,
[Register.C] = 0,
[Register.D] = 0
};
Run(registers);
return registers[Register.A];
}
public int PartTwo()
{
var registers = new Dictionary<Register, int>
{
[Register.A] = 0,
[Register.B] = 0,
[Register.C] = 1,
[Register.D] = 0
};
Run(registers);
return registers[Register.A];
}
private void Run(IDictionary<Register, int> registers)
{
var idx = 0;
while (idx < _instructions.Count)
{
switch (_instructions[idx])
{
case CopyLiteralValue cpy1:
registers[cpy1.Destination] = cpy1.Literal;
break;
case CopyRegisterValue cpy2:
registers[cpy2.Destination] = registers[cpy2.Source];
break;
case IncrementRegisterValue inc:
registers[inc.Destination]++;
break;
case DecrementRegisterValue dec:
registers[dec.Destination]--;
break;
case JumpIfValueNotZero jnz1 when jnz1.Source != 0:
idx += jnz1.Offset;
continue;
case JumpIfRegisterNotZero jnz2 when registers[jnz2.Source] != 0:
idx += jnz2.Offset;
continue;
}
idx++;
}
}
private static IInstruction ParseInstruction(string instruction)
{
var segments = instruction.Split(" ");
return segments[0] switch
{
"cpy" when int.TryParse(segments[1], out var value) => new CopyLiteralValue(value,
ParseRegister(segments[2])),
"cpy" => new CopyRegisterValue(ParseRegister(segments[1]), ParseRegister(segments[2])),
"inc" => new IncrementRegisterValue(ParseRegister(segments[1])),
"dec" => new DecrementRegisterValue(ParseRegister(segments[1])),
"jnz" when int.TryParse(segments[1], out var value) => new JumpIfValueNotZero(value,
int.Parse(segments[2])),
"jnz" => new JumpIfRegisterNotZero(ParseRegister(segments[1]), int.Parse(segments[2])),
_ => throw new ArgumentException($"Invalid instruction '{instruction}'")
};
}
private static Register ParseRegister(string register) => register switch
{
"a" => Register.A,
"b" => Register.B,
"c" => Register.C,
"d" => Register.D,
_ => throw new ArgumentException($"Invalid register '{register}'")
};
private enum Register
{
A,
B,
C,
D
}
private interface IInstruction
{
}
private class CopyLiteralValue : IInstruction
{
public CopyLiteralValue(int literal, Register destination)
{
Literal = literal;
Destination = destination;
}
public int Literal { get; }
public Register Destination { get; }
}
private class CopyRegisterValue : IInstruction
{
public CopyRegisterValue(Register source, Register destination)
{
Source = source;
Destination = destination;
}
public Register Source { get; }
public Register Destination { get; }
}
private class IncrementRegisterValue : IInstruction
{
public IncrementRegisterValue(Register destination)
{
Destination = destination;
}
public Register Destination { get; }
}
private class DecrementRegisterValue : IInstruction
{
public DecrementRegisterValue(Register destination)
{
Destination = destination;
}
public Register Destination { get; }
}
private class JumpIfValueNotZero : IInstruction
{
public JumpIfValueNotZero(int source, int offset)
{
Source = source;
Offset = offset;
}
public int Source { get; }
public int Offset { get; }
}
private class JumpIfRegisterNotZero : IInstruction
{
public JumpIfRegisterNotZero(Register source, int offset)
{
Source = source;
Offset = offset;
}
public Register Source { get; }
public int Offset { get; }
}
}
}