Skip to content

Instantly share code, notes, and snippets.

@rhowe
Created February 20, 2020 17:14
Show Gist options
  • Save rhowe/3f500c784cd75820645a7909c36c8fbd to your computer and use it in GitHub Desktop.
Save rhowe/3f500c784cd75820645a7909c36c8fbd to your computer and use it in GitHub Desktop.
AOC2019day2
#!/bin/bash
set -eu
IFS=, read -r -a program
for noun in $(seq 0 99); do
for verb in $(seq 0 99); do
program[1]=$noun
program[2]=$verb
ram=("${program[@]}")
pc=0
while true; do
opcode=${ram[$pc]}
case $opcode in
1)
src1=${ram[$((pc+1))]}
src2=${ram[$((pc+2))]}
dest=${ram[$((pc+3))]}
ram[$dest]=$((ram[src1] + ram[src2]))
pc=$((pc+4))
;;
2)
src1=${ram[$((pc+1))]}
src2=${ram[$((pc+2))]}
dest=${ram[$((pc+3))]}
ram[$dest]=$((ram[src1] * ram[src2]))
pc=$((pc+4))
;;
99)
break
;;
esac
done
result=${ram[0]}
if [ "$result" = 19690720 ]; then
echo "100 * ${program[1]} * ${program[2]} = $((100 * program[1] + program[2]))"
exit
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment