-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-25.php
164 lines (132 loc) · 3.7 KB
/
day-25.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
require_once __DIR__ . '/utils.php';
$input = trim(file_get_contents(__DIR__ . '/input/day-25'));
$input = explode(',', $input);
$start = microtime(true);
$consoleInput = fopen('php://stdin', 'r');
$backMove = [
'east' => 'west',
'west' => 'east',
'north' => 'south',
'south' => 'north',
];
$gotAllItems = false;
$move = '';
$drone = new Drone(new IntCodeParser($input));
$drone->execute();
while (true) {
$drone->pickWhiteListedItems();
if ($drone->see( 'Pressure-Sensitive Floor')) {
echo 'Part 1: ' . $drone->getDigits() . PHP_EOL;
break;
}
if ($drone->see('Security Checkpoint') && !$gotAllItems) {
$vertex->finished = true;
$vertex->visited = true;
$move = $backMove[$move];
$vertex = $vertex->moves[$move];
$drone->command($move);
continue;
}
$vertex ??= new Vertex();
$vertex->visited = true;
foreach (['east', 'west', 'north', 'south'] as $move) {
if ($drone->see($move . "\n")) {
if (!isset($vertex->moves[$move])) {
$vertex->moves[$move] = new Vertex();
$vertex->moves[$move]->moves[$backMove[$move]] = $vertex;
}
}
}
if (count($vertex->moves) === 1) {
$vertex->finished = true;
$move = array_key_first($vertex->moves);
$vertex = $vertex->moves[$move];
$drone->command($move);
continue;
}
foreach ($vertex->moves as $possibleMove => $targetVertex) {
if ($targetVertex->finished) continue;
if (!$targetVertex->visited) {
$foundNew = true;
$move = $possibleMove;
$vertex = $targetVertex;
$drone->command($move);
continue 2;
}
}
$unblocked = 0;
foreach ($vertex->moves as $targetVertex) {
if (!$targetVertex->finished) $unblocked++;
}
if ($unblocked === 1) {
$vertex->finished = true;
} elseif ($unblocked === 0) {
$gotAllItems = true;
$vertex = null;
continue;
}
foreach ($vertex->moves as $possibleMove => $targetVertex) {
if ($targetVertex->finished) continue;
$move = $possibleMove;
$vertex = $targetVertex;
$drone->command($move);
break;
}
}
fclose($consoleInput);
echo 'Finished in ' . (microtime(true) - $start) . PHP_EOL;
class Drone
{
private IntCodeParser $computer;
private string $lastOutput;
const WHITELIST_ITEMS = [
'mug',
'coin',
'hypercube',
'astrolabe',
];
public function __construct(IntCodeParser $computer)
{
$this->computer = $computer;
}
public function command(string $command): void
{
foreach (str_split($command) as $char) {
$this->computer->input(ord($char));
}
$this->computer->input(ord("\n"));
$this->execute();
}
public function getDigits(): string
{
preg_match('/\d+/', $this->lastOutput, $matches);
return $matches[0];
}
public function execute(): void
{
$this->lastOutput = '';
while ($output = $this->computer->output()) {
$this->lastOutput .= chr($output);
}
}
public function see(string $something): bool
{
return strpos($this->lastOutput, $something) !== false;
}
public function pickWhiteListedItems()
{
foreach (self::WHITELIST_ITEMS as $item) {
if (strpos($this->lastOutput, '- ' . $item)) {
$this->command('take ' . $item);
break;
}
}
}
}
class Vertex
{
public array $moves = [];
public bool $finished = false;
public bool $visited = false;
}