Files
advent_of_code_2021/12/part1.php
2021-12-12 21:23:29 +01:00

30 lines
881 B
PHP

<?php
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$input = array_map(fn($line) => explode('-', $line), $input);
$adjacencyList = [];
foreach ($input as $key => $item) {
if ($item[1] != 'start') $adjacencyList[$item[0]][] = $item[1];
if ($item[0] != 'start') $adjacencyList[$item[1]][] = $item[0];
}
$history = [];
echo track($adjacencyList, "start", $history);
function track($adjacencyList, $cave, $history) {
$count = 0;
$history[] = $cave;
foreach($adjacencyList[$cave] as $path) {
if(strtolower($path) === $path && in_array($path,$history)) {
//bad path, go no further
}
else if($path == "end") {
$count += 1;
echo implode(",",$history) ."\n";
}
else {
$count += track($adjacencyList, $path, $history);
}
}
return $count;
}