Repository navigation
Expand file tree
/
Copy pathDay8.cs
More file actions
72 lines (63 loc) · 1.65 KB
/
Copy pathDay8.cs
File metadata and controls
72 lines (63 loc) · 1.65 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
public static class Day8
{
public static int GetAccValue(string path)
{
var lines = File.ReadAllLines(path).ToArray();
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i];
if (lines[i].StartsWith("nop"))
{
lines[i] = lines[i].Replace("nop", "jmp");
}
else if (lines[i].StartsWith("jmp"))
{
lines[i] = lines[i].Replace("jmp", "nop");
}
else
{
continue;
}
try
{
return GetAccValue(lines);
}
catch (System.Exception)
{
lines[i] = line;
}
}
throw new Exception("should never get here....");
}
private static int GetAccValue(string[] lines)
{
var acc = 0;
var hits = new HashSet<int>();
for (int i = 0; i < lines.Length;)
{
if (hits.Contains(i))
{
throw new Exception();
}
hits.Add(i);
var line = lines[i];
var split = line.Split(' ');
var instruction = split.First();
var amount = split.Last();
switch (instruction)
{
case "acc":
acc += int.Parse(amount);
i++;
break;
case "jmp":
i += int.Parse(amount);
break;
default:
i++;
break;
}
}
return acc;
}
}