Skip to content

Instantly share code, notes, and snippets.

@mattk42
Created December 3, 2020 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattk42/a271d50c54153024f6e3d32fd4f9c597 to your computer and use it in GitHub Desktop.
Save mattk42/a271d50c54153024f6e3d32fd4f9c597 to your computer and use it in GitHub Desktop.
#!/bin/bash
pos=1
line_num=0
trees=0
while read line
do
len=$(echo $line | wc -c)
char_at_pos=$(echo $line | cut -c $pos)
# Calculate next pos
pos=$(($pos + 3))
if [ $pos -ge $len ]
then
pos=$((($pos % $len) + 1))
fi
# Don't count the first line
if [ $line_num -gt 0 ]
then
if [ $char_at_pos != "." ]
then
trees=$(($trees + 1))
fi
fi
line_num=$(($line_num + 1))
done <<< $(< $1)
echo $trees
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment