-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3b.php
75 lines (63 loc) · 1.43 KB
/
3b.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
<?php
declare(strict_types=1);
include 'utilities.php';
$input = (int) input();
function solve(int $puzzle) : int
{
$grid = [];
$x = $y = 0;
$right = $up = $left = $down = 0;
$stepRight = 1;
$stepLeft = 2;
$value = 0;
$neighbors = [
[0, 1],
[1, 1],
[1, 0],
[1, -1],
[0, -1],
[-1, -1],
[-1, 0],
[-1, 1]
];
while ($value <= $puzzle) {
$value = 0;
if ($x === 0 && $y === 0) {
$value = 1;
}
foreach (neighbor($neighbors, $grid, $x, $y) as $i) {
$value += $i;
}
$grid[$y][$x] = $value;
if ($right < $stepRight) {
$right++;
$x++;
} elseif ($up < $stepRight) {
$up++;
$y++;
} elseif ($left < $stepLeft) {
$left++;
$x--;
} elseif ($down < $stepLeft) {
$down++;
$y--;
} else {
$right = 0;
$up = 0;
$left = 0;
$down = 0;
$stepLeft += 2;
$stepRight += 2;
}
}
return $value;
}
function neighbor(array $neighbors, array $grid, int $x, int $y) : generator
{
foreach ($neighbors as $neighbor) {
if (isset($grid[$y + $neighbor[0]][$x + $neighbor[1]])) {
yield $grid[$y + $neighbor[0]][$x + $neighbor[1]];
}
}
}
echo solve($input) . PHP_EOL;