-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.linq
68 lines (59 loc) · 1.49 KB
/
8.linq
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
<Query Kind="Program" />
#load ".\shared"
void Main()
{
var inputStrings = Utils.ParseStrings("8.txt");
List<Instruction> defaultInstructions = inputStrings.Select(s =>
{
string[] split = s.Split(" ", StringSplitOptions.None);
return new Instruction { Op = split[0], N = int.Parse(split[1]) };
}).ToList();
int p1;
Process(defaultInstructions, out p1);
$"P1: {p1}".Dump();
for (int i = 0; i < defaultInstructions.Count; i++)
{
var modifiedInstructions = defaultInstructions.ToList();
var modifiedInstruction = modifiedInstructions[i];
if (modifiedInstruction.Op == "acc") continue;
string newOp = modifiedInstruction.Op == "jmp" ? "nop" : "jmp";
modifiedInstructions[i] = modifiedInstructions[i] with { Op = newOp };
int p2;
bool completed = Process(modifiedInstructions, out p2);
if (completed)
{
$"P2: {p2}".Dump();
return;
}
}
bool Process(List<Instruction> currentInstructions, out int output)
{
HashSet<int> seenInstructions = new();
output = 0;
for (int i = 0; i >= 0 && i < currentInstructions.Count;)
{
if (!seenInstructions.Contains(i)) seenInstructions.Add(i);
else return false;
Instruction instruction = currentInstructions[i];
switch (instruction.Op)
{
case "nop":
i++;
break;
case "acc":
i++;
output += instruction.N;
break;
case "jmp":
i += instruction.N;
break;
}
}
return true;
}
}
public record Instruction
{
public string Op { get; init; }
public int N { get; init; }
}