day 23 support more inputs
[aoc_eblake.git] / 2015 / advent6.sh
blob46f771d63e50618aa77a67077d7484f7d121313e
1 mapfile -t list < <(sed 's/turn //; s/through //; s/,/ /g')
2 echo "parsed ${#list[*]} instructions"
3 count=0
4 for row in {0..999}; do
5 echo "processing row $row"
6 for col in {0..999}; do
7 line[$col]=0
8 done
9 # printf " inst 000"
10 for ((i = 0; i < ${#list[*]}; i++)); do
11 # printf '\b\b\b%3d' $i
12 read inst x1 y1 x2 y2 <<< ${list[$i]}
13 if [[ $row -lt $y1 || $row -gt $y2 ]]; then
14 # echo " no-op for inst $i on row $row"
15 continue;
17 # echo " $i: running operation $inst over $((x2-x1+1)) cells"
18 pos=$x1
19 while [[ $pos -le $x2 ]]; do
20 if [[ $part1 ]]; then
21 case $inst in
22 on) line[$pos]=1 ;;
23 off) line[$pos]=0 ;;
24 toggle) line[$pos]=$((1-${line[$pos]})) ;;
25 *) echo oops; exit 2 ;;
26 esac
27 else
28 case $inst in
29 on) line[$pos]=$((${line[$pos]}+1)) ;;
30 off) line[$pos]=$((${line[$pos]}?${line[$pos]}-1:0)) ;;
31 toggle) line[$pos]=$((${line[$pos]}+2)) ;;
32 *) echo oops; exit 2 ;;
33 esac
35 pos=$((pos + 1))
36 done
37 done
38 subcount=$(( $(echo ${line[*]} | tr ' ' +) ))
39 # echo
40 printf "row $row adds $subcount\n"
41 count=$((count + subcount))
42 done
43 echo "final count $count"