-
-
Save rhowe/b7e26b6acc63fa2db62f29e93ce5b346 to your computer and use it in GitHub Desktop.
AOC2019day9part2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -eu | |
declare -ai program ram | |
declare -i addrmode1 addrmode2 arg1 arg2 pc=0 rb=0 | |
IFS=, read -r -a program < "$1" | |
shift | |
inputs=("$@") | |
ram=("${program[@]}") | |
ops=(x '+' '*' x x '-eq' '-ne' '<' '==') | |
while : ; do | |
printf -v opcode %05d "${ram[pc++]}" | |
case "${opcode: -2:2}" in | |
01|02|07|08) addrmode1=${opcode: -3:1} addrmode2=${opcode: -4:1} arg1=${ram[pc++]} arg2=${ram[pc++]} ram[(${opcode: -5:1} != 0)*rb+${ram[pc++]}]=$(((addrmode1 == 1 ? arg1 : ram[(addrmode1 != 0)*rb+arg1]) ${ops[${opcode: -1:1}]} (addrmode2 == 1 ? arg2 : ram[(addrmode2 != 0)*rb+arg2]))) ;; | |
03) | |
ram[(${opcode: -3:1} == 0 ? 0 : rb)+${ram[pc++]}]=${inputs[0]} | |
inputs=("${inputs[*]:1}") | |
;; | |
04) | |
addrmode1=${opcode: -3:1} arg1=${ram[pc++]} | |
echo $((addrmode1 == 1 ? arg1 : ram[(addrmode1 == 0 ? 0 : rb)+arg1])) | |
;; | |
05|06) | |
addrmode1=${opcode: -3:1} addrmode2=${opcode: -4:1} arg1=${ram[pc++]} arg2=${ram[pc++]} | |
[ "$((addrmode1 == 1 ? arg1 : ram[(addrmode1 == 0 ? 0 : rb)+arg1]))" ${ops[${opcode: -1:1}]} 0 ] || pc=$((addrmode2 == 1 ? arg2 : ram[(addrmode2 == 0 ? 0 : rb)+arg2])) | |
;; | |
09) | |
addrmode1=${opcode: -3:1} arg1=${ram[pc++]} | |
((rb+=addrmode1 == 1 ? arg1 : ram[(addrmode1 == 0 ? 0 : rb)+arg1])) | |
;; | |
99) break ;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment