Created
December 4, 2020 06:06
-
-
Save mattk42/81928295df3735f023ab6474d0ea8b95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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