day 25 optimize and improve heuristics
[aoc_eblake.git] / 2015 / advent23.sh
blob090dea342aeeac63c62aa3e58f6f9b0c61690dec
1 count=0
2 while read line; do
3 inst[$((count++))]=$line
4 done
5 echo "read $count instructions, starting execution"
6 a=0 b=0 pc=0 count=0
7 [[ ! $part1 ]] && a=1
8 while [[ ${inst[pc]+set} ]]; do
9 : $((count++))
10 read i r o <<< "${inst[pc]}"
11 case $i in
12 hlf) case $r in a) a=$((a/2));; b) b=$((b/2));; *) exit 2;; esac
13 pc=$((pc+1)) ;;
14 tpl) case $r in a) a=$((a*3));; b) b=$((b*3));; *) exit 2;; esac
15 pc=$((pc+1)) ;;
16 inc) case $r in a) a=$((a+1));; b) b=$((b+1));; *) exit 2;; esac
17 pc=$((pc+1)) ;;
18 jmp) pc=$((pc+r)) ;;
19 jie) case $r in a,) ((a%2));; b,) ((b%2));; *) exit 2;; esac
20 pc=$((pc+($? ? o : 1))) ;;
21 jio) case $r in a,) ((a==1));; b,) ((b==1));; *) exit 2;; esac
22 pc=$((pc+($? ? 1 : o))) ;;
23 *) echo "invalid instruction $i, exiting"; exit 1 ;;
24 esac
25 done
26 echo "final register state a=$a b=$b, after $count instructions executed"