Project Euler – Problem # 22 – Solved with Java

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

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?

**********

To solve this problem, I had help with reading the file and making it ready for the program from a stackoverflow question/answerhttp://stackoverflow.com/questions/8672712/optimizing-project-euler-22

One Possible Solution: Java

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.io.File;

public class Problem_22 {
	public static void main(String args[]){
		try
	    {
	       Scanner in = new Scanner(new File("names.txt")).useDelimiter("[\",]+");
	       ArrayList<String> names = new ArrayList<String>();
	       while (in.hasNext())
	       {
	    	   names.add(in.next());
	       }
	       Collections.sort(names);
	       
	       int z = 0;
	       for(int i = 0; i <= names.size() - 1; i ++){
	    	   int x2 = 0;
	    	   int y = 0;
	    	   String name = names.get(i);
	    	   for(int m = 0; m <= name.length() - 1; m ++){
	    		   int x1 = letter_counter(name.charAt(m));
	    		   x2 += x1;
	    	   }
	    	   y = x2 * (i + 1);
	    	   z += y;
			}
	       System.out.format("Total = %d%n", z);
	    }
		catch( FileNotFoundException filenotfoundexcption )
		{
		   System.out.println( "names.txt, does not exist" );
		}
		 
		catch( IOException ioexception )
		{
		   ioexception.printStackTrace( );
		}
	}
	
	public static Integer letter_counter(char letter_u)
	{
		String letter = Character.toString(letter_u);
		Map<String, Integer> letv = new HashMap<String, Integer>();
		letv.put("A", 1);
		letv.put("B", 2);
		letv.put("C", 3);
		letv.put("D", 4);
		letv.put("E", 5);
		letv.put("F", 6);
		letv.put("G", 7);
		letv.put("H", 8);
		letv.put("I", 9);
		letv.put("J", 10);
		letv.put("K", 11);
		letv.put("L", 12);
		letv.put("M", 13);
		letv.put("N", 14);
		letv.put("O", 15);
		letv.put("P", 16);
		letv.put("Q", 17);
		letv.put("R", 18);
		letv.put("S", 19);
		letv.put("T", 20);
		letv.put("U", 21);
		letv.put("V", 22);
		letv.put("W", 23);
		letv.put("X", 24);
		letv.put("Y", 25);
		letv.put("Z", 26);
		
		int lc = letv.get(letter);
		return lc;
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s