$count) $ret += $count * get_content_count($rules, $content_color); return $ret + 1; } function get_parent_colors(array $rules, string $color): array { $ret = []; foreach ($rules as $parent_color => $content) if (array_key_exists($color, $content)) $ret = array_merge($ret, [$parent_color], get_parent_colors($rules, $parent_color)); return array_unique($ret); } function parse_bag_rules(array $bag_rules): array { $ret = []; foreach ($bag_rules as $rule) { if (preg_match('/^(([a-z ]+) bags?) contain ([0-9a-z, ]+ bags?)+\.$/', trim($rule), $matches)) { [,, $color, $content] = $matches; if ($content === 'no other bags') $ret[$color] = []; elseif (preg_match_all('/((\d+) ([a-z ]+) bags?)+?,?/', trim($content), $content_matches, PREG_SET_ORDER)) { $parsed_content = []; foreach ($content_matches as $content_match) { [,, $content_qty, $content_color] = $content_match; $parsed_content[$content_color] = $content_qty; } $ret[$color] = $parsed_content; } } } return $ret; }