-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay8.cs
59 lines (56 loc) · 1.34 KB
/
Day8.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
using System;
using System.IO;
(int, bool) Part1(string[] lines)
{
int acc = 0;
bool[] visited = new bool[lines.Length];
int pointer = 0;
while (pointer < lines.Length)
{
if (visited[pointer])
{
return (acc, false);
}
visited[pointer] = true;
string[] parts = lines[pointer].Split(' ');
string type = parts[0];
int arg = int.Parse(parts[1]);
if (type == "acc")
{
acc += arg;
pointer++;
}
else if (type == "jmp")
{
pointer += arg;
}
else
{
pointer++;
}
}
return (acc, true);
}
int Part2(string[] lines)
{
for (int i = 0; i < lines.Length; i++)
{
string[] entry = lines[i].Split(' ');
if (entry[0] == "acc")
{
continue;
}
string oldLine = lines[i];
lines[i] = (entry[0] == "nop" ? "jmp" : "nop") + " " + entry[1];
var (acc, exitNormally) = Part1(lines);
if (exitNormally)
{
return acc;
}
lines[i] = oldLine;
}
return 0;
}
string[] lines = File.ReadAllLines("input.txt");
Console.WriteLine("Part 1: {0}", Part1(lines));
Console.WriteLine("Part 2: {0}", Part2(lines));