-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path19.php
37 lines (30 loc) · 828 Bytes
/
19.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
<?php
$time_start = microtime(true);
$input = trim(file_get_contents('19.txt'));
[$available, $designs] = explode("\n\n", $input);
$available = explode(", ", $available);
$designs = explode("\n", $designs);
function eat(string $design, array $available): int
{
static $cache = [];
if ($design === "") return 1;
if (isset($cache[$design])) return $cache[$design];
$p = 0;
foreach ($available as $a) {
if (str_starts_with($design, $a)) $p += eat(substr($design, strlen($a)), $available);
}
$cache[$design] = $p;
return $p;
}
$pt1 = 0;
$pt2 = 0;
foreach ($designs as $d) {
$p = eat($d, $available);
if ($p) $pt1++;
$pt2 += $p;
}
echo "--- Day 19 ---", PHP_EOL;
echo "Part 1: ", $pt1, PHP_EOL;
echo "Part 2: ", $pt2, PHP_EOL;
echo "Took ", (microtime(true) - $time_start) * 1000, " ms", PHP_EOL;
echo PHP_EOL;