From 013c76a977c11f8c049152a6edb455c77caa1e9d Mon Sep 17 00:00:00 2001 From: Pierre Jeanjean Date: Sun, 12 Dec 2021 11:28:43 +0100 Subject: [PATCH] Day 12 --- .gitignore | 1 + 12/01.sh | 32 ++++++++++++++++++++++++++++++++ 12/02.sh | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100755 12/01.sh create mode 100755 12/02.sh diff --git a/.gitignore b/.gitignore index ce5c312..d146596 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ **/input.txt +**/example.txt diff --git a/12/01.sh b/12/01.sh new file mode 100755 index 0000000..33ccfca --- /dev/null +++ b/12/01.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +declare -A transitions +while read line; do + first=$(sed 's/-.*$//' <<< $line) + last=$(sed 's/^.*-//' <<< $line) + transitions[$first]=${transitions[$first]}','$last',' + transitions[$last]=${transitions[$last]}','$first',' +done + +paths=() + +find_paths() { + local current_path=( ${1//,/ } ) + local marked_caves=$2 + local current_transitions=${transitions[${current_path[-1]}]} + current_transitions=( ${current_transitions//,/ } ) + local current_transition + for current_transition in ${current_transitions[@]}; do + if [[ $current_transition == 'end' ]]; then + paths+=( $1','$current_transition',' ) + elif [[ ${current_transition:0:1} == [A-Z] ]]; then + find_paths $1','$current_transition',' $marked_caves + elif ! [[ $marked_caves == *','$current_transition','* ]]; then + find_paths $1','$current_transition',' $marked_caves','$current_transition',' + fi + done +} + +find_paths ',start,' ',start,' + +echo ${#paths[@]} diff --git a/12/02.sh b/12/02.sh new file mode 100755 index 0000000..a2aeefb --- /dev/null +++ b/12/02.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +declare -A transitions +while read line; do + first=$(sed 's/-.*$//' <<< $line) + last=$(sed 's/^.*-//' <<< $line) + transitions[$first]=${transitions[$first]}','$last',' + transitions[$last]=${transitions[$last]}','$first',' +done + +paths=() + +find_paths() { + local current_path=( ${1//,/ } ) + local marked_caves=$2 + local visited_twice=$3 + local current_transitions=${transitions[${current_path[-1]}]} + current_transitions=( ${current_transitions//,/ } ) + local current_transition + for current_transition in ${current_transitions[@]}; do + if [[ $current_transition == 'end' ]]; then + paths+=( $1','$current_transition',' ) + elif [[ ${current_transition:0:1} == [A-Z] ]]; then + find_paths $1','$current_transition',' $marked_caves $visited_twice + elif ! [[ $marked_caves == *','$current_transition','* ]]; then + find_paths $1','$current_transition',' $marked_caves','$current_transition',' $visited_twice + elif [ $visited_twice -eq 0 ] && ! [[ $current_transition == 'start' ]]; then + find_paths $1','$current_transition',' $marked_caves 1 + fi + done +} + +find_paths ',start,' ',start,' 0 + +echo ${#paths[@]}