-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvent_Code_2022_10.pde
More file actions
150 lines (112 loc) · 2.56 KB
/
Advent_Code_2022_10.pde
File metadata and controls
150 lines (112 loc) · 2.56 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Elf Radio
final boolean F = false;
final boolean T = true;
class anInstruction
{
String instruction;
int value;
anInstruction(String _i, int _v)
{
instruction = _i;
value = _v;
}
}
anInstruction theProg[];
int nofLines;
String fileLines[];
void loadData()
{
String filename;
int lineNo;
filename = "Data_10_03.dat";
fileLines = loadStrings(filename);
println("No of lines" + fileLines.length);
// print the data
for (lineNo = 0 ; lineNo < fileLines.length; lineNo++)
{
print(String.format("L %4d: %s", lineNo, fileLines[lineNo]));
println();
}
}
void scanData()
{
String aLine;
String ary[];
String instruction;
int value;
char cHite;
int hite;
int ix, jx;
nofLines = fileLines.length;
println(String.format("Nof lines %d", nofLines));
theProg = new anInstruction[nofLines];
for (jx = 0; jx < nofLines; jx++){
aLine = fileLines[jx];
ary = split(aLine, " ");
printArray(ary);
instruction = ary[0];
if (ary.length > 1) {value = Integer.parseInt(ary[1]);}
else value = 0;
theProg[jx] = new anInstruction(instruction, value);
print(String.format("S %4d %s %2d",jx, theProg[jx].instruction, theProg[jx].value));
println();
}
}
int totalStr = 0;
void testVal(int inNo, int rX, int cCnt)
{
int strength;
strength = cCnt * rX;
if ((cCnt - 20) % 40 == 0)
{
println(String.format("IXC %4d %3d %4d S %4d", inNo, cCnt, rX, strength));
totalStr += strength;
}
int xChr, yChr;
int iGX, iGY;
int gScale = 10;
xChr = (cCnt - 1) % 40;
yChr = (cCnt - 1) / 40;
iGX = gScale + xChr * gScale;
iGY = gScale + yChr * gScale;
fill(20);
if (rX >= xChr - 1 && rX <= xChr + 1)
{fill(0, 200, 0);}
rect(iGX, iGY, gScale - 1, gScale - 1);
}
int regX;
int cyclCount;
void doCPU()
{
int jx;
regX = 1;
cyclCount = 0;
for (jx = 0; jx < nofLines; jx++){
switch (theProg[jx].instruction){
case "noop":
cyclCount++;
testVal(jx, regX, cyclCount);
break;
case "addx":
cyclCount++;
testVal(jx, regX, cyclCount);
cyclCount++;
testVal(jx, regX, cyclCount);
regX += theProg[jx].value;
break;
}
}
}
void setup()
{
size(500, 300);
frameRate(1);
loadData();
scanData();
background(10);
doCPU();
println(String.format("Total str %10d", totalStr));
}
void draw()
{
}