import java.util.ArrayList; //Note 1: //Did you know I can't read??. the way I made it allows for any amount of enters. //this was how ever not part of the asignment ;-;. Now my code is a bit bloated. //can't say I'm to proud of this code but it does work, there is some hacking here at work. //in the method that loads the file I add an extra enter. I know how to fix this, but I am just //too lazy. so remember to read, don't assume anything; just read!!. // //Note 2: //the not being able to read continues!. we now do part 2 of the challange, the crying continues. //I just wanna hack, refactoring isn't fun; why can't I just be lazy ;-;. // //Note 3: //I did it! (not talking about reading, still can't do that) I improved a lot of things. the challange is completed. //it took whhhaaaayyyy to long, I hope I learned my lesson. on to the next challange. // //.p.s. no I did not fix the problem with the enter. public class Day1 { private SleighFactory factory; public Day1(){ factory = new SleighFactory(); } public void show(String file){ Sleight test = factory.parse(file); printSleight(test); } private void printSleight(Sleight sleight){ for(int i = 0; i < sleight.getElfs(); i++){ System.out.println("["+i+"] calories: "+sleight.getCalories(i)); } System.out.println("total elfs: "+sleight.getElfs()); System.out.println("most calories: "+sleight.getGreediest()+" amount: "+sleight.getMostCalories()); System.out.println("Sum of top 3: "+sleight.getSumTop(3)); } } class SleighFactory { private ArrayList elfs; public SleighFactory(){ elfs = new ArrayList<>(); } public Sleight parse(String file){ parseFile(file); Sleight newSleight = new Sleight(elfs); elfs.clear(); return newSleight; } private void parseFile(String file){ String currentNumber = ""; int calories = 0; for(int i = 0; i < file.length(); i++){ if(Character.isDigit(file.charAt(i))){ currentNumber += file.charAt(i); continue; } if(file.charAt(i) == '\n'){ calories = calcCalories(currentNumber, calories); currentNumber = ""; } } } private int calcCalories(String currentNumber, int currentCalories){ if(currentNumber != "") return currentCalories + Integer.parseInt(currentNumber); else return newElf(currentCalories); } private int newElf(int calories){ if(calories > 0) insertElf(new Elf(calories)); return 0; } private void insertElf(Elf newElf){ for(int i = 0; i < elfs.size(); i++){ if(hasMoreCalories(newElf, elfs.get(i)) == false){ elfs.add(i, newElf); return; } } elfs.add(newElf); } private boolean hasMoreCalories(Elf elfA, Elf elfB){ if(elfA.getTotalCalories() > elfB.getTotalCalories()) return true; return false; } } class Sleight{ private ArrayList elfs; public Sleight(ArrayList elfs){ this.elfs = new ArrayList<>(); for(int i = 0; i < elfs.size(); i++){ this.elfs.add(elfs.get(i)); } } public int getGreediest(){ return elfs.size() -1; } public int getMostCalories(){ return elfs.get(getGreediest()).getTotalCalories(); } public int getCalories(int index){ if(index >= 0 && index < elfs.size()){ return elfs.get(index).getTotalCalories(); } return -1; } public int getSumTop(int elfCount){ int start = elfs.size() - elfCount; int sumTotal = 0; for(int i = start; i < elfs.size(); i++) sumTotal += elfs.get(i).getTotalCalories(); return sumTotal; } public int getElfs(){ return elfs.size(); } } class Elf { private int totalCalories; public Elf(int totalCalories){ this.totalCalories = totalCalories; } public int getTotalCalories(){ return totalCalories; } }