diff --git a/12/input b/12/input new file mode 100644 index 0000000..96b504b --- /dev/null +++ b/12/input @@ -0,0 +1,23 @@ +vn-DD +qm-DD +MV-xy +end-xy +KG-end +end-kw +qm-xy +start-vn +MV-vn +vn-ko +lj-KG +DD-xy +lj-kh +lj-MV +ko-MV +kw-qm +qm-MV +lj-kw +VH-lj +ko-qm +ko-start +MV-start +DD-ko diff --git a/12/part1.php b/12/part1.php new file mode 100644 index 0000000..bf9f145 --- /dev/null +++ b/12/part1.php @@ -0,0 +1,29 @@ + 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; +} + diff --git a/12/part2.php b/12/part2.php new file mode 100644 index 0000000..cbe6b6f --- /dev/null +++ b/12/part2.php @@ -0,0 +1,31 @@ + 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, 0); + +function track($adjacencyList, $cave, $history, $doubled) +{ + $count = 0; + $history[] = $cave; + foreach ($adjacencyList[$cave] as $path) { + //small cave, not already visited + if ($path == "start") { + } elseif (strtolower($path) === $path && in_array($path, $history)) { + if ($doubled == 0) { + $count += track($adjacencyList, $path, $history, 1); + } + } elseif ($path == "end") { + $count += 1; + } else { + $count += track($adjacencyList, $path, $history, $doubled); + } + } + return $count; +} diff --git a/README.md b/README.md index 93cfb09..b673ae2 100644 --- a/README.md +++ b/README.md @@ -53,4 +53,10 @@ ### DAY 11 - Dumbo octopus - [part1](11/part1.php) -- [part2](11/part2.php) \ No newline at end of file +- [part2](11/part2.php) + + +### DAY 12 - Linked caves + +- [part1](12/part1.php) +- [part2](12/part2.php) \ No newline at end of file