Project Euler with Befunge

Fork me on GitHub

Problem 022: Names scores


Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

>               v                                     >   v
 MARY           >"??IE"*++:20p00p>010p0 >:00gg"@"-:0`#^_$0>10g74**+10pv
 PATRICIA                      > ^      ^          _v#         -+66:+1<
 LINDA                    v   _^#!p00:-1g00pg000g01$<          >v
 BARBARA                  >20g30p>       040p> 040g1+g 040g2+g`|>40g:1+>60p70p66+:80p>:60g1+g\70g1+g80g60g1+v
 ELIZABETH                                   |`\-1g03 p04:+1g04<                   $<|\p08:-1:g08p+1g07g08p <
 JENNIFER                        |-2p03-1:g03<       // Sum the values              ^<
 MARIA               v           <v+1p09+g09*g05-"@"<
 SUSAN               >090p20g50p>1>:50gg:" "       -|
 MARGARET                       |p05:-1g05       $$$<
 DOROTHY                   @.g09<
This program is too big to display/execute here, click [download] to get the full program.


The main thing here was sorting the list. I used bubble sort, which is not the fastest algorithm but was pretty easy to implement in befunge. The rest is just calculating the single scores.

Interpreter steps: 4 703 607 994
Execution time (BefunExec): 16min (4.89 MHz)
Program size: 109 x 5164
Solution: 871198282
Solved at: 2014-09-16

made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript