Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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