Skip to content

Instantly share code, notes, and snippets.

@JFFail
Last active August 29, 2015 14:13
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 JFFail/5411dcb87494b20c52bf to your computer and use it in GitHub Desktop.
Save JFFail/5411dcb87494b20c52bf to your computer and use it in GitHub Desktop.
Shell Script for Reddit Daily Programmer - #198 Easy
#!/usr/local/bin/zsh
#Get the left word.
echo -n "Enter the left word: "
read leftWord
echo -n "Enter the right word: "
read rightWord
#Get the length of each.
llen=${#leftWord}
#Make a counter. Start at 1 since zsh indexes there.
counter=1
#Loop through and compare the characters.
while [[ $counter -le $llen ]]; do
letter=$leftWord[$counter]
#Find the index of the current character of the left word in the right word.
index=$(echo "$rightWord" | sed -n "s/[$letter].*//p" | wc -c | tr -d ' ')
if [[ $index -gt 0 ]]; then
leftWord=$(echo "$leftWord" | sed "s/$letter//")
rightWord=$(echo "$rightWord" | sed "s/$letter//")
#Reassess the length.
llen=${#leftWord}
else
#Only increment the counter if there were no changes in size.
counter=$(expr $counter + 1)
fi
done
#Write the final results.
echo "Results: <$leftWord> - <$rightWord>"
#Compare the two and determine the winner.
if [[ ${#leftWord} -gt ${#rightWord} ]]; then
echo "Left wins: ${#leftWord} to ${#rightWord}"
elif [[ ${#leftWord} -lt ${#rightWord} ]]; then
echo "Right wins: ${#leftWord} to ${#rightWord}"
else
echo "Tie: ${#leftWord} to ${#rightWord}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment