5 filename="${1:-example.txt}"
13 while read -u 3 poly; do
16 inserts[$left]="$right"
19 declare -A pair_result=()
20 for k in "${!inserts[@]}"; do
22 pair_result[$k]="${k:0:1}${char} ${char}${k:1:1}"
26 for (( a=0; a<$((${#template} - 1)); a++ )); do
27 lookup="${template:$a:2}"
28 if [ "${pairs[$lookup]+abc}" ]; then
36 declare -A new_pairs=()
37 for pair in "${!pairs[@]}"; do
39 for new_pair in ${pair_result[$pair]}; do
40 if [ "${new_pairs[$new_pair]+abc}" ]; then
41 ((new_pairs[$new_pair]+=$count))
43 new_pairs[$new_pair]=$count
48 declare -g -A pairs=()
49 for k in "${!new_pairs[@]}"; do
50 pairs[$k]=${new_pairs[$k]}
55 declare -A char_counts=()
56 for k in "${!pairs[@]}"; do
57 for (( a=0; a<2; a++ )); do
59 if [ "${char_counts[$char]+abc}" ]; then
60 ((char_counts[$char]+=${pairs[$k]}))
62 char_counts[$char]=${pairs[$k]}
67 for char in ${template:0:1} ${template: -1:1}; do # start and end of template get lost from the original count
68 if [ "${char_counts[$char]+abc}" ]; then
69 ((char_counts[$char]+=1))
76 for v in "${char_counts[@]}"; do
80 declare -a vals=("${!hl[@]}")
85 diff=$((diff / 2)) # because the parts are in pairs, our counts are going to be double what they should be
90 for (( a=1; a<=10; a++ )); do
94 echo "Part 1: $(get_diff)"
96 for (( a=11; a<=40; a++ )); do
100 echo "Part 2: $(get_diff)"