-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.php
83 lines (62 loc) · 2.13 KB
/
day9.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
<?php
include(__DIR__."/_loader.php");
$input = InputHelper::loadFile("datasets/day9.txt");
// Part 1
$head = new Knot();
$tail = new Knot();
foreach ($input as $line) {
list($direction, $nb) = explode(' ', $line);
for ($i=0; $i<$nb; $i++) {
$head->move($direction);
$tail->follow($head);
}
}
$answer = count(array_unique($tail->history));
echo "Answer #1: ".$answer.PHP_EOL;
// Part 2
$knots = [];
for ($k=0; $k<10; $k++) $knots[$k] = new Knot();
foreach ($input as $line) {
list($direction, $nb) = explode(' ', $line);
for ($i=0; $i<$nb; $i++) {
$knots[0]->move($direction);
for ($k=1; $k<10; $k++) {
$knots[$k]->follow($knots[($k-1)]);
}
}
}
$answer = count(array_unique($knots[9]->history));
echo "Answer #2: ".$answer.PHP_EOL;
class Knot {
public $x = 0;
public $y = 0;
public $history = ['0_0'];
// "R" for Right -- or "RU" for Right+Up
public function move($directions) {
$directions = str_split($directions);
foreach ($directions as $direction) {
switch ($direction) {
case 'R': $this->x++; break;
case 'L': $this->x--; break;
case 'U': $this->y++; break;
case 'D': $this->y--; break;
}
}
$this->history[] = $this->x."_".$this->y;
}
public function follow($knot) {
$diffX = ($knot->x - $this->x);
$diffY = ($knot->y - $this->y);
if (abs($diffX) <= 1 && abs($diffY) <= 1) return;
// single axis
if ($diffY == 0) $this->move(($diffX == 2) ? 'R' : 'L');
elseif ($diffX == 0) $this->move(($diffY == 2) ? 'U' : 'D');
// diagonal
else {
if ($diffX == 2) $this->move('R' . (($diffY > 0) ? 'U' : 'D') );
elseif ($diffX == -2) $this->move('L' . (($diffY > 0) ? 'U' : 'D') );
elseif ($diffY == 2) $this->move('U' . (($diffX > 0) ? 'R' : 'L') );
elseif ($diffY == -2) $this->move('D' . (($diffX > 0) ? 'R' : 'L') );
}
}
}