5 filename=${1:-p1_16.txt}
10 declare -A hex_to_binary=( [0]="0000" [1]="0001" [2]="0010" [3]="0011" [4]="0100" [5]="0101" [6]="0110" [7]="0111" [8]="1000" [9]="1001" [A]="1010" [B]="1011" [C]="1100" [D]="1101" [E]="1110" [F]="1111" )
14 declare -g __packets=""
15 for (( a=0; a<${#hex_string}; a++ )); do
16 __packets+=${hex_to_binary[${hex_string:$a:1}]}
19 declare -g __versions_total=0
21 declare __packet_offset=0
22 declare -g __cur_level=0
24 if [ $debug == "0" ]; then
25 echo_verbose() { true; }
27 echo_verbose() { echo "$@" >&2; }
31 local level="${1:- 1}"
32 declare -g __packet_version=$((2#${__packets:$__packet_offset:3}))
33 ((__packet_offset+=3))
34 echo_verbose "Packet version: $__packet_version"
38 local level="${1:- 1}"
39 declare -g __packet_type=$((2#${__packets:$__packet_offset:3}))
40 ((__packet_offset+=3))
41 echo_verbose "Packet type: $__packet_type"
45 local level="${1:- 1}"
46 local keep_going=${__packets:$__packet_offset:1}
48 ((__packet_offset+=1))
49 while [ $keep_going -eq 1 ]; do
50 binary_value+=${__packets:$__packet_offset:4}
51 ((__packet_offset+=4))
52 keep_going=${__packets:$__packet_offset:1}
53 ((__packet_offset+=1))
55 # if we're here, we're on to the last group
56 binary_value+=${__packets:$__packet_offset:4}
57 ((__packet_offset+=4))
58 declare -g __packet_value=$((2#$binary_value))
59 echo_verbose "Packet literal: $__packet_value"
63 local level="${1:- 1}"
65 local len_type=$((2#${__packets:$__packet_offset:1}))
67 ((__packet_offset+=1))
71 number=$((2#${__packets:$__packet_offset:15}))
72 ((__packet_offset+=15))
75 number=$((2#${__packets:$__packet_offset:11}))
76 ((__packet_offset+=11))
82 parse_sum $len_type $number
85 parse_product $len_type $number
88 parse_minimum $len_type $number
91 parse_maximum $len_type $number
94 parse_greaterthan $len_type $number
97 parse_lessthan $len_type $number
100 parse_equal $len_type $number
109 local id=$__packet_offset
112 echo_verbose "[$id]: Doing a sum ($len_type - $count)"
116 local end_offset=$((__packet_offset+$count))
117 while [ $__packet_offset -lt $end_offset ]; do
118 echo_verbose "[$id]: Getting sum sub packet"
120 ((total+=$__packet_value))
124 for (( a=0; a<$count; a++ )); do
125 echo_verbose "[$id]: Getting sum sub packet"
127 ((total+=$__packet_value))
132 echo_verbose "[$id]: Sum total: $total (offset: $__packet_offset)"
134 declare -g __packet_value=$total
141 local id=$__packet_offset
144 echo_verbose "[$id]: Doing a product ($len_type - $count)"
148 local end_offset=$((__packet_offset+$count))
149 while [ $__packet_offset -lt $end_offset ]; do
150 echo_verbose "[$id]: Getting product sub packet"
152 ((total*=$__packet_value))
156 for (( a=0; a<$count; a++ )); do
157 echo_verbose "[$id]: Getting product sub packet"
159 ((total*=$__packet_value))
164 echo_verbose "[$id]: Product total: $total (offset: $__packet_offset)"
166 declare -g __packet_value=$total
173 local id=$__packet_offset
176 echo_verbose "[$id]: Doing mimimum"
180 local end_offset=$((__packet_offset+$count))
181 while [ $__packet_offset -lt $end_offset ]; do
182 echo_verbose "[$id]: Getting minimum sub packet"
184 if [ -z $minimum ] || [ $__packet_value -lt $minimum ]; then
185 minimum=$__packet_value
190 for (( a=0; a<$count; a++ )); do
191 echo_verbose "[$id]: Getting minimum sub packet"
193 if [ -z $minimum ] || [ $__packet_value -lt $minimum ]; then
194 minimum=$__packet_value
200 echo_verbose "[$id]: Minimum value: $minimum (offset: $__packet_offset)"
202 declare -g __packet_value=$minimum
209 local id=$__packet_offset
212 echo_verbose "[$id]: Doing maximum ($len_type - $count)"
216 local end_offset=$((__packet_offset+$count))
217 while [ $__packet_offset -lt $end_offset ]; do
218 echo_verbose "[$id]: Getting maximum sub packet"
220 if [ -z $maximum ] || [ $__packet_value -gt $maximum ]; then
221 maximum=$__packet_value
226 for (( a=0; a<$count; a++ )); do
227 echo_verbose "[$id]: Getting maximum sub packet"
229 if [ -z $maximum ] || [ $__packet_value -gt $maximum ]; then
230 maximum=$__packet_value
236 echo_verbose "[$id]: Maximum value: $maximum (offset: $__packet_offset)"
238 declare -g __packet_value=$maximum
241 parse_greaterthan() {
244 local id=$__packet_offset
246 echo_verbose "[$id]: Doing >"
248 # we don't actually care, we know that it's exactly 2 sub packets that we want... so
250 local val1=$__packet_value
252 local val2=$__packet_value
254 if [ $val1 -gt $val2 ]; then
255 declare -g __packet_value=1
257 declare -g __packet_value=0
260 echo_verbose "[$id]: Got $__packet_value (offset: $__packet_offset)"
266 local id=$__packet_offset
268 echo_verbose "[$id]: Doing <"
270 # we don't actually care, we know that it's exactly 2 sub packets that we want... so
272 local val1=$__packet_value
274 local val2=$__packet_value
276 if [ $val1 -lt $val2 ]; then
277 declare -g __packet_value=1
279 declare -g __packet_value=0
282 echo_verbose "[$id]: Got $__packet_valuei (offset: $__packet_offset)"
288 local id=$__packet_offset
290 echo_verbose "[$id]: Doing ="
292 # we don't actually care, we know that it's exactly 2 sub packets that we want... so
294 local val1=$__packet_value
296 local val2=$__packet_value
298 if [ $val1 -eq $val2 ]; then
299 declare -g __packet_value=1
301 declare -g __packet_value=0
304 echo_verbose "[$id]: Got $__packet_value (offset: $__packet_offset)"
308 parse_version && local pversion=$__packet_version
309 parse_type && local ptype=$__packet_type
311 ((__versions_total+=$pversion))
318 parse_operator $__cur_level $ptype
324 local level="${1:- 1}"
325 # we'll first need the version and the type, so grab those
327 while [ $__packet_offset -lt ${#__packets} ]; do
331 if [ $(($__packet_offset+7)) -ge ${#__packets} ]; then
339 echo "Total: $__versions_total"
340 echo "End value: $__packet_value"