Skip to content

Instantly share code, notes, and snippets.

@rhowe
Created January 23, 2020 04:52
Show Gist options
  • Save rhowe/c681aeec766af345114f2d7ef261ec48 to your computer and use it in GitHub Desktop.
Save rhowe/c681aeec766af345114f2d7ef261ec48 to your computer and use it in GitHub Desktop.
AOC2019day16part1
#!/bin/bash
set -eu
declare -ai input basepattern
read -r inputstr < "${1:?}"
for ((i=0; i < ${#inputstr}; i++)); do
input+=("${inputstr:$i:1}")
done
basepattern=(0 1 0 -1)
declare -ai coeff
IFS="
"
coeffs=()
for ((i=0; i < ${#input[@]}; i++)); do
for ((j=0; j < ${#input[@]}; j++)); do
coeff[j]=${basepattern[((j+1) / (i+1)) % 4]}
done
coeffs+=("${coeff[*]}")
done
for phase in {1..100}; do
results=()
for ((i=0; i < ${#inputstr}; i++)); do
ans=$(echo "${input[*]}" | paste -d \* - <(echo "${coeffs[i]}") | paste -sd+ | bc)
results[i]=${ans: -1:1}
done
echo "After $phase phases: ${results[@]}"
input=("${results[@]}")
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment