-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay17.cs
More file actions
162 lines (146 loc) · 4.9 KB
/
Day17.cs
File metadata and controls
162 lines (146 loc) · 4.9 KB
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
using _2024.Utils;
namespace _2024._17;
public class Day17 : Base
{
public Day17(bool example) : base(example)
{
Day = "17";
}
private static long GetComboOperand(int operand, List<long> registers)
{
return operand switch
{
>= 0 and <= 3 => operand,
4 => registers[0],
5 => registers[1],
6 => registers[2],
_ => throw new InputInvalidException("Operand invalid")
};
}
private string RunProgram(List<int> instructions, List<long> registers, int stage)
{
List<long> output = [];
for (int i = 0; i < instructions.Count;)
{
switch (instructions[i])
{
case 0:
registers[0] = (long)(registers[0] / Math.Pow(2, GetComboOperand(instructions[i + 1], registers)));
i += 2;
break;
case 1:
registers[1] ^= instructions[i + 1];
i += 2;
break;
case 2:
registers[1] = MathExtensions.Modulo(GetComboOperand(instructions[i + 1], registers), 8);
i += 2;
break;
case 3:
if (registers[0] == 0)
{
i += 2;
continue;
}
i = instructions[i + 1];
break;
case 4:
registers[1] ^= registers[2];
i += 2;
break;
case 5:
output.Add(GetComboOperand(instructions[i + 1], registers) % 8);
if (stage == 2)
{
return output[^1].ToString();
}
i += 2;
break;
case 6:
registers[1] = (long)(registers[0] / Math.Pow(2, GetComboOperand(instructions[i + 1], registers)));
i += 2;
break;
case 7:
registers[2] = (long)(registers[0] / Math.Pow(2, GetComboOperand(instructions[i + 1], registers)));
i += 2;
break;
default:
throw new InputInvalidException($"Opcode: {instructions[i]} is not valid");
}
}
return string.Join(",", output);
}
public override object PartOne()
{
string[] input = ReadInput();
// three registers
List<long> registers = new List<long>(3);
List<int> instructions = [];
foreach(string line in input)
{
if (string.IsNullOrEmpty(line))
{
continue;
}
string[] split = line.Split(": ");
if (line.StartsWith("Register"))
{
registers.Add(long.Parse(split[1]));
}
if (line.StartsWith("Program"))
{
instructions = split[1].Split(",").Select(int.Parse).ToList();
}
}
return RunProgram(instructions, registers, 1);
}
public override object PartTwo()
{
string[] input = ReadInput();
// three registers
List<long> registers = new(3);
List<int> instructions = [];
foreach (string line in input)
{
if (string.IsNullOrEmpty(line))
{
continue;
}
string[] split = line.Split(": ");
if (line.StartsWith("Register"))
{
registers.Add(long.Parse(split[1]));
}
if (line.StartsWith("Program"))
{
instructions = split[1].Split(",").Select(int.Parse).ToList();
}
}
int currInstruction = instructions.Count - 1;
List<long> candidates = [0];
while (currInstruction >= 0)
{
List<long> newCandidates = new(8*candidates.Count);
foreach (long candidate in candidates)
{
for (long i = 0; i < 8; i++)
{
registers[0] = (candidate * 8) + i;
registers[1] = 0;
registers[2] = 0;
string output = RunProgram(instructions, registers, 2);
if (output == instructions[currInstruction].ToString())
{
newCandidates.Add((candidate * 8) + i);
}
}
}
candidates = [..newCandidates];
currInstruction--;
}
return candidates.Min();
}
public override void Reset()
{
}
}