From: Brett Parker Date: Tue, 14 Dec 2021 02:38:29 +0000 (+0000) Subject: Day 12 X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2021.git/commitdiff_plain/446ce0047d450cbf18efd8b48e9478b24e317772?ds=inline Day 12 --- diff --git a/day12/input.txt b/day12/input.txt new file mode 100644 index 0000000..416c4ba --- /dev/null +++ b/day12/input.txt @@ -0,0 +1,22 @@ +zi-end +XR-start +zk-zi +TS-zk +zw-vl +zk-zw +end-po +ws-zw +TS-ws +po-TS +po-YH +po-xk +zi-ws +zk-end +zi-XR +XR-zk +vl-TS +start-zw +vl-start +XR-zw +XR-vl +XR-ws diff --git a/day12/large-example-p1_226.txt b/day12/large-example-p1_226.txt new file mode 100644 index 0000000..65f3833 --- /dev/null +++ b/day12/large-example-p1_226.txt @@ -0,0 +1,18 @@ +fs-end +he-DX +fs-he +start-DX +pj-DX +end-zg +zg-sl +zg-pj +pj-he +RW-he +fs-DX +pj-RW +zg-RW +start-pj +he-WI +zg-he +pj-fs +start-RW diff --git a/day12/large-example-p2_3509.txt b/day12/large-example-p2_3509.txt new file mode 120000 index 0000000..e8daddc --- /dev/null +++ b/day12/large-example-p2_3509.txt @@ -0,0 +1 @@ +large-example-p1_226.txt \ No newline at end of file diff --git a/day12/mid-example-p1_19.txt b/day12/mid-example-p1_19.txt new file mode 100644 index 0000000..62cc714 --- /dev/null +++ b/day12/mid-example-p1_19.txt @@ -0,0 +1,10 @@ +dc-end +HN-start +start-kj +dc-start +dc-HN +LN-dc +HN-end +kj-sa +kj-HN +kj-dc diff --git a/day12/mid-example-p2_103.txt b/day12/mid-example-p2_103.txt new file mode 120000 index 0000000..d4a04aa --- /dev/null +++ b/day12/mid-example-p2_103.txt @@ -0,0 +1 @@ +mid-example-p1_19.txt \ No newline at end of file diff --git a/day12/pathfinder.sh b/day12/pathfinder.sh new file mode 100755 index 0000000..d93d996 --- /dev/null +++ b/day12/pathfinder.sh @@ -0,0 +1,78 @@ +#!/bin/bash + +set -u + +filename="${1:-small-example-p1_10.txt}" + +exec 3<"$filename" + +declare -A links + +while read -u 3 line; do + a=${line%-*} + b=${line#*-} + + # we actually want it to go both ways, so we'll + # do that + if [ "${links[$a]+abc}" ]; then + links[$a]+=" $b" + else + links[$a]=$b + fi + + if [ "${links[$b]+abc}" ]; then + links[$b]+=" $a" + else + links[$b]=$a + fi +done + +declare -A paths + +do_path() { + local point=$1 + shift + local -a previous_points=("$@") + local -a new_previous_path=() + local path + + local -A __previous_points + + for temp_point in "${previous_points[@]}"; do + __previous_points[$temp_point]=1 + done + + path="${previous_points[@]}" + + if [ "${point}" == "${point,,}" ]; then + # if it's lower case, and we've already been there + # return + if [ "${__previous_points[$point]+abc}" ]; then + return 0 + fi + fi + + if [ "${point}" == "end" ]; then + path="$path $point" + if [ ! "${paths[$path]+abc}" ]; then + paths["$path"]=1 + fi + else + new_previous_path=("${previous_points[@]}") + new_previous_path+=($point) + for new_point in ${links[$point]}; do + do_path $new_point "${new_previous_path[@]}" + done + fi +} + +# start at start and then work through all possible paths +for thing in ${links["start"]}; do + do_path $thing start +done + +for path in "${!paths[@]}"; do + echo "Got path: $path" +done + +echo "Total paths: ${#paths[@]}" diff --git a/day12/pathfinder2.sh b/day12/pathfinder2.sh new file mode 100755 index 0000000..dd66e04 --- /dev/null +++ b/day12/pathfinder2.sh @@ -0,0 +1,109 @@ +#!/bin/bash + +set -u + +filename="${1:-small-example-p2_36.txt}" + +exec 3<"$filename" + +declare -A links + +while read -u 3 line; do + a=${line%-*} + b=${line#*-} + + if [ "$a" == "start" ] || [ "$b" == "end" ]; then + if [ "${links[$a]+abc}" ]; then + links[$a]+=" $b" + else + links[$a]="$b" + fi + continue + fi + + if [ "$a" == "end" ] || [ "$b" == "start" ]; then + if [ "${links[$b]+abc}" ]; then + links[$b]+=" $a" + else + links[$b]="$a" + fi + continue + fi + + # we actually want it to go both ways, so we'll + # do that + if [ "${links[$a]+abc}" ]; then + links[$a]+=" $b" + else + links[$a]="$b" + fi + + if [ "$a" != "start" ]; then + if [ "${links[$b]+abc}" ]; then + links[$b]+=" $a" + else + links[$b]="$a" + fi + fi +done + +declare -A paths=() + +do_path() { + local point=$1 + shift + local -a previous_points=("$@") + local -a new_previous_path=() + local path + + local have_dupe_lowercase=0 + + local -A __previous_points + + for temp_point in "${previous_points[@]}"; do + if [ "${__previous_points[$temp_point]+abc}" ]; then + ((__previous_points[$temp_point]+=1)) + if [ "${temp_point}" == "${temp_point,,}" ]; then + # at least 2 of this point + have_dupe_lowercase=1 + fi + else + __previous_points[$temp_point]=1 + fi + done + + path="${previous_points[@]}" + + if [ "${point}" == "${point,,}" ]; then + if [ "${__previous_points[$point]+abc}" ]; then + if [ $have_dupe_lowercase -eq 1 ]; then + # can only go through one lowercase point twice + return 0 + fi + fi + fi + + if [ "${point}" == "end" ]; then + path="$path $point" + if [ ! "${paths[$path]+abc}" ]; then + paths["$path"]=1 + fi + else + new_previous_path=("${previous_points[@]}") + new_previous_path+=($point) + for new_point in ${links[$point]}; do + do_path $new_point "${new_previous_path[@]}" + done + fi +} + +# start at start and then work through all possible paths +for thing in ${links["start"]}; do + do_path $thing start +done + +for path in "${!paths[@]}"; do + echo "Got path: $path" +done + +echo "Total paths: ${#paths[@]}" diff --git a/day12/paths.txt b/day12/paths.txt new file mode 100644 index 0000000..836be05 --- /dev/null +++ b/day12/paths.txt @@ -0,0 +1,111 @@ +declare -A links=([A]="c b end" [d]="b" [c]="A" [b]="A d end" [start]="A b" ) +declare -A __previous_points=([start]="1" ) +declare -A __previous_points=([A]="1" [start]="1" ) +declare -A __previous_points=([A]="1" [c]="1" [start]="1" ) +declare -A __previous_points=([A]="2" [c]="1" [start]="1" ) +Can't do c on start A c A +declare -A __previous_points=([A]="2" [c]="1" [start]="1" ) +declare -A __previous_points=([A]="2" [c]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="3" [c]="1" [b]="1" [start]="1" ) +Can't do c on start A c A b A +declare -A __previous_points=([A]="3" [c]="1" [b]="1" [start]="1" ) +Can't do b on start A c A b A +declare -A __previous_points=([A]="3" [c]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="2" [c]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="2" [d]="1" [c]="1" [b]="1" [start]="1" ) +Can't do b on start A c A b d +declare -A __previous_points=([A]="2" [c]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="2" [c]="1" [start]="1" ) +declare -A __previous_points=([A]="1" [start]="1" ) +declare -A __previous_points=([A]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="2" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="2" [c]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="3" [c]="1" [b]="1" [start]="1" ) +Can't do c on start A b A c A +declare -A __previous_points=([A]="3" [c]="1" [b]="1" [start]="1" ) +Can't do b on start A b A c A +declare -A __previous_points=([A]="3" [c]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="2" [b]="1" [start]="1" ) +Can't do b on start A b A +declare -A __previous_points=([A]="2" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="1" [d]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="1" [d]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="2" [d]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="2" [d]="1" [c]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="3" [d]="1" [c]="1" [b]="2" [start]="1" ) +Can't do c on start A b d b A c A +declare -A __previous_points=([A]="3" [d]="1" [c]="1" [b]="2" [start]="1" ) +Can't do b on start A b d b A c A +declare -A __previous_points=([A]="3" [d]="1" [c]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="2" [d]="1" [b]="2" [start]="1" ) +Can't do b on start A b d b A +declare -A __previous_points=([A]="2" [d]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="1" [d]="1" [b]="2" [start]="1" ) +Can't do d on start A b d b +declare -A __previous_points=([A]="1" [d]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="1" [start]="1" ) +declare -A __previous_points=([start]="1" ) +declare -A __previous_points=([b]="1" [start]="1" ) +declare -A __previous_points=([A]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="1" [c]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="2" [c]="1" [b]="1" [start]="1" ) +Can't do c on start b A c A +declare -A __previous_points=([A]="2" [c]="1" [b]="1" [start]="1" ) +Can't do b on start b A c A +declare -A __previous_points=([A]="2" [c]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([A]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="2" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="2" [c]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="3" [c]="1" [b]="2" [start]="1" ) +Can't do c on start b A b A c A +declare -A __previous_points=([A]="3" [c]="1" [b]="2" [start]="1" ) +Can't do b on start b A b A c A +declare -A __previous_points=([A]="3" [c]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="2" [b]="2" [start]="1" ) +Can't do b on start b A b A +declare -A __previous_points=([A]="2" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="1" [d]="1" [b]="2" [start]="1" ) +Can't do b on start b A b d +declare -A __previous_points=([A]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([b]="1" [start]="1" ) +declare -A __previous_points=([d]="1" [b]="1" [start]="1" ) +declare -A __previous_points=([d]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="1" [d]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="1" [d]="1" [c]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="2" [d]="1" [c]="1" [b]="2" [start]="1" ) +Can't do c on start b d b A c A +declare -A __previous_points=([A]="2" [d]="1" [c]="1" [b]="2" [start]="1" ) +Can't do b on start b d b A c A +declare -A __previous_points=([A]="2" [d]="1" [c]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([A]="1" [d]="1" [b]="2" [start]="1" ) +Can't do b on start b d b A +declare -A __previous_points=([A]="1" [d]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([d]="1" [b]="2" [start]="1" ) +Can't do d on start b d b +declare -A __previous_points=([d]="1" [b]="2" [start]="1" ) +declare -A __previous_points=([b]="1" [start]="1" ) +Got path: start A b end +Got path: start b A b end +Got path: start A c A b A end +Got path: start b A end +Got path: start b end +Got path: start b d b A end +Got path: start A c A b end +Got path: start A b d b end +Got path: start A b d b A c A end +Got path: start A b d b A end +Got path: start A c A end +Got path: start b A b A end +Got path: start b A b A c A end +Got path: start A b A c A end +Got path: start b d b A c A end +Got path: start b d b end +Got path: start A b A end +Got path: start b A c A end +Got path: start A end +Total paths: 19 diff --git a/day12/small-example-p1_10.txt b/day12/small-example-p1_10.txt new file mode 100644 index 0000000..6fd8c41 --- /dev/null +++ b/day12/small-example-p1_10.txt @@ -0,0 +1,7 @@ +start-A +start-b +A-c +A-b +b-d +A-end +b-end diff --git a/day12/small-example-p2_36.txt b/day12/small-example-p2_36.txt new file mode 120000 index 0000000..fc54951 --- /dev/null +++ b/day12/small-example-p2_36.txt @@ -0,0 +1 @@ +small-example-p1_10.txt \ No newline at end of file diff --git a/day12/task.txt b/day12/task.txt new file mode 100644 index 0000000..e970020 --- /dev/null +++ b/day12/task.txt @@ -0,0 +1,150 @@ +--- Day 12: Passage Pathing --- +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. + +Fortunately, the sensors are still mostly working, and so you build a rough map of the remaining caves (your puzzle input). For example: + +start-A +start-b +A-c +A-b +b-d +A-end +b-end +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. + +So, the above cave system looks roughly like this: + + start + / \ +c--A-----b--d + \ / + end +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. + +Given these rules, there are 10 paths through this example cave system: + +start,A,b,A,c,A,end +start,A,b,A,end +start,A,b,end +start,A,c,A,b,A,end +start,A,c,A,b,end +start,A,c,A,end +start,A,end +start,b,A,c,A,end +start,b,A,end +start,b,end +(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.) + +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. + +Here is a slightly larger example: + +dc-end +HN-start +start-kj +dc-start +dc-HN +LN-dc +HN-end +kj-sa +kj-HN +kj-dc +The 19 paths through it are as follows: + +start,HN,dc,HN,end +start,HN,dc,HN,kj,HN,end +start,HN,dc,end +start,HN,dc,kj,HN,end +start,HN,end +start,HN,kj,HN,dc,HN,end +start,HN,kj,HN,dc,end +start,HN,kj,HN,end +start,HN,kj,dc,HN,end +start,HN,kj,dc,end +start,dc,HN,end +start,dc,HN,kj,HN,end +start,dc,end +start,dc,kj,HN,end +start,kj,HN,dc,HN,end +start,kj,HN,dc,end +start,kj,HN,end +start,kj,dc,HN,end +start,kj,dc,end +Finally, this even larger example has 226 paths through it: + +fs-end +he-DX +fs-he +start-DX +pj-DX +end-zg +zg-sl +zg-pj +pj-he +RW-he +fs-DX +pj-RW +zg-RW +start-pj +he-WI +zg-he +pj-fs +start-RW +How many paths through this cave system are there that visit small caves at most once? + +Your puzzle answer was 3761. + +--- Part Two --- +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. + +Now, the 36 possible paths through the first example above are: + +start,A,b,A,b,A,c,A,end +start,A,b,A,b,A,end +start,A,b,A,b,end +start,A,b,A,c,A,b,A,end +start,A,b,A,c,A,b,end +start,A,b,A,c,A,c,A,end +start,A,b,A,c,A,end +start,A,b,A,end +start,A,b,d,b,A,c,A,end +start,A,b,d,b,A,end +start,A,b,d,b,end +start,A,b,end +start,A,c,A,b,A,b,A,end +start,A,c,A,b,A,b,end +start,A,c,A,b,A,c,A,end +start,A,c,A,b,A,end +start,A,c,A,b,d,b,A,end +start,A,c,A,b,d,b,end +start,A,c,A,b,end +start,A,c,A,c,A,b,A,end +start,A,c,A,c,A,b,end +start,A,c,A,c,A,end +start,A,c,A,end +start,A,end +start,b,A,b,A,c,A,end +start,b,A,b,A,end +start,b,A,b,end +start,b,A,c,A,b,A,end +start,b,A,c,A,b,end +start,b,A,c,A,c,A,end +start,b,A,c,A,end +start,b,A,end +start,b,d,b,A,c,A,end +start,b,d,b,A,end +start,b,d,b,end +start,b,end +The slightly larger example above now has 103 paths through it, and the even larger example now has 3509 paths through it. + +Given these new rules, how many paths through this cave system are there? + +Your puzzle answer was 99138. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle.