-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay10.java
More file actions
83 lines (68 loc) · 1.6 KB
/
Day10.java
File metadata and controls
83 lines (68 loc) · 1.6 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
import java.util.ArrayList;
public class Day10 {
public static void main(String[] args) {
new Day10();
}
public Day10() {
ArrayList<String> input = ReadInput.read("res/input10.txt");
partOne(input);
partTwo(input);
}
private void partOne(ArrayList<String> input) {
int cycle = 1;
int processingDoneCycle = -1;
int processingValue = 0;
int registryX = 1;
int res = 0;
for(int i = 0; i < input.size(); i++) {
String instruction = input.get(i);
if(cycle % 40 == 20) {
res += cycle * registryX;
}
if(cycle > 220)
break;
if(processingDoneCycle > cycle) {
i--;
cycle++;
registryX += processingValue;
continue;
}
if(instruction.contains("addx")) {
processingValue = Integer.parseInt(instruction.split(" ")[1]);
processingDoneCycle = cycle + 2;
}
cycle++;
}
System.out.println(res);
}
private void partTwo(ArrayList<String> input) {
int cycle = 1;
int processingDoneCycle = -1;
int processingValue = 0;
int registryX = 1;
String output = "";
for(int i = 0; i < input.size(); i++) {
String instruction = input.get(i);
if((cycle - 1) % 40 >= registryX - 1 && (cycle - 1) % 40 <= registryX + 1)
output += "#";
else
output += ".";
if(cycle % 40 == 0)
output += "\n";
if(cycle > 240)
break;
if(processingDoneCycle > cycle) {
i--;
cycle++;
registryX += processingValue;
continue;
}
if(instruction.contains("addx")) {
processingValue = Integer.parseInt(instruction.split(" ")[1]);
processingDoneCycle = cycle + 2;
}
cycle++;
}
System.out.println(output);
}
}