-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd06.php
86 lines (71 loc) · 1.95 KB
/
d06.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
<?php
function setup_lights_1(&$grid, $startX, $startY, $endX, $endY, $action)
{
for ($i = $startX; $i <= $endX; $i++) {
for ($j = $startY; $j <= $endY; $j++) {
switch ($action) {
case 'turn on':
$grid[$i][$j] = 1;
break;
case 'turn off':
$grid[$i][$j] = 0;
break;
case 'toggle':
$grid[$i][$j] ^= 1;
break;
}
}
}
}
function setup_lights_2(&$grid, $startX, $startY, $endX, $endY, $action)
{
for ($i = $startX; $i <= $endX; $i++) {
for ($j = $startY; $j <= $endY; $j++) {
switch ($action) {
case 'turn on':
$grid[$i][$j]++;
break;
case 'turn off':
if ($grid[$i][$j] > 0) $grid[$i][$j]--;
break;
case 'toggle':
$grid[$i][$j] += 2;
break;
}
}
}
}
$lines = file('input_d06.txt', FILE_IGNORE_NEW_LINES);
// Init
for ($i = 0; $i < 1000; $i++) {
for ($j = 0; $j < 1000; $j++) {
$grid1[$i][$j] = 0;
$grid2[$i][$j] = 0;
}
}
foreach ($lines as $line) {
$matches = [];
preg_match(
'/^(turn on|turn off|toggle) (\d{1,3}),(\d{1,3}) through (\d{1,3}),(\d{1,3})$/',
$line,
$matches
);
setup_lights_1($grid1, $matches[2], $matches[3], $matches[4], $matches[5], $matches[1]);
setup_lights_2($grid2, $matches[2], $matches[3], $matches[4], $matches[5], $matches[1]);
}
// Part 1
$on_count = 0;
for ($i = 0; $i < 1000; $i++) {
for ($j = 0; $j < 1000; $j++) {
if ($grid1[$i][$j] === 1) $on_count++;
}
}
var_dump($on_count);
// Part 2
$brightness = 0;
for ($i = 0; $i < 1000; $i++) {
for ($j = 0; $j < 1000; $j++) {
$brightness += $grid2[$i][$j];
}
}
var_dump($brightness);