One of the things we like to do once we have working code is to refactor it to remove redundancy. Of course, as with any engineering discipline things go wrong when taken to an extreme, and Code Golf is at “remove it all” end of that extreme.
Here is an example Code Golf puzzle taken from the eponymous web site. You start with a grid of numbers:
01 | 34 | 46 | 31 | 55 | 21 | 16 | 88 | 87 | 87 |
32 | 40 | 82 | 40 | 43 | 96 | 08 | 82 | 41 | 86 |
30 | 16 | 24 | 18 | 04 | 54 | 65 | 96 | 38 | 48 |
32 | 00 | 99 | 90 | 24 | 75 | 89 | 41 | 04 | 01 |
11 | 80 | 31 | 83 | 08 | 93 | 37 | 96 | 27 | 64 |
09 | 81 | 28 | 41 | 48 | 23 | 68 | 55 | 86 | 72 |
64 | 61 | 14 | 55 | 33 | 39 | 40 | 18 | 57 | 59 |
49 | 34 | 50 | 81 | 85 | 12 | 22 | 54 | 80 | 76 |
18 | 45 | 50 | 26 | 81 | 95 | 25 | 14 | 46 | 75 |
22 | 52 | 37 | 50 | 37 | 40 | 16 | 71 | 52 | 17 |
Each row has a sum as does each column. Given the numbers sent in via stdin, what is the shortest program that will find the row or column with the greatest sum and print out that sum. I.e., something like:
solution_program < numbers.txt
Should print “615” (which is the sum of column 8).
Shortest has a very simple definition here, the least number of characters in the program, counting white space, new lines and everything else.
So, who is up for some golf? How short can you make your program?