From: Brett Parker Date: Fri, 7 Jan 2022 10:15:27 +0000 (+0000) Subject: Day 15 X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2019.git/commitdiff_plain/refs/heads/main?ds=inline Day 15 --- diff --git a/day15/computer.sh b/day15/computer.sh new file mode 100644 index 0000000..8d7a56e --- /dev/null +++ b/day15/computer.sh @@ -0,0 +1,166 @@ +#!/bin/bash + +declare -a data + +get_value() { + local -n da=$1 + local index=$2 + + if [ $index -lt 0 ]; then + echo "Index negative, give up!" + exit 2 + fi + + if ! [ ${da[$index]+a} ]; then + da[$index]=0 + fi + + declare -g __comp_value="${da[$index]}" +} + +get_param() { + local -n dap=$1 + local pos=$2 + local mode=$3 + local relative_base=$4 + + get_param_loc dap $pos $mode $relative_base + loc=$__comp_param_loc + + get_value dap $loc +} + +get_param_loc() { + local -n dapl=$1 + local pos=$2 + local mode=$3 + local relative_base=$4 + + case $mode in + 0) + get_value dapl $pos + declare -g __comp_param_loc=$__comp_value + ;; + 1) + declare -g __comp_param_loc=$pos + ;; + 2) + get_value dapl $pos + move_by=$__comp_value + declare -g __comp_param_loc=$((relative_base+$move_by)) + ;; + *) + echo "Unknown parameter mode: $mode" + exit 2 + ;; + esac +} + +run_program() { + local -n od=$1 + local relative_base=0 + data=("${od[@]}") + pos=0 + declare -a immediate + while [ $pos -le ${#data[@]} ]; do + # first, printf the value to 5 digits + instruction=$(printf "%05d" ${data[$pos]}) + for (( a=0; a<3; a++ )); do + immediate[$((3-a))]=${instruction:$a:1} + done + instruction=${instruction:3} + param_count=3 + echo -n $'\r'" "$'\r'$instruction >&2 + case $instruction in + 01|02) + # 01 - add, 02 multiply + symbol="+" + if [ $instruction == "02" ]; then + symbol="*" + fi + get_param data $((pos+1)) ${immediate[1]} $relative_base + val1=$__comp_value + get_param data $((pos+2)) ${immediate[2]} $relative_base + val2=$__comp_value + get_param_loc data $((pos+3)) ${immediate[3]} $relative_base + res_loc=$__comp_param_loc + data[$res_loc]=$(($val1 $symbol $val2)) + ;; + 03) + # 03 - read input + echo -n $'\r'" "$'\r' >&2 + echo "input: " + read input + input=${input//\n/} + input=${input//\r/} + echo >&2 + echo "$instruction: '$input'" >&2 + echo >&2 + get_param_loc data $((pos+1)) ${immediate[1]} $relative_base + res_loc=$__comp_param_loc + data[$res_loc]=$input + param_count=1 + ;; + 04) + # 04 - output + get_param data $((pos+1)) ${immediate[1]} $relative_base + val=$__comp_value + echo -n $'\r'" "$'\r' >&2 + echo $val + param_count=1 + ;; + 05|06) + # 05 - jump-if-true, 06 - jump-if-false + get_param data $((pos+1)) ${immediate[1]} $relative_base + val=$__comp_value + get_param data $((pos+2)) ${immediate[2]} $relative_base + jumpto=$__comp_value + param_count=2 + if ( [ $val -ne 0 ] && [ "$instruction" == "05" ] ) || + ( [ $val -eq 0 ] && [ "$instruction" == "06" ] ); then + pos=$jumpto + continue + fi + ;; + 07|08) + # 07 - is less than, 08 - is equal + get_param data $((pos+1)) ${immediate[1]} $relative_base + val1=$__comp_value + get_param data $((pos+2)) ${immediate[2]} $relative_base + val2=$__comp_value + echo >&2 + echo "$instruction: '$val1' '$val2'" >&2 + echo >&2 + get_param_loc data $((pos+3)) ${immediate[3]} $relative_base + res_pos=$__comp_param_loc + data[$res_pos]=0 + if [ "$instruction" == "07" ]; then + if [ $val1 -lt $val2 ]; then + data[$res_pos]=1 + fi + else + if [ $val1 -eq $val2 ]; then + data[$res_pos]=1 + fi + fi + ;; + 09) + # adjust relative base + param_count=1 + get_param data $((pos+1)) ${immediate[1]} $relative_base + val=$__comp_value + relative_base=$((relative_base+$val)) + ;; + 99) + break + ;; + *) + echo "Invalid opcode: $instruction at position $pos" + exit 1 + ;; + esac + pos=$(($pos+$param_count+1)) + done + echo -n $'\r'" "$'\r' >&2 +} + diff --git a/day15/input-google.txt b/day15/input-google.txt new file mode 100644 index 0000000..ac4f004 --- /dev/null +++ b/day15/input-google.txt @@ -0,0 +1 @@ +3,1033,1008,1033,1,1032,1005,1032,31,1008,1033,2,1032,1005,1032,58,1008,1033,3,1032,1005,1032,81,1008,1033,4,1032,1005,1032,104,99,1002,1034,1,1039,1002,1036,1,1041,1001,1035,-1,1040,1008,1038,0,1043,102,-1,1043,1032,1,1037,1032,1042,1106,0,124,1001,1034,0,1039,1002,1036,1,1041,1001,1035,1,1040,1008,1038,0,1043,1,1037,1038,1042,1106,0,124,1001,1034,-1,1039,1008,1036,0,1041,1002,1035,1,1040,1001,1038,0,1043,101,0,1037,1042,1105,1,124,1001,1034,1,1039,1008,1036,0,1041,102,1,1035,1040,1001,1038,0,1043,101,0,1037,1042,1006,1039,217,1006,1040,217,1008,1039,40,1032,1005,1032,217,1008,1040,40,1032,1005,1032,217,1008,1039,39,1032,1006,1032,165,1008,1040,3,1032,1006,1032,165,1102,1,2,1044,1106,0,224,2,1041,1043,1032,1006,1032,179,1102,1,1,1044,1106,0,224,1,1041,1043,1032,1006,1032,217,1,1042,1043,1032,1001,1032,-1,1032,1002,1032,39,1032,1,1032,1039,1032,101,-1,1032,1032,101,252,1032,211,1007,0,59,1044,1105,1,224,1102,1,0,1044,1105,1,224,1006,1044,247,101,0,1039,1034,1001,1040,0,1035,101,0,1041,1036,1002,1043,1,1038,1002,1042,1,1037,4,1044,1105,1,0,93,27,71,56,88,17,30,78,5,57,79,56,3,82,62,58,16,2,21,89,95,33,12,32,90,12,7,76,83,31,8,13,27,89,60,33,7,40,22,50,8,63,35,45,57,94,81,4,65,33,47,73,28,98,11,70,95,17,82,39,19,73,62,56,80,85,23,91,39,86,91,82,50,37,86,4,90,83,8,65,56,63,15,99,51,3,60,60,77,58,90,82,5,52,14,87,37,74,85,43,17,61,91,35,31,81,19,12,34,54,9,66,34,69,67,21,4,14,87,22,76,26,82,79,4,69,48,73,8,73,57,61,83,23,83,60,3,41,75,67,53,44,91,27,52,84,66,13,65,95,81,83,30,26,60,12,33,92,81,46,78,25,13,72,87,26,63,57,35,2,60,96,63,26,2,76,95,21,38,60,5,79,86,89,47,42,12,91,30,52,69,55,67,73,47,44,5,86,8,52,69,81,23,70,3,38,41,89,88,58,41,9,96,27,67,21,14,68,67,35,84,23,20,91,63,47,75,34,70,57,13,54,82,33,61,27,97,88,46,44,56,74,14,5,96,71,16,40,86,61,84,41,81,81,16,88,51,41,96,76,28,97,44,41,65,87,50,73,58,71,46,73,51,43,18,46,99,74,65,9,89,3,77,22,34,93,94,39,54,96,12,35,62,87,56,69,64,9,34,91,64,71,28,10,94,1,96,20,67,92,39,37,26,79,68,16,76,57,83,92,46,75,99,26,64,39,72,65,37,93,65,5,53,62,36,13,97,14,38,85,33,76,56,99,29,64,84,28,19,91,92,55,33,88,32,70,38,53,76,1,76,35,26,75,18,18,7,88,19,53,65,22,91,20,85,15,13,72,82,13,31,75,62,68,4,56,91,89,56,10,46,63,7,74,50,15,85,87,64,77,12,95,10,66,77,51,6,61,75,91,75,85,61,78,4,97,99,4,90,34,89,44,44,68,89,30,20,70,24,22,81,22,77,61,33,89,2,11,75,50,85,13,43,56,78,73,49,27,38,78,56,90,17,94,72,51,5,55,67,32,19,81,81,45,83,18,96,33,75,53,4,29,87,80,33,57,78,80,43,68,57,71,83,10,18,98,70,36,61,31,73,33,69,24,78,76,43,88,96,16,14,91,43,66,15,98,44,48,68,57,72,48,49,89,62,31,55,83,68,86,97,16,25,87,13,74,40,82,43,48,85,40,45,72,33,60,84,4,47,96,19,92,75,73,46,6,69,4,81,98,89,48,55,89,24,64,31,47,50,93,72,47,72,36,79,7,24,66,60,65,18,81,93,40,37,36,62,94,48,8,77,21,82,22,65,20,46,85,47,52,70,55,74,19,65,15,72,81,57,67,46,94,21,16,94,84,36,43,62,82,48,47,79,5,96,39,58,85,80,31,7,98,23,69,22,99,37,69,35,66,36,70,3,69,47,6,64,38,69,42,57,91,89,21,89,13,42,78,24,44,79,74,65,63,85,10,50,71,94,26,78,55,5,26,71,46,20,83,96,51,87,2,99,83,5,38,86,8,13,94,61,93,39,67,23,60,74,87,57,30,72,23,19,95,57,93,83,58,34,83,35,4,47,81,88,24,87,34,93,79,70,18,24,73,98,76,77,24,93,18,66,56,87,25,29,7,7,97,40,61,56,96,96,1,42,21,92,73,11,10,97,69,58,93,2,82,27,96,7,84,44,67,57,63,13,79,56,72,34,89,26,94,24,86,99,71,73,98,26,89,10,98,5,64,70,85,32,61,35,67,0,0,21,21,1,10,1,0,0,0,0,0,0 diff --git a/day15/input.txt b/day15/input.txt new file mode 100644 index 0000000..0fb579e --- /dev/null +++ b/day15/input.txt @@ -0,0 +1 @@ +3,1033,1008,1033,1,1032,1005,1032,31,1008,1033,2,1032,1005,1032,58,1008,1033,3,1032,1005,1032,81,1008,1033,4,1032,1005,1032,104,99,1001,1034,0,1039,1002,1036,1,1041,1001,1035,-1,1040,1008,1038,0,1043,102,-1,1043,1032,1,1037,1032,1042,1105,1,124,1001,1034,0,1039,1001,1036,0,1041,1001,1035,1,1040,1008,1038,0,1043,1,1037,1038,1042,1106,0,124,1001,1034,-1,1039,1008,1036,0,1041,101,0,1035,1040,1001,1038,0,1043,102,1,1037,1042,1105,1,124,1001,1034,1,1039,1008,1036,0,1041,1002,1035,1,1040,101,0,1038,1043,101,0,1037,1042,1006,1039,217,1006,1040,217,1008,1039,40,1032,1005,1032,217,1008,1040,40,1032,1005,1032,217,1008,1039,5,1032,1006,1032,165,1008,1040,35,1032,1006,1032,165,1102,1,2,1044,1106,0,224,2,1041,1043,1032,1006,1032,179,1102,1,1,1044,1105,1,224,1,1041,1043,1032,1006,1032,217,1,1042,1043,1032,1001,1032,-1,1032,1002,1032,39,1032,1,1032,1039,1032,101,-1,1032,1032,101,252,1032,211,1007,0,44,1044,1105,1,224,1102,0,1,1044,1106,0,224,1006,1044,247,1002,1039,1,1034,1001,1040,0,1035,1001,1041,0,1036,1002,1043,1,1038,102,1,1042,1037,4,1044,1105,1,0,5,26,24,17,68,40,71,9,36,46,67,39,48,8,20,23,12,47,28,13,47,2,68,17,71,31,63,31,83,14,78,31,8,33,30,63,30,5,7,11,91,97,17,84,23,37,46,6,14,59,1,76,41,63,85,83,86,63,33,13,50,17,37,16,59,8,7,35,71,9,23,67,46,62,58,38,76,3,71,43,17,64,29,30,72,91,17,70,21,15,76,31,89,20,38,27,65,53,60,34,90,99,56,15,45,57,8,52,70,36,15,79,32,35,83,78,10,3,90,16,74,14,84,43,20,81,91,25,71,83,24,31,92,72,34,59,27,78,6,31,14,31,76,9,80,63,35,40,92,12,84,65,41,27,82,10,7,56,25,70,4,98,16,37,65,46,78,11,97,20,16,95,98,24,31,3,57,74,42,99,36,34,74,10,81,46,43,97,2,24,61,55,13,96,41,41,46,14,64,2,46,94,53,3,3,81,37,85,7,54,29,90,22,75,47,20,26,86,69,53,89,17,2,55,13,85,99,90,2,48,29,66,55,31,19,39,59,56,98,28,38,10,46,10,62,20,63,18,53,97,9,32,6,46,3,91,24,6,62,30,73,26,24,50,3,16,78,3,34,50,8,18,40,65,64,21,28,30,87,45,99,8,21,77,40,73,38,56,12,86,64,43,61,89,4,55,47,28,14,8,99,52,51,40,82,26,19,68,17,53,70,5,14,22,64,69,84,14,69,2,80,18,79,5,66,18,34,48,31,34,54,50,8,33,73,38,52,94,71,7,31,94,31,93,66,82,39,40,42,80,91,70,10,6,50,35,96,13,7,89,22,58,30,24,85,81,88,55,7,58,38,91,55,11,35,84,28,87,26,78,48,66,11,88,8,18,68,55,38,6,1,57,60,1,8,99,58,21,29,88,32,32,57,72,8,20,45,5,91,39,51,59,82,29,52,37,33,49,5,28,38,17,6,58,67,11,72,51,42,4,3,12,94,84,25,31,72,32,89,49,4,23,57,49,27,38,50,30,23,15,80,4,12,67,14,48,76,91,58,11,63,37,95,1,15,22,84,8,23,87,61,32,78,87,7,47,1,81,31,84,91,21,19,68,6,87,3,72,43,60,23,67,42,40,62,9,86,33,84,69,24,97,37,49,24,67,2,16,52,3,42,49,3,95,84,61,8,40,79,10,74,51,6,77,63,1,66,7,55,24,80,68,17,30,47,54,30,77,40,99,18,85,99,85,2,27,18,33,54,99,27,5,64,39,22,66,12,71,29,26,35,49,13,41,22,76,30,70,30,75,34,7,5,62,1,23,61,43,90,24,91,40,42,75,48,40,91,39,46,38,56,17,28,51,56,7,51,40,56,22,87,43,99,6,58,93,35,47,83,10,57,55,68,34,68,93,28,55,11,3,53,80,9,41,42,50,95,7,4,84,10,91,33,12,99,98,60,76,73,24,70,46,72,27,36,62,27,25,43,59,39,9,95,72,9,17,79,36,52,52,22,4,55,57,16,19,65,62,83,11,76,73,37,89,21,86,6,88,17,93,1,59,8,48,73,90,96,10,85,46,12,99,16,16,76,4,2,2,45,62,30,12,14,72,60,9,19,71,43,41,36,99,69,38,1,1,48,32,33,83,26,15,51,19,31,71,92,8,49,34,87,32,80,73,28,65,95,7,8,85,12,63,22,83,8,70,1,82,96,59,29,95,43,59,72,68,38,48,11,87,54,90,11,93,30,63,12,96,41,64,21,89,24,94,73,79,18,55,40,95,0,0,21,21,1,10,1,0,0,0,0,0,0 diff --git a/day15/run.sh b/day15/run.sh new file mode 100755 index 0000000..bfb8d2d --- /dev/null +++ b/day15/run.sh @@ -0,0 +1,312 @@ +#!/bin/bash + +set -u + +filename=${1:-input.txt} +verbose=${2:- 0} +exec 3<"$filename" +OLDIFS=$IFS +IFS="," read -u 3 -a instructions +IFS=$OLDIFS + +basedir=$(dirname $(readlink -f ${BASH_SOURCE})) + +. $basedir/computer.sh + +coproc computer (run_program instructions 2>/dev/null) +comp_pid=${computer_PID} +comp_stdin=${computer[1]} +comp_stdout=${computer[0]} + +cur_x=0 +cur_y=0 +declare -A map=( [$cur_x,$cur_y]=".") +min_x=0 +max_x=0 +min_y=0 +max_y=0 + +refind_home=0 +declare -a oxygen_location=() +declare -A directions=( [n]=1 [s]=2 [w]=3 [e]=4 ) +declare -A direction_names=( [1]="North" [2]="South" [3]="West" [4]="East" ) + +if [ "$verbose" == "0" ]; then + echo_verbose() { echo "$@"; } +else + echo_verbose() { true ; } +fi + +# use left hand rule, if we can go left, go left, if we can't, go straight, if +# that fails, go right, and if that fails, go back on ourselves +cur_direction=${directions[w]} + +change_direction() { + case $cur_direction in + ${directions["n"]}) + cur_direction=${directions["w"]} + ;; + ${directions["e"]}) + cur_direction=${directions["n"]} + ;; + ${directions["s"]}) + cur_direction=${directions["e"]} + ;; + ${directions["w"]}) + cur_direction=${directions["s"]} + ;; + esac +} + +wall_change() { + case $cur_direction in + ${directions["n"]}) + cur_direction=${directions["e"]} + ;; + ${directions["e"]}) + cur_direction=${directions["s"]} + ;; + ${directions["s"]}) + cur_direction=${directions["w"]} + ;; + ${directions["w"]}) + cur_direction=${directions["n"]} + ;; + esac +} + +calc_best_route() { + declare -A tentative=( [0,0]=0 ) + declare -A lowest_cost=() + + total_x=$((max_x - $min_x)) + total_y=$((max_y - $min_y)) + total_points=$((total_x*total_y)) + cur_x=0 + cur_y=0 + + while [ ! "${lowest_cost[${oxygen_location[0]},${oxygen_location[1]}]+abc}" ]; do + echo -ne "\rProgress: " + printf "%2d" $((((${#lowest_cost[@]}*100) / $total_points))) + echo -n "%" + lowest_cost[$cur_x,$cur_y]="${tentative["$cur_x,$cur_y"]}" + unset tentative[$cur_x,$cur_y] + + # go left / right / up / down from our point to get new tentatives + for test_x in $((cur_x-1)) $((cur_x+1)); do + if [ "${map[$test_x,$cur_y]+abc}" ] && [[ "${map[$test_x,$cur_y]}" =~ [\.O] ]]; then + if [ ! "${lowest_cost[$test_x,$cur_y]+abc}" ]; then + new_cost=${lowest_cost[$cur_x,$cur_y]} + ((new_cost+=1)) # always 1 step from where we were previously + if [ "${tentative[$test_x,$cur_y]+abc}" ]; then + if [ $new_cost -lt ${tentative[$test_x,$cur_y]} ]; then + tentative[$test_x,$cur_y]=$new_cost + fi + else + tentative[$test_x,$cur_y]=$new_cost + fi + fi + fi + done + for test_y in $((cur_y-1)) $((cur_y+1)); do + if [ "${map[$cur_x,$test_y]+abc}" ] && [[ "${map[$cur_x,$test_y]}" =~ [\.O] ]]; then + if [ ! "${lowest_cost[$cur_x,$test_y]+abc}" ]; then + new_cost=${lowest_cost[$cur_x,$cur_y]} + ((new_cost+=1)) # always 1 step from where we were previously + if [ "${tentative[$cur_x,$test_y]+abc}" ]; then + if [ $new_cost -lt ${tentative[$cur_x,$test_y]} ]; then + tentative[$cur_x,$test_y]=$new_cost + fi + else + tentative[$cur_x,$test_y]=$new_cost + fi + fi + fi + done + + # find the new lowest tentative + new_location="" + new_value=-1 + + for location in "${!tentative[@]}"; do + if [ -z "$new_location" ]; then + new_location=$location + new_value=${tentative[$location]} + elif [ ${tentative[$location]} -lt $new_value ]; then + new_location=$location + new_value=${tentative[$location]} + fi + done + + if [ -z "$new_location" ]; then + break + fi + cur_x=${new_location%,*} + cur_y=${new_location#*,} + done + echo -e "\rProgress: 100%" + + echo "Lowest route cost: ${lowest_cost[${oxygen_location[0]},${oxygen_location[1]}]}" +} + +# fill the room with oxygen, this could take a few iterations +oxygen_fill() { + rounds=0 + declare -A oxygen_locations=( [${oxygen_location[0]},${oxygen_location[1]}]=0 ) + declare -a spread_points=( "${oxygen_location[0]},${oxygen_location[1]}" ) + declare -a new_spread_points=() + + while [ "${#spread_points[@]}" -gt 0 ]; do + ((rounds+=1)) + for spread_point in "${spread_points[@]}"; do + # check round the point, left, right, up and down, and if they haven't already got O + # add them to the oxygen_locations, and the new_spread_points, if they're on the map + spread_x=${spread_point%,*} + spread_y=${spread_point#*,} + for test_x in $((spread_x-1)) $((spread_x+1)); do + if [ "${map[$test_x,$spread_y]+abc}" ]; then + if [ "${map[$test_x,$spread_y]}" == "." ]; then + if [ ! ${oxygen_locations[$test_x,$spread_y]+abc} ]; then + oxygen_locations[$test_x,$spread_y]=$rounds + new_spread_points+=( "$test_x,$spread_y" ) + fi + fi + fi + done + for test_y in $((spread_y-1)) $((spread_y+1)); do + if [ "${map[$spread_x,$test_y]+abc}" ]; then + if [ "${map[$spread_x,$test_y]}" == "." ]; then + if [ ! "${oxygen_locations[$spread_x,$test_y]+abc}" ]; then + oxygen_locations[$spread_x,$test_y]=$rounds + new_spread_points+=( "$spread_x,$test_y" ) + fi + fi + fi + done + done + if [ ${#new_spread_points[@]} -eq 0 ]; then + ((rounds-=1)) + fi + unset spread_points + declare -a spread_points=() + for spread_point in "${new_spread_points[@]}"; do + spread_points+=( "$spread_point" ) + done + unset new_spread_points + declare -a new_spread_points=() + done + + echo "It'd take $rounds minutes to fill the maze with oxygen" +} + +declare -a spinner=( '-' '\' '|' '/' ) +spinner_offset=0 + +update_spinner() { + echo -ne "\r${spinner[$spinner_offset]}" + ((spinner_offset+=1)) + if [ $spinner_offset -gt $((${#spinner[@]} - 1)) ]; then + spinner_offset=0 + fi +} + +display_map() { + echo "min_x $min_x max_x $max_x min_y $min_y max_y $max_y" + for (( a=$max_y; a >= $min_y; a-- )); do + for (( b=$min_x; b <= $max_x; b++ )); do + marker=${map[$b,$a]:-" "} + if [ $a -eq 0 ] && [ $b -eq 0 ]; then + marker="S" + fi + if [ $a -eq $cur_y ] && [ $b -eq $cur_x ]; then + marker="D" + fi + echo -n "$marker" + done + echo + done + echo +} + +while ps -p $comp_pid > /dev/null 2>/dev/null; do + read -u $comp_stdout prompt + if [ "$prompt" != "input:" ]; then + echo_verbose "We has no input!" + exit 2 + fi + echo $cur_direction >&$comp_stdin + new_x=$cur_x + new_y=$cur_y + case $cur_direction in + ${directions["n"]}) + ((new_y+=1)) + if [ $new_y -gt $max_y ]; then + max_y=$new_y + fi + ;; + ${directions["s"]}) + ((new_y-=1)) + if [ $new_y -lt $min_y ]; then + min_y=$new_y + fi + ;; + ${directions["e"]}) + ((new_x+=1)) + if [ $new_x -gt $max_x ]; then + max_x=$new_x + fi + ;; + ${directions["w"]}) + ((new_x-=1)) + if [ $new_x -lt $min_x ]; then + min_x=$new_x + fi + ;; + esac + echo_verbose "x: $cur_x, new_x: $new_x, y: $cur_y, new_y: $new_y, direction: ${direction_names[$cur_direction]}" + read -u $comp_stdout status + case $status in + "0") + echo_verbose "Hit a wall" + map[$new_x,$new_y]="#" + wall_change + ;; + "1") + echo_verbose "Moved ${direction_names[$cur_direction]}" + if [ ! ${map[$cur_x,$cur_y]+abc} ]; then + map[$cur_x,$cur_y]="." + fi + cur_x=$new_x + cur_y=$new_y + if [ $refind_home -eq 1 ] && [ $new_x -eq 0 ] && [ $new_y -eq 0 ]; then + break + fi + change_direction + ;; + "2") + echo_verbose "Found oxygen system" + map[$cur_x,$cur_y]="." + map[$new_x,$new_y]="O" + cur_x=$new_x + cur_y=$new_y + refind_home=1 + echo -ne '\r \r' + display_map + oxygen_location=( $cur_x $cur_y ) + ;; + *) + echo_verbose "Weird response '$status'" + ;; + esac + update_spinner +done + +echo -ne '\r \r' +display_map + +echo "Calculating best route." + +calc_best_route +oxygen_fill + +echo 0 >&$comp_stdin diff --git a/day15/summary.txt b/day15/summary.txt new file mode 100644 index 0000000..2bcb70f --- /dev/null +++ b/day15/summary.txt @@ -0,0 +1,118 @@ +--- Day 15: Oxygen System --- +Out here in deep space, many things can go wrong. Fortunately, many of those things have indicator lights. Unfortunately, one of those lights is lit: the oxygen system for part of the ship has failed! + +According to the readouts, the oxygen system must have failed days ago after a rupture in oxygen tank two; that section of the ship was automatically sealed once oxygen levels went dangerously low. A single remotely-operated repair droid is your only option for fixing the oxygen system. + +The Elves' care package included an Intcode program (your puzzle input) that you can use to remotely control the repair droid. By running that program, you can direct the repair droid to the oxygen system and fix the problem. + +The remote control program executes the following steps in a loop forever: + +Accept a movement command via an input instruction. +Send the movement command to the repair droid. +Wait for the repair droid to finish the movement operation. +Report on the status of the repair droid via an output instruction. +Only four movement commands are understood: north (1), south (2), west (3), and east (4). Any other command is invalid. The movements differ in direction, but not in distance: in a long enough east-west hallway, a series of commands like 4,4,4,4,3,3,3,3 would leave the repair droid back where it started. + +The repair droid can reply with any of the following status codes: + +0: The repair droid hit a wall. Its position has not changed. +1: The repair droid has moved one step in the requested direction. +2: The repair droid has moved one step in the requested direction; its new position is the location of the oxygen system. +You don't know anything about the area around the repair droid, but you can figure it out by watching the status codes. + +For example, we can draw the area using D for the droid, # for walls, . for locations the droid can traverse, and empty space for unexplored locations. Then, the initial state looks like this: + + + + D + + +To make the droid go north, send it 1. If it replies with 0, you know that location is a wall and that the droid didn't move: + + + # + D + + +To move east, send 4; a reply of 1 means the movement was successful: + + + # + .D + + +Then, perhaps attempts to move north (1), south (2), and east (4) are all met with replies of 0: + + + ## + .D# + # + +Now, you know the repair droid is in a dead end. Backtrack with 3 (which you already know will get a reply of 1 because you already know that location is open): + + + ## + D.# + # + +Then, perhaps west (3) gets a reply of 0, south (2) gets a reply of 1, south again (2) gets a reply of 0, and then west (3) gets a reply of 2: + + + ## + #..# + D.# + # +Now, because of the reply of 2, you know you've found the oxygen system! In this example, it was only 2 moves away from the repair droid's starting position. + +What is the fewest number of movement commands required to move the repair droid from its starting position to the location of the oxygen system? + +Your puzzle answer was 238. + +--- Part Two --- +You quickly repair the oxygen system; oxygen gradually fills the area. + +Oxygen starts in the location containing the repaired oxygen system. It takes one minute for oxygen to spread to all open locations that are adjacent to a location that already contains oxygen. Diagonal locations are not adjacent. + +In the example above, suppose you've used the droid to explore the area fully and have the following map (where locations that currently contain oxygen are marked O): + + ## +#..## +#.#..# +#.O.# + ### +Initially, the only location which contains oxygen is the location of the repaired oxygen system. However, after one minute, the oxygen spreads to all open (.) locations that are adjacent to a location containing oxygen: + + ## +#..## +#.#..# +#OOO# + ### +After a total of two minutes, the map looks like this: + + ## +#..## +#O#O.# +#OOO# + ### +After a total of three minutes: + + ## +#O.## +#O#OO# +#OOO# + ### +And finally, the whole region is full of oxygen after a total of four minutes: + +So, in this example, all locations contain oxygen after 4 minutes. + +Use the repair droid to get a complete map of the area. How many minutes will it take to fill with oxygen? + +Your puzzle answer was 392. + +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.