From 85b400169009e58e8f36a9970cc43fa6c0872b7b Mon Sep 17 00:00:00 2001 From: Brett Parker Date: Mon, 7 Dec 2020 01:36:58 +0000 Subject: [PATCH] Day 7 2019 - part 1 --- day7/computer.sh | 102 +++++++++++++++++++++++++++++++++++++++++++ day7/input-basic.txt | 1 + day7/input-full.txt | 1 + day7/part1.sh | 59 +++++++++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 day7/computer.sh create mode 100644 day7/input-basic.txt create mode 100644 day7/input-full.txt create mode 100644 day7/part1.sh diff --git a/day7/computer.sh b/day7/computer.sh new file mode 100644 index 0000000..1486f23 --- /dev/null +++ b/day7/computer.sh @@ -0,0 +1,102 @@ +#!/bin/bash + +declare -a data + +run_program() { + local -n od=$1 + 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 + case $instruction in + 01|02) + # 01 - add, 02 multiply + symbol="+" + if [ $instruction == "02" ]; then + symbol="*" + fi + val1=${data[$((pos+1))]} + val2=${data[$((pos+2))]} + res_loc=${data[$((pos+3))]} + if [ ${immediate[1]} -eq 0 ]; then + val1=${data[$val1]} + fi + if [ ${immediate[2]} -eq 0 ]; then + val2=${data[$val2]} + fi + data[$res_loc]=$(($val1 $symbol $val2)) + ;; + 03) + # 03 - read input + read -p "input: " input + res_loc=${data[$((pos+1))]} + data[$res_loc]=$input + param_count=1 + ;; + 04) + # 04 - output + if [ ${immediate[1]} -eq 1 ]; then + echo ${data[$((pos+1))]} + else + res_loc=${data[$((pos+1))]} + echo ${data[$res_loc]} + fi + param_count=1 + ;; + 05|06) + # 05 - jump-if-true, 06 - jump-if-false + val=${data[$((pos+1))]} + jumpto=${data[$((pos+2))]} + param_count=2 + if [ ${immediate[1]} -eq 0 ]; then + val=${data[$val]} + fi + if [ ${immediate[2]} -eq 0 ]; then + jumpto=${data[$jumpto]} + fi + 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 + val1=${data[$((pos+1))]} + val2=${data[$((pos+2))]} + res_pos=${data[$((pos+3))]} + if [ ${immediate[1]} -eq 0 ]; then + val1=${data[$val1]} + fi + if [ ${immediate[2]} -eq 0 ]; then + val2=${data[$val2]} + fi + 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 + ;; + 99) + break + ;; + *) + echo "Invalid opcode: $instruction at position $pos" + exit 1 + esac + pos=$(($pos+$param_count+1)) + done +} + diff --git a/day7/input-basic.txt b/day7/input-basic.txt new file mode 100644 index 0000000..e626457 --- /dev/null +++ b/day7/input-basic.txt @@ -0,0 +1 @@ +3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0 diff --git a/day7/input-full.txt b/day7/input-full.txt new file mode 100644 index 0000000..3ab6c2d --- /dev/null +++ b/day7/input-full.txt @@ -0,0 +1 @@ +3,8,1001,8,10,8,105,1,0,0,21,38,59,76,89,106,187,268,349,430,99999,3,9,1002,9,3,9,101,2,9,9,1002,9,4,9,4,9,99,3,9,1001,9,5,9,1002,9,5,9,1001,9,2,9,1002,9,3,9,4,9,99,3,9,1001,9,4,9,102,4,9,9,1001,9,3,9,4,9,99,3,9,101,4,9,9,1002,9,5,9,4,9,99,3,9,1002,9,3,9,101,5,9,9,1002,9,3,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,99 diff --git a/day7/part1.sh b/day7/part1.sh new file mode 100644 index 0000000..f7e99ca --- /dev/null +++ b/day7/part1.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +set -u +set -e + +. "$(dirname $(readlink -f "$BASH_SOURCE"))/computer.sh" + +exec 3