Skip to content

Commit 4f070e3

Browse files
author
Adrian Reimann
committed
solution for day 12 part 2
1 parent bb5b333 commit 4f070e3

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed

day_12/12-2.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
//names of start and end node of the graph
4+
const CAVE_START = 'start';
5+
const CAVE_END = 'end';
6+
7+
//read input and split it into links for the cave "graph"
8+
$node_links = [];
9+
$input = fopen("input.txt", "r");
10+
while (($input_line = fgets($input)) !== false) {
11+
$link = explode('-', trim($input_line)); //split into links
12+
$node_links[$link[0]][] = $link[1]; //add link b to possible links from a
13+
$node_links[$link[1]][] = $link[0]; //add link a to possible links from b
14+
}
15+
fclose($input);
16+
17+
//trigger recursive depth search for every node from 'start':
18+
findDepth(CAVE_START, $node_links,$paths);
19+
20+
//result
21+
echo "There are " . count($paths) . " paths through the cave system." . PHP_EOL;
22+
23+
24+
/**
25+
* Recursively loop through the $node_links, and, from a given $node, find subsequent paths
26+
*
27+
* @author Adrian
28+
* @date_created 2021-12-13
29+
*
30+
* @param $node //current node to find paths from
31+
* @param $node_links //"map" or "graph" of all nodes and where they link to
32+
* @param $found_paths //referenced variable providing a list of all found paths from start to end
33+
* @param array $visited //current path, checked against to ensure small caves are only visited once
34+
*
35+
* @return bool
36+
*/
37+
function findDepth($node, $node_links, &$found_paths, $visited = []) {
38+
39+
//add node to the path
40+
$visited[] = $node;
41+
42+
//if we found the end node here, add to found paths
43+
if($node == CAVE_END) {
44+
$found_paths[] = $visited;
45+
return true;
46+
}
47+
48+
//find all linked nodes, and recursively "go deeper" for them
49+
foreach ($node_links[$node] as $node_link) {
50+
//for lowercase only keep going if it hasn't already been visited or no small cave has been visited twice (if the unique count of small caves is not the same as total count of small caves in the path)
51+
$small_caves = array_filter($visited, 'ctype_lower');
52+
if($node_link === CAVE_START || (ctype_lower($node_link) && in_array($node_link, $visited) && count(array_unique($small_caves)) < count($small_caves))) {
53+
continue;
54+
}
55+
56+
findDepth($node_link, $node_links, $found_paths, $visited);
57+
}
58+
59+
//return $found_paths
60+
return $found_paths;
61+
}

day_12/puzzle.txt

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
--- Day 12: Passage Pathing ---
2+
3+
With your submarine's subterranean subsystems subsisting suboptimally, the only way you're getting out of this cave anytime soon is by finding a path yourself. Not just a path - the only way to know if you've found the best path is to find all of them.
4+
5+
Fortunately, the sensors are still mostly working, and so you build a rough map of the remaining caves (your puzzle input). For example:
6+
7+
start-A
8+
start-b
9+
A-c
10+
A-b
11+
b-d
12+
A-end
13+
b-end
14+
15+
This is a list of how all of the caves are connected. You start in the cave named start, and your destination is the cave named end. An entry like b-d means that cave b is connected to cave d - that is, you can move between them.
16+
17+
So, the above cave system looks roughly like this:
18+
19+
start
20+
/ \
21+
c--A-----b--d
22+
\ /
23+
end
24+
25+
Your goal is to find the number of distinct paths that start at start, end at end, and don't visit small caves more than once. There are two types of caves: big caves (written in uppercase, like A) and small caves (written in lowercase, like b). It would be a waste of time to visit any small cave more than once, but big caves are large enough that it might be worth visiting them multiple times. So, all paths you find should visit small caves at most once, and can visit big caves any number of times.
26+
27+
Given these rules, there are 10 paths through this example cave system:
28+
29+
start,A,b,A,c,A,end
30+
start,A,b,A,end
31+
start,A,b,end
32+
start,A,c,A,b,A,end
33+
start,A,c,A,b,end
34+
start,A,c,A,end
35+
start,A,end
36+
start,b,A,c,A,end
37+
start,b,A,end
38+
start,b,end
39+
40+
(Each line in the above list corresponds to a single path; the caves visited by that path are listed in the order they are visited and separated by commas.)
41+
42+
Note that in this cave system, cave d is never visited by any path: to do so, cave b would need to be visited twice (once on the way to cave d and a second time when returning from cave d), and since cave b is small, this is not allowed.
43+
44+
Here is a slightly larger example:
45+
46+
dc-end
47+
HN-start
48+
start-kj
49+
dc-start
50+
dc-HN
51+
LN-dc
52+
HN-end
53+
kj-sa
54+
kj-HN
55+
kj-dc
56+
57+
The 19 paths through it are as follows:
58+
59+
start,HN,dc,HN,end
60+
start,HN,dc,HN,kj,HN,end
61+
start,HN,dc,end
62+
start,HN,dc,kj,HN,end
63+
start,HN,end
64+
start,HN,kj,HN,dc,HN,end
65+
start,HN,kj,HN,dc,end
66+
start,HN,kj,HN,end
67+
start,HN,kj,dc,HN,end
68+
start,HN,kj,dc,end
69+
start,dc,HN,end
70+
start,dc,HN,kj,HN,end
71+
start,dc,end
72+
start,dc,kj,HN,end
73+
start,kj,HN,dc,HN,end
74+
start,kj,HN,dc,end
75+
start,kj,HN,end
76+
start,kj,dc,HN,end
77+
start,kj,dc,end
78+
79+
Finally, this even larger example has 226 paths through it:
80+
81+
fs-end
82+
he-DX
83+
fs-he
84+
start-DX
85+
pj-DX
86+
end-zg
87+
zg-sl
88+
zg-pj
89+
pj-he
90+
RW-he
91+
fs-DX
92+
pj-RW
93+
zg-RW
94+
start-pj
95+
he-WI
96+
zg-he
97+
pj-fs
98+
start-RW
99+
100+
How many paths through this cave system are there that visit small caves at most once?
101+
102+
Your puzzle answer was 4773.
103+
104+
--- Part Two ---
105+
106+
After reviewing the available paths, you realize you might have time to visit a single small cave twice. Specifically, big caves can be visited any number of times, a single small cave can be visited at most twice, and the remaining small caves can be visited at most once. However, the caves named start and end can only be visited exactly once each: once you leave the start cave, you may not return to it, and once you reach the end cave, the path must end immediately.
107+
108+
Now, the 36 possible paths through the first example above are:
109+
110+
start,A,b,A,b,A,c,A,end
111+
start,A,b,A,b,A,end
112+
start,A,b,A,b,end
113+
start,A,b,A,c,A,b,A,end
114+
start,A,b,A,c,A,b,end
115+
start,A,b,A,c,A,c,A,end
116+
start,A,b,A,c,A,end
117+
start,A,b,A,end
118+
start,A,b,d,b,A,c,A,end
119+
start,A,b,d,b,A,end
120+
start,A,b,d,b,end
121+
start,A,b,end
122+
start,A,c,A,b,A,b,A,end
123+
start,A,c,A,b,A,b,end
124+
start,A,c,A,b,A,c,A,end
125+
start,A,c,A,b,A,end
126+
start,A,c,A,b,d,b,A,end
127+
start,A,c,A,b,d,b,end
128+
start,A,c,A,b,end
129+
start,A,c,A,c,A,b,A,end
130+
start,A,c,A,c,A,b,end
131+
start,A,c,A,c,A,end
132+
start,A,c,A,end
133+
start,A,end
134+
start,b,A,b,A,c,A,end
135+
start,b,A,b,A,end
136+
start,b,A,b,end
137+
start,b,A,c,A,b,A,end
138+
start,b,A,c,A,b,end
139+
start,b,A,c,A,c,A,end
140+
start,b,A,c,A,end
141+
start,b,A,end
142+
start,b,d,b,A,c,A,end
143+
start,b,d,b,A,end
144+
start,b,d,b,end
145+
start,b,end
146+
147+
The slightly larger example above now has 103 paths through it, and the even larger example now has 3509 paths through it.
148+
149+
Given these new rules, how many paths through this cave system are there?
150+
151+
Your puzzle answer was 116985.

0 commit comments

Comments
 (0)