From 9017e242041b51939c88e7e99221923965e919e1 Mon Sep 17 00:00:00 2001 From: Brett Parker Date: Thu, 16 Dec 2021 16:07:04 +0000 Subject: [PATCH] Day 16 pt 1 - doesn't actually stack packets hierarchically - just parses the headers --- day16/decoder.sh | 106 +++++++++++++++++++++++++++++++++++++++++++++++ day16/input.txt | 1 + day16/p1_12.txt | 1 + day16/p1_16.txt | 1 + day16/p1_23.txt | 1 + day16/p1_31.txt | 1 + 6 files changed, 111 insertions(+) create mode 100755 day16/decoder.sh create mode 100644 day16/input.txt create mode 100644 day16/p1_12.txt create mode 100644 day16/p1_16.txt create mode 100644 day16/p1_23.txt create mode 100644 day16/p1_31.txt diff --git a/day16/decoder.sh b/day16/decoder.sh new file mode 100755 index 0000000..fe67cbd --- /dev/null +++ b/day16/decoder.sh @@ -0,0 +1,106 @@ +#!/bin/bash + +set -u + +filename=${1:-p1_16.txt} +debug=${2:-0} + +exec 3<"${filename}" + +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" ) + +read -u 3 hex_string + +declare -g __packets="" +for (( a=0; a<${#hex_string}; a++ )); do + __packets+=${hex_to_binary[${hex_string:$a:1}]} +done + +declare -g __versions_total=0 + +declare __packet_offset=0 + +if [ $debug == "0" ]; then + echo_verbose() { true; } +else + echo_verbose() { echo "$@" >&2; } +fi + +parse_version() { + declare -g __packet_version=$((2#${__packets:$__packet_offset:3})) + ((__packet_offset+=3)) + echo_verbose "Packet version: $__packet_version" +} + +parse_type() { + declare -g __packet_type=$((2#${__packets:$__packet_offset:3})) + ((__packet_offset+=3)) + echo_verbose "Packet type: $__packet_type" +} + +parse_literal() { + local keep_going=${__packets:$__packet_offset:1} + local binary_value="" + ((__packet_offset+=1)) + while [ $keep_going -eq 1 ]; do + binary_value+=${__packets:$__packet_offset:4} + ((__packet_offset+=4)) + keep_going=${__packets:$__packet_offset:1} + ((__packet_offset+=1)) + done + # if we're here, we're on to the last group + binary_value+=${__packets:$__packet_offset:4} + ((__packet_offset+=4)) + declare -g __packet_literal=$((2#$binary_value)) + echo_verbose "Packet literal: $__packet_literal" +} + +parse_operator() { + local len_type=$((2#${__packets:$__packet_offset:1})) + ((__packet_offset+=1)) + + case $len_type in + 0) + __packets_length=$((2#${__packets:$__packet_offset:15})) + ((__packet_offset+=15)) + ;; + 1) + __packets_count=$((2#${__packets:$__packet_offset:11})) + ((__packet_offset+=11)) + ;; + esac +} + +parse_packets() { + # we'll first need the version and the type, so grab those + local pversion + local ptype + + while [ $__packet_offset -lt ${#__packets} ]; do + parse_version + parse_type + version=$__packet_version + ptype=$__packet_type + + ((__versions_total+=$version)) + + echo_verbose "Remaining: ${__packets:$__packet_offset}" + + case $ptype in + 4) + parse_literal + ;; + *) + parse_operator + ;; + esac + + if [ $(($__packet_offset+7)) -ge ${#__packets} ]; then + break + fi + done +} + +parse_packets + +echo "Total: $__versions_total" diff --git a/day16/input.txt b/day16/input.txt new file mode 100644 index 0000000..159f371 --- /dev/null +++ b/day16/input.txt @@ -0,0 +1 @@ +40541D900AEDC01A88002191FE2F45D1006A2FC2388D278D4653E3910020F2E2F3E24C007ECD7ABA6A200E6E8017F92C934CFA0E5290B569CE0F4BA5180213D963C00DC40010A87905A0900021B0D624C34600906725FFCF597491C6008C01B0004223342488A200F4378C9198401B87311A0C0803E600FC4887F14CC01C8AF16A2010021D1260DC7530042C012957193779F96AD9B36100907A00980021513E3943600043225C1A8EB2C3040043CC3B1802B400D3CA4B8D3292E37C30600B325A541D979606E384B524C06008E802515A638A73A226009CDA5D8026200D473851150401E8BF16E2ACDFB7DCD4F5C02897A5288D299D89CA6AA672AD5118804F592FC5BE8037000042217C64876000874728550D4C0149F29D00524ACCD2566795A0D880432BEAC79995C86483A6F3B9F6833397DEA03E401004F28CD894B9C48A34BC371CF7AA840155E002012E21260923DC4C248035299ECEB0AC4DFC0179B864865CF8802F9A005E264C25372ABAC8DEA706009F005C32B7FCF1BF91CADFF3C6FE4B3FB073005A6F93B633B12E0054A124BEE9C570004B245126F6E11E5C0199BDEDCE589275C10027E97BE7EF330F126DF3817354FFC82671BB5402510C803788DFA009CAFB14ECDFE57D8A766F0001A74F924AC99678864725F253FD134400F9B5D3004A46489A00A4BEAD8F7F1F7497C39A0020F357618C71648032BB004E4BBC4292EF1167274F1AA0078902262B0D4718229C8608A5226528F86008CFA6E802F275E2248C65F3610066274CEA9A86794E58AA5E5BDE73F34945E2008D27D2278EE30C489B3D20336D00C2F002DF480AC820287D8096F700288082C001DE1400C50035005AA2013E5400B10028C009600A74001EF2004F8400C92B172801F0F4C0139B8E19A8017D96A510A7E698800EAC9294A6E985783A400AE4A2945E9170 diff --git a/day16/p1_12.txt b/day16/p1_12.txt new file mode 100644 index 0000000..ed3b78a --- /dev/null +++ b/day16/p1_12.txt @@ -0,0 +1 @@ +620080001611562C8802118E34 diff --git a/day16/p1_16.txt b/day16/p1_16.txt new file mode 100644 index 0000000..0d2cbff --- /dev/null +++ b/day16/p1_16.txt @@ -0,0 +1 @@ +8A004A801A8002F478 diff --git a/day16/p1_23.txt b/day16/p1_23.txt new file mode 100644 index 0000000..827e51b --- /dev/null +++ b/day16/p1_23.txt @@ -0,0 +1 @@ +C0015000016115A2E0802F182340 diff --git a/day16/p1_31.txt b/day16/p1_31.txt new file mode 100644 index 0000000..0a1278e --- /dev/null +++ b/day16/p1_31.txt @@ -0,0 +1 @@ +A0016C880162017C3686B18A3D4780 -- 2.39.5