-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6.php
More file actions
executable file
·71 lines (56 loc) · 1.75 KB
/
Copy pathday6.php
File metadata and controls
executable file
·71 lines (56 loc) · 1.75 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
<?php
ini_set("memory_limit","256M");
$inputFile = fopen("input6.txt",'r');
$grid = array();
while ($row = trim(fgets($inputFile))) {
$row = str_replace('toggle', 'toggle it', $row);
$order = explode(' ', $row);
$startCoord = explode(',',$order[2]);
$startX = $startCoord[0];
$startY = $startCoord[1];
$stopCoord = explode(',',$order[4]);
$stopX = $stopCoord[0];
$stopY = $stopCoord[1];
for ($i = $startX; $i <= $stopX; $i++) {
for ($j= $startY; $j <= $stopY; $j++) {
switch ($order[0] . $order[1]) {
case 'turnon':
if (isset($grid[$i][$j])) {
$grid[$i][$j] = $grid[$i][$j] + 1;
} else {
$grid[$i][$j] = 1;
};
break;
case 'turnoff':
// $grid[$i][$j] = false;
if(isset($grid[$i][$j])) {
$grid[$i][$j] = $grid[$i][$j] - 1;
if ($grid[$i][$j] <= 0) {
unset($grid[$i][$j]);
}
}
break;
case 'toggleit':
if (isset($grid[$i][$j])) {
$grid[$i][$j] = $grid[$i][$j] + 2;
} else {
$grid[$i][$j] = 2;
}
break;
default:
throw new \RuntimeException(":(");
break;
}
}
}
}
$lit = 0;
for ($i=0; $i < 1000; $i++) {
for ($j=0; $j < 1000; $j++) {
if (isset($grid[$i][$j])) {
$lit = $lit + $grid[$i][$j];
};
}
}
var_dump($lit);
fclose($inputFile);