]> git.sommitrealweird.co.uk Git - advent-of-code-2019.git/blobdiff - day5/computer.sh
Add description for day9
[advent-of-code-2019.git] / day5 / computer.sh
index e0eab88316fb8a769b7455e048e89fe15c5e122a..529af8aba1331988f25fcc6472a3aa061ca9261c 100644 (file)
@@ -23,6 +23,7 @@ run_program() {
         param_count=3
         case $instruction in
             01|02)
+                # 01 - add, 02 multiply
                 symbol="+"
                 if [ $instruction == "02" ]; then
                     symbol="*"
@@ -38,12 +39,14 @@ run_program() {
                 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
@@ -53,6 +56,7 @@ run_program() {
                 param_count=1
                 ;;
             05|06)
+                # 05 - jump-if-true, 06 - jump-if-false
                 val=${data[$((pos+1))]}
                 jumpto=${data[$((pos+2))]}
                 param_count=2
@@ -68,7 +72,8 @@ run_program() {
                     continue
                 fi
                 ;;
-            07)
+            07|08)
+                # 07 - is less than, 08 - is equal
                 val1=${data[$((pos+1))]}
                 val2=${data[$((pos+2))]}
                 res_pos=${data[$((pos+3))]}
@@ -78,26 +83,15 @@ run_program() {
                 if [ ${immediate[2]} -eq 0 ]; then
                     val2=${data[$val2]}
                 fi
-                if [ $val1 -lt $val2 ]; then
-                    data[$res_pos]=1
+                data[$res_pos]=0
+                if [ "$instruction" == "07" ]; then
+                    if [ $val1 -lt $val2 ]; then
+                        data[$res_pos]=1
+                    fi
                 else
-                    data[$res_pos]=0
-                fi
-                ;;
-            08)
-                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
-                if [ $val1 -eq $val2 ]; then
-                    data[$res_pos]=1
-                else
-                    data[$res_pos]=0
+                    if [ $val1 -eq $val2 ]; then
+                        data[$res_pos]=1
+                    fi
                 fi
                 ;;
             99)