-
Notifications
You must be signed in to change notification settings - Fork 0
/
d03.php
76 lines (62 loc) · 1.31 KB
/
d03.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
<?php
function part_1()
{
$delivered = 0;
$houses = [];
$x = $y = 0;
$fh = fopen('input_d03.txt', 'r');
while (false !== ($char = fgetc($fh))) {
if (!isset($houses[$x][$y])) {
$houses[$x][$y] = 1;
$delivered++;
}
move($char, $x, $y);
}
var_dump($delivered);
}
function part_2()
{
$delivered = 0;
$houses = [];
$step = 0;
$sx = $sy = 0; // Position of Santa
$rx = $ry = 0; // Position of Robo-Santa
$fh = fopen('input_d03.txt', 'r');
while (false !== ($char = fgetc($fh))) {
if ($step % 2 === 0) { // Santa
$x = $sx;
$y = $sy;
} else { // Robo-Santa
$x = $rx;
$y = $ry;
}
if (!isset($houses[$x][$y])) {
$houses[$x][$y] = 1;
$delivered++;
}
if ($step % 2 === 0) move($char, $sx, $sy);
else move($char, $rx, $ry);
$step++;
}
var_dump($delivered);
}
function move($char, &$x, &$y)
{
// Move to next house
switch ($char) {
case '^':
$y--;
break;
case '>':
$x++;
break;
case 'v':
$y++;
break;
case '<':
$x--;
break;
}
}
part_1();
part_2();