Day 7 2019 - part 1
authorBrett Parker <iDunno@sommitrealweird.co.uk>
Mon, 7 Dec 2020 01:36:58 +0000 (01:36 +0000)
committerBrett Parker <iDunno@sommitrealweird.co.uk>
Mon, 7 Dec 2020 01:36:58 +0000 (01:36 +0000)
day7/computer.sh [new file with mode: 0644]
day7/input-basic.txt [new file with mode: 0644]
day7/input-full.txt [new file with mode: 0644]
day7/part1.sh [new file with mode: 0644]

diff --git a/day7/computer.sh b/day7/computer.sh
new file mode 100644 (file)
index 0000000..1486f23
--- /dev/null
@@ -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 (file)
index 0000000..e626457
--- /dev/null
@@ -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 (file)
index 0000000..3ab6c2d
--- /dev/null
@@ -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 (file)
index 0000000..f7e99ca
--- /dev/null
@@ -0,0 +1,59 @@
+#!/bin/bash
+
+set -u
+set -e
+
+. "$(dirname $(readlink -f "$BASH_SOURCE"))/computer.sh"
+
+exec 3<input-full.txt
+
+OLDIFS=$IFS
+IFS="," read -u 3 -a orig_data
+IFS=$OLDIFS
+
+echo "Part 1:"
+
+run_amplifier() {
+    sequence=($@)
+    output=0
+    for phase in ${sequence[@]}; do
+        output=$(echo -e "$phase\n$output" | run_program orig_data)
+    done
+    echo "$output"
+}
+
+values="01234"
+biggest_output=0
+biggest_order="01234"
+for (( a=0; a<${#values}; a++ )); do
+    values_2="${values:0:$a}${values:$((a+1))}"
+    value_1=${values:$a:1}
+    for (( b=0; b<${#values_2}; b++ )); do
+        values_3="${values_2:0:$b}${values_2:$((b+1))}"
+        value_2=${values_2:$b:1}
+        for (( c=0; c<${#values_3}; c++ )); do
+            values_4="${values_3:0:$c}${values_3:$((c+1))}"
+            value_3=${values_3:$c:1}
+            for (( d=0; d<${#values_4}; d++ )); do
+                value_5="${values_4:0:$d}${values_4:$((d+1))}"
+                value_4="${values_4:d:1}"
+                output=$(run_amplifier $value_1 $value_2 $value_3 $value_4 $value_5)
+                if [ $output -gt $biggest_output ]; then
+                    biggest_output=$output
+                    biggest_order="${value_1}${value_2}${value_3}${value_4}${value_5}"
+                fi
+            done
+        done
+    done
+done
+
+echo "Got biggest output $biggest_output with phases $biggest_order"
+
+exit 0
+
+output=0
+for val in 4 3 2 1 0; do
+    output=$(echo -e "$val\n$output" | run_program orig_data)
+done
+echo $output
+