-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.cs
94 lines (77 loc) · 2.75 KB
/
day10.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
class Day10 {
private static string GetCRTPixel(int cycle, int X) {
if (cycle - 1 <= X && X <= cycle + 1) {
return "#";
} else {
return ".";
}
}
private static void part2(string[] instructions) {
var cycles = 0;
var carryOver = 0;
var X = 1;
var crt = "";
var opCycles = new Dictionary<string,int>() {
{"noop", 1},
{"addx", 2}
};
var lastOperation = "";
foreach(var instruction in instructions) {
var args = instruction.Split(" ");
var operation = args[0];
X += carryOver;
Console.WriteLine($"{string.Join("\n",crt.Chunk(40).Select(c => string.Join("", c)))}");
carryOver = operation switch {
"noop" => 0,
"addx" => Int32.Parse(args[1]),
_ => 0
};
cycles += opCycles[operation];
var crtCycle = (cycles - 1) % 40;
if(operation == "noop") {
crt += GetCRTPixel(crtCycle, X);
} else if (operation == "addx") {
crt += GetCRTPixel(crtCycle - 1, X);
crt += GetCRTPixel(crtCycle, X);
}
}
Console.WriteLine($"Part 2: \n{string.Join("\n",crt.Chunk(40).Select(c => string.Join("", c)))}");
}
private static void part1(string[] instructions) {
var cycles = 0;
var carryOver = 0;
var X = 1;
var signal = 0;
HashSet<int> specialCycles = new() {20,60,100,140,180,220};
int GetSignalForCycle(Func<int, bool> cycleInterval) {
int specialCycle = specialCycles.FirstOrDefault(n => cycleInterval(n));
if (specialCycle != 0) {
specialCycles.Remove(specialCycle);
return specialCycle;
}
return 0;
}
foreach(var instruction in instructions) {
var args = instruction.Split(" ");
var operation = args[0];
var opCycles = 0;
X += carryOver;
if (operation == "noop") {
opCycles = 1;
carryOver = 0;
} else if (operation == "addx") {
opCycles = 2;
carryOver = Int32.Parse(args[1]);
}
cycles += opCycles;
signal += X * GetSignalForCycle((n) => (cycles-opCycles <= n && n <= cycles));
Console.WriteLine($"{instruction} \t- cycles {cycles} - X {X}");
}
Console.WriteLine($"Part 1: {signal}");
}
public static void Run() {
var input = File.ReadAllLines(@"input/10");
//part1(input);
part2(input);
}
}