-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.php
210 lines (164 loc) · 5.02 KB
/
day14.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
include(__DIR__."/_loader.php");
$input = InputHelper::loadFile("datasets/day14.txt");
// null: hide all the unnecessary output
// "map": show only the map evolution
// "debug": verbose everything
$verbose = null;
// null: no timer (the fastest)
// nb microseconds, eg:
// 150000: 0.15s delay between each tick
$usleep = null;
// Part 1
$answer = 0;
$map = new Map($input);
$map->display();
$status = 0;
while ($status >= 0) {
$answer++;
$status = $map->drop(500,0);
$map->display();
if (! is_null($usleep)) usleep($usleep);
}
echo "Answer #1: ".($answer-1).PHP_EOL;
// Part 2
$answer = 0;
$map = new Map($input);
$map->addFloor();
$map->display();
$map->allowExtend = true;
$status = 0;
while ($status >= 0) {
$answer++;
$status = $map->drop(500,0);
$map->display();
if (! is_null($usleep)) usleep($usleep);
}
echo "Answer #2: ".($answer-1).PHP_EOL;
class Map {
public $map = [];
public $height = 0;
public $allowExtend = false;
public function __construct($input) {
$this->map = [];
$blocks = [];
$minY = 0;
$minX = 1000000;
$maxY = $maxX = 0;
// Reading all the blocks of rocks
foreach ($input as $line) {
$rocks = [];
$line = str_replace(' -> ', ' ', $line);
$coords = explode(' ', $line);
foreach ($coords as $coord) $rocks[] = explode(',', $coord);
$blocks[] = $rocks;
}
// Parsing the blocks to set the map size
foreach ($blocks as $rocks) {
foreach ($rocks as $rock) {
list($x, $y) = $rock;
$minX = min($minX, $x);
$maxY = max($maxY, $y);
$maxX = max($maxX, $x);
}
}
$this->height = $maxY;
// Creating the empty map
for ($y=$minY; $y<=$maxY; $y++) {
$this->map[$y] = [];
for ($x=$minX; $x<=$maxX; $x++) $this->map[$y][$x] = '.';
}
// Adding the rocks
foreach ($blocks as $rocks) {
for ($r=0; $r<(count($rocks)-1); $r++) {
list($x, $y) = $rocks[$r];
list($x2, $y2) = $rocks[($r+1)];
if ($x == $x2) {
$c = min($y, $y2);
$max = max($y, $y2);
for (;$c<=$max;$c++) $this->map[$c][$x] = '#';
} else {
$c = min($x, $x2);
$max = max($x, $x2);
for (;$c<=$max;$c++) $this->map[$y][$c] = '#';
}
}
}
// Adding the sand generator
$this->map[0][500] = '+';
}
public function display() {
global $verbose;
if (is_null($verbose)) return;
foreach ($this->map as $y => $xs) {
ksort($xs);
echo str_pad($y, 3, '0', STR_PAD_LEFT)." ";
foreach ($xs as $x => $s) {
echo $s;
}
echo PHP_EOL;
}
echo PHP_EOL;
}
/**
* @return $status :
* -1 : Out of bound
* 0 : Location [$x,$y] not free
* 1 : OK, sand added.
*/
public function drop($x, $y, $sub=0) {
verbose("drop({$x}, {$y});");
// Out of bound
if (! isset($this->map[$y])) return -1;
if (! isset($this->map[$y][$x])) {
$ret = $this->extend($x); // for PART 2
if ($ret == -1) return -1;
}
// Location not free
if (! in_array($this->map[$y][$x], ['.','+'])) {
if ($sub) return 0;
return -1;
}
// straight until rock or map border
for (; $y < ($this->height - 1); $y++) {
if (in_array($this->map[($y+1)][$x], ['.','+'])) continue;
break;
}
verbose("down to [{$x}, {$y}]");
// trying left
verbose("Trying left");
$left = $this->drop($x-1, $y+1, 1);
if ($left == -1) return -1;
if ($left == 1) return 1;
// trying right
verbose("Trying right");
$right = $this->drop($x+1, $y+1, 1);
if ($right == -1) return -1;
if ($right == 1) return 1;
verbose("Placing sand to [{$x}, {$y}]");
$this->map[$y][$x] = 'o';
return 1;
}
// -------- PART 2 methods ----------
public function addFloor() {
$xs = array_keys($this->map[0]);
$this->height++;
$this->map[$this->height] = [];
foreach ($xs as $x) $this->map[$this->height][$x] = '.';
$this->height++;
$this->map[$this->height] = [];
foreach ($xs as $x) $this->map[$this->height][$x] = '#';
}
public function extend($x) {
verbose("extend({$x});");
if (! $this->allowExtend) return -1;
for ($y=0; $y<$this->height; $y++) $this->map[$y][$x] = '.';
$this->map[$y][$x] = '#';
return 0;
}
}
function verbose($msg) {
global $verbose;
if ($verbose != 'debug') return;
echo $msg.PHP_EOL;
}