-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday07a.php
More file actions
24 lines (19 loc) · 824 Bytes
/
day07a.php
File metadata and controls
24 lines (19 loc) · 824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
$container = ['shiny gold' => []];
preg_replace_callback('#([a-z ]+) bags contain (.+?\.)#', function ($m) use (&$container) {
preg_replace_callback('#(\d+) ([a-z ]+) bags?[\.,]#', function ($mm) use (&$container, $m) {
if (!isset($container[$mm[2]])) $container[$mm[2]] = [];
$container[$mm[2]][] = $m[1];
}, $m[2]);
}, file_get_contents('input07.txt'));
$colors = [];
$scan = function ($color) use (&$scan, &$colors, &$container) {
if (empty($container[$color])) return;
foreach ($container[$color] as $possibleColor) {
if (isset($colors[$possibleColor])) continue;
$colors[$possibleColor] = true;
$scan($possibleColor);
}
};
$scan('shiny gold');
printf("%d\n", count($colors));