-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay10a.java
More file actions
87 lines (75 loc) · 1.93 KB
/
Day10a.java
File metadata and controls
87 lines (75 loc) · 1.93 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
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
public class Day10a {
public static String myRead(Scanner scanner) {
String line;
try {
line = scanner.nextLine().trim();
} catch (Exception e) {
line = null;
}
return line;
}
public static Scanner myScanner(File text) {
Scanner scanner;
try {
scanner = new Scanner(text);
} catch (Exception e) {
System.out.println("No Scanner");
return null;
}
return scanner;
}
public static void main(String[] args) {
File text = new File("/Users/axelteo/Desktop/AdventofCode/Day10input.txt");
Scanner scanner = myScanner(text);
if (scanner == null) {
return;
}
String line;
String[] inputs;
CRT curr = new CRT(1, 1);
int sigSum = 0;
while ((line = myRead(scanner)) != null) {
inputs = line.split(" ", 0);
if (inputs[0].equals("noop")) {
sigSum += curr.sigStrength();
curr = curr.noop();
} else {
for (int step = 0; step < 2; ++step) {
sigSum += curr.sigStrength();
curr = curr.addX(step, Integer.valueOf(inputs[1]));
}
}
}
System.out.println("Sum of signal strengths: " + sigSum);
}
}
class CRT {
int xVal;
int clockCycle;
public CRT(int x, int cc) {
this.xVal = x;
this.clockCycle = cc;
}
public int sigStrength() {
if (this.clockCycle <= 220 && this.clockCycle % 40 == 20) {
return this.xVal * this.clockCycle;
}
return 0;
}
// simulates one clock cycle of noop instruction
public CRT noop() {
return new CRT(this.xVal, this.clockCycle + 1);
}
// simulates one clock cycle of a addx instruction
// since addx takes 2 cycles, addX will only occur when cycle is 1
public CRT addX(int cycle, int x) {
if (cycle == 1) {
return new CRT(this.xVal + x, this.clockCycle + 1);
} else {
return new CRT(this.xVal, this.clockCycle + 1);
}
}
}