Skip to content

Instantly share code, notes, and snippets.

@mattk42
Created December 4, 2020 06:06
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/81928295df3735f023ab6474d0ea8b95 to your computer and use it in GitHub Desktop.
Save mattk42/81928295df3735f023ab6474d0ea8b95 to your computer and use it in GitHub Desktop.
#!/bin/bash
function ride_slope(){
pos=1
line_num=0
trees=0
slope_right=$1
slope_down=$2
while read line
do
len=$(echo $line | wc -c)
# Don't count the first line
if [ $line_num -gt 0 ]
then
if [ $(($line_num % slope_down)) -eq 0 ]
then
# Calculate next pos
pos=$(($pos + slope_right))
if [ $pos -ge $len ]
then
pos=$((($pos % $len) + 1))
fi
char_at_pos=$(echo $line | cut -c $pos)
if [ $char_at_pos != "." ]
then
trees=$(($trees + 1))
fi
fi
fi
line_num=$(($line_num + 1))
done <<< $(< $input)
echo $trees
}
input=$1
prod=$(ride_slope 1 1)
prod=$(($prod * $(ride_slope 3 1)))
prod=$(($prod * $(ride_slope 5 1)))
prod=$(($prod * $(ride_slope 7 1)))
prod=$(($prod * $(ride_slope 1 2)))
echo $prod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment