Files
advent-of-code-2021/16/01.sh
2021-12-16 11:08:24 +01:00

74 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
read input
input=( $(sed 's/\(.\)/ \1 /g' <<< $input) )
bits=()
cursor=0
sum_version=0
for input_char in ${input[@]}; do
case $input_char in
'0') bits+=( 0 0 0 0 ) ;;
'1') bits+=( 0 0 0 1 ) ;;
'2') bits+=( 0 0 1 0 ) ;;
'3') bits+=( 0 0 1 1 ) ;;
'4') bits+=( 0 1 0 0 ) ;;
'5') bits+=( 0 1 0 1 ) ;;
'6') bits+=( 0 1 1 0 ) ;;
'7') bits+=( 0 1 1 1 ) ;;
'8') bits+=( 1 0 0 0 ) ;;
'9') bits+=( 1 0 0 1 ) ;;
'A') bits+=( 1 0 1 0 ) ;;
'B') bits+=( 1 0 1 1 ) ;;
'C') bits+=( 1 1 0 0 ) ;;
'D') bits+=( 1 1 0 1 ) ;;
'E') bits+=( 1 1 1 0 ) ;;
'F') bits+=( 1 1 1 1 ) ;;
esac
done
function parse_packet() {
local version=$((2#${bits[$((cursor))]}${bits[$((cursor+1))]}${bits[$((cursor+2))]}))
local packet_type=$((2#${bits[$((cursor+3))]}${bits[$((cursor+4))]}${bits[$((cursor+5))]}))
cursor=$((cursor+6))
case $packet_type in
'4')
local binary_value='2#'
local end_reached=0
while [ $end_reached -eq 0 ]; do
if [ ${bits[$cursor]} -eq 0 ]; then
end_reached=1
fi
cursor=$((cursor+5))
done
;;
*)
local length_type=${bits[$cursor]}
cursor=$((cursor+1))
if [ $length_type -eq 0 ]; then
local subpackets_length_arr=${bits[@]:$cursor:15}
local subpackets_length=$((2#${subpackets_length_arr// /}))
cursor=$((cursor+15))
local curr_cursor=$cursor
while [ $cursor -lt $((curr_cursor+subpackets_length)) ]; do
parse_packet
done
else
local subpackets_num_arr=${bits[@]:$cursor:11}
local subpackets_num=$((2#${subpackets_num_arr// /}))
local i
cursor=$((cursor+11))
for ((i=0;i<subpackets_num;i++)); do
parse_packet
done
fi
;;
esac
sum_version=$((sum_version+version))
}
parse_packet
echo $sum_version