Created
December 26, 2019 19:13
-
-
Save rhowe/89ef7dd299ebbeff08bf8e8987731d29 to your computer and use it in GitHub Desktop.
AOC2019day25part1
This file contains hidden or 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) | |
| if [ "${#inputs[@]}" -eq 0 ]; then | |
| read -r cmd | |
| for ((i=0; i < "${#cmd}"; i++)); do | |
| inputs[i]=$(printf %d "'${cmd:i:1}") | |
| done | |
| inputs+=("10") | |
| fi | |
| ram[(${opcode: -3:1} == 0 ? 0 : rb)+${ram[pc++]}]=${inputs[0]} | |
| inputs=("${inputs[@]:1}") | |
| ;; | |
| 04) | |
| addrmode1=${opcode: -3:1} arg1=${ram[pc++]} | |
| printf \\x$(printf %x "$((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