day 14 semi-golf, with missing fifth glyph
[aoc_eblake.git] / 2015 / advent3.sh
bloba9180b78b9b5bc8f973cc17fb2f2ae944e714846
1 if [[ $part1 ]]; then
3 read input
4 unset houses
5 declare -A houses
6 x=0 y=0
7 houses[0/0]=1
8 while test "$input"; do
9 case ${input%%${input#?}} in
10 '<') x=$((x-1)) ;;
11 ^) y=$((y+1)) ;;
12 '>') x=$((x+1)) ;;
13 v) y=$((y-1)) ;;
14 *) echo 'unexpected input'; exit 1 ;;
15 esac
16 houses[$x/$y]=$(( ${houses[$x/$y]} +1 ))
17 input=${input#?}
18 done
19 echo ${houses[*]} | tr ' ' '\n' | wc -l
21 else # part 2
23 read input
24 unset houses
25 declare -A houses
26 x1=0 y1=0
27 x2=0 y2=0
28 houses[0/0]=2
29 while test "$input"; do
30 case ${input%%${input#?}} in
31 '<') x1=$((x1-1)) ;;
32 ^) y1=$((y1+1)) ;;
33 '>') x1=$((x1+1)) ;;
34 v) y1=$((y1-1)) ;;
35 *) echo 'unexpected input'; exit 1 ;;
36 esac
37 houses[$x1/$y1]=$(( ${houses[$x1/$y1]} +1 ))
38 input=${input#?}
39 case ${input%%${input#?}} in
40 '<') x2=$((x2-1)) ;;
41 ^) y2=$((y2+1)) ;;
42 '>') x2=$((x2+1)) ;;
43 v) y2=$((y2-1)) ;;
44 *) echo 'unexpected input'; exit 1 ;;
45 esac
46 houses[$x2/$y2]=$(( ${houses[$x2/$y2]} +1 ))
47 input=${input#?}
48 done
49 echo ${houses[*]} | tr ' ' '\n' | wc -l