-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.php
54 lines (39 loc) · 942 Bytes
/
day10.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
<?php
include(__DIR__."/_loader.php");
$input = InputHelper::loadFile("datasets/day10.txt");
// Part 1
$answer = 0;
$X = 1;
$cycles = 1;
function cycle($cycles, $X) {
if (in_array($cycles, [20,60,100,140,180,220])) return $cycles*$X;
return 0;
}
foreach ($input as $line) {
$answer += cycle($cycles, $X);
$cycles++;
if ($line == 'noop') continue;
$answer += cycle($cycles, $X);
$cycles++;
$inc = str_replace('addx ', '', $line);
$X += $inc;
}
echo "Answer #1: ".$answer.PHP_EOL;
// Part 2
$X = 1;
$cycles = 1;
echo "Answer #2: ".PHP_EOL;
function display($cycles, $X) {
$diff = abs(($cycles-1)%40 - $X);
echo ($diff <= 1) ? '#' : '.';
if ($cycles%40 == 0) echo PHP_EOL;
}
foreach ($input as $line) {
display($cycles, $X);
$cycles++;
if ($line == 'noop') continue;
display($cycles, $X);
$cycles++;
$inc = str_replace('addx ', '', $line);
$X += $inc;
}