Skip to content

Instantly share code, notes, and snippets.

@rhowe
Created December 8, 2019 10:02
Show Gist options
  • Save rhowe/8dec58098bb1d263ac908e4d398e879b to your computer and use it in GitHub Desktop.
Save rhowe/8dec58098bb1d263ac908e4d398e879b to your computer and use it in GitHub Desktop.
AOC2019day8part1
#!/bin/bash
set -eu
input=$(<"$1")
layers=()
width=25
height=6
area=$((width * height))
for offset in $(seq 0 "$area" "${#input}"); do
layers+=("${input:$offset:$area}")
done
bestlayer=0
bestlayer_nonzeros=0
for layer in "${layers[@]}"; do
nonzeros=${layer//0/}
if [ "${#nonzeros}" -gt "$bestlayer_nonzeros" ]; then
bestlayer=$layer
bestlayer_nonzeros=${#nonzeros}
fi
done
ones=$(tr -dc 1 <<<"$bestlayer")
twos=$(tr -dc 2 <<<"$bestlayer")
echo $(( ${#ones} * ${#twos}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment