-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.chpl
81 lines (72 loc) · 1.99 KB
/
day10.chpl
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
use IO, Map;
var numCycles : map(string,int);
numCycles["addx"] = 2;
numCycles["noop"] = 1;
iter computeAndReadValues(whenToRead : [] int) {
var line : string;
var x = 1; // register we are computing with
var cycleCount = 1; // cycles start at 1
var readCount = 0; // how many values we have read so far
while readLine(line, stripNewline=true)
&& readCount<whenToRead.size {
var lineArray = line.split();
var cmd = lineArray[0];
// determine if we should read this cycle
for 1..#numCycles[cmd] {
if cycleCount == whenToRead[readCount] {
readCount += 1;
yield x; // reading register value
}
cycleCount += 1;
} // NOTE: when this curly brace was missing got an error
// at the end of the file, which isn't helpful
// Do command
if cmd=="addx" {
var operand = lineArray[1];
x += operand : int;
}
}
}
const whenToRead = [20, 60, 100, 140, 180, 220];
//var x = computeAndReadValues(whenToRead);
//writeln("x = ",x);
//writeln("x*whenToRead = ", x*whenToRead);
//writeln("answer = ", + reduce (x*whenToRead));
//---- Part 2
iter computeXeveryCycle() {
var X = 1; // register we are computing with
var cycleCount = 1; // cycles start at 1
var line : string;
while readLine(line, stripNewline=true) {
var lineArray = line.split();
var cmd = lineArray[0];
// Do command
if cmd=="addx" {
yield X;
yield X;
var operand = lineArray[1];
X += operand : int;
cycleCount += 2;
} else {
yield X;
cycleCount += 1;
}
}
}
var valuesForX = computeXeveryCycle();
writeln(valuesForX);
writeln(valuesForX.size);
var D = {0..5,0..39};
writeln("D.size = ", D.size);
var CRT : [D] string;
for ((y,x), X) in zip(D, valuesForX[0..#D.size]) {
writeln("y,x, X = (", x, ", ", y, "), ", X);
if X-1 <= x && x <= X+1 {
CRT[y,x] = "#";
} else {
CRT[y,x] = ".";
}
writeln("CRT[y] = ", CRT[y,..]);
}
writeln();
writeln(CRT);