-
Notifications
You must be signed in to change notification settings - Fork 0
/
crt.php
executable file
·86 lines (70 loc) · 1.55 KB
/
crt.php
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
#!/usr/bin/php
<?php
interface Instruction {
function getCycleCount(): int;
function onFinished(&$register): void;
}
class NoopInstruction implements Instruction {
public function getCycleCount(): int {
return 1;
}
public function onFinished(&$register): void {
// do nothing
}
}
class AddxInstruction implements Instruction {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getCycleCount(): int {
return 2;
}
public function onFinished(&$register): void {
$register += $this->value;
}
}
$program = [];
while(($line = fgets(STDIN)) !== false) {
$line = trim($line);
if($line === "") continue;
$cmd = substr($line, 0, 4);
$arg = substr($line, 5);
if($cmd === "noop") {
// could have just one instance of noop and re-use it, but whatever
$program[] = new NoopInstruction();
} else if($cmd === "addx") {
$program[] = new AddxInstruction((int)$arg);
}
}
$X = 1;
$p1 = 0;
$p2 = '';
$p2a = [];
$instr = null;
$instrNo = 0;
$instrCycles = 0;
for($cycleNo = 1; $cycleNo <= 240; ++$cycleNo) {
if($instr === null) {
$instr = $program[$instrNo];
$instrNo = ($instrNo + 1) % count($program);
$instrCycles = 0;
}
if(($cycleNo % 40) === 20) {
$p1 += $cycleNo * $X;
}
$pix = ($cycleNo - 1) % 40;
$p2 .= (abs($pix - $X) <= 1) ? '#' : '.';
if($pix == 39) {
$p2a[] = $p2;
$p2 = '';
}
$instrCycles += 1;
if($instrCycles === $instr->getCycleCount()) {
$instr->onFinished($X);
$instr = null;
}
}
echo "Part 1: ", $p1, "\n";
echo "Part 2:\n";
foreach($p2a as $row) echo $row, "\n";