One partion contributing to the beauty of a program is its simpleness. It should, accuratelly speaking, be called using another word, abstraction. Few lines of codes which look out of the human comprehension can actually deal with varying situations. Powerful magic hides in these simple codes.
Why do they appear so simple but not barren at all? A useful tool that can help you climb on the stage is disperse mathematics. This great helper can shrink the logic expression of fussy situations as possible as it can. So keep it in mind and use it properly.