DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Solution To Triangle Puzzle

02.07.2012
| 3838 views |
```        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<int[]> input = new ArrayList<int[]>();
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; i<stringHolder.length; i++){
tempHolder[i] = new Integer(stringHolder[i]).intValue();
}
counter++;
}

//actual work
for (int j=input.size()-1; j>0; j--){
int[] newLine = rowReduce(input.get(j-1), input.get(j));
input.set(j-1,newLine);
}
}

public static int[] rowReduce(int[] aboveRow, int[] currRow){
int larger;
int[] mergedRows = new int[aboveRow.length];
for (int i=0; i<aboveRow.length; i++){
larger = getLarger(currRow[i]+aboveRow[i], currRow[i+1]+aboveRow[i]);
mergedRows[i] = larger;
}
return mergedRows;
}

public static int getLarger(int a, int b){
if (a>b){
return a;
}
else if(b>a){
return b;
}
else{
return a;
}
}
}
```
`    `