Over a million developers have joined DZone.

Solution To Triangle Puzzle

·
Problem Description: http://www.yodle.com/downloads/puzzles/triangle.html


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;


public class mainClass3 {

	public static void main (String[] args) throws FileNotFoundException{
		//ugly crap to get values from text file
		Scanner scan = new Scanner(new File("triangle.txt"));
		ArrayList input = new ArrayList();
		int[] tempHolder;
		String[] stringHolder;
		int counter = 1;
		String line;
		
		while (scan.hasNextLine()){
			tempHolder = new int[counter];
			stringHolder = new String[counter];	
			line = scan.nextLine();
			stringHolder = line.split(" ");
			for (int i=0; i0; j--){
			int[] newLine = rowReduce(input.get(j-1), input.get(j));
			input.set(j-1,newLine);
		}
		System.out.println("Answer: "+input.get(0)[0]);
	}
	
	public static int[] rowReduce(int[] aboveRow, int[] currRow){
		int larger;
		int[] mergedRows = new int[aboveRow.length];
		for (int i=0; ib){
			return a;
		}
		else if(b>a){
			return b;
		}
		else{
			return a;
		}
	}
}
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}