diff --git a/bin/Day1.class b/bin/Day1.class index 8ce6b49..f2e19a8 100644 Binary files a/bin/Day1.class and b/bin/Day1.class differ diff --git a/bin/Elf.class b/bin/Elf.class index a5112e4..6b25ffe 100644 Binary files a/bin/Elf.class and b/bin/Elf.class differ diff --git a/bin/SleighFactory.class b/bin/SleighFactory.class index b42cd52..7b9fd2c 100644 Binary files a/bin/SleighFactory.class and b/bin/SleighFactory.class differ diff --git a/bin/Sleight.class b/bin/Sleight.class index c7cbc6b..c7763bf 100644 Binary files a/bin/Sleight.class and b/bin/Sleight.class differ diff --git a/src/Day1.java b/src/Day1.java index dcb97de..1593190 100644 --- a/src/Day1.java +++ b/src/Day1.java @@ -1,11 +1,22 @@ 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!!. +//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; @@ -20,34 +31,29 @@ public class Day1 { } private void printSleight(Sleight sleight){ - System.out.println("total elfs: "+sleight.getElfs()); - System.out.println("most calories: "+sleight.getGreediest()+" amount: "+sleight.getMostCalories()); 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; - private int greediestIndex; public SleighFactory(){ elfs = new ArrayList<>(); - reset(); } public Sleight parse(String file){ parseFile(file); - Sleight newSleight = new Sleight(elfs, greediestIndex); - reset(); + Sleight newSleight = new Sleight(elfs); + elfs.clear(); return newSleight; } - private void reset(){ - greediestIndex = 0; - elfs.clear(); - } private void parseFile(String file){ String currentNumber = ""; @@ -67,40 +73,31 @@ class SleighFactory { } private int calcCalories(String currentNumber, int currentCalories){ - if(currentNumber != ""){ + if(currentNumber != "") return currentCalories + Integer.parseInt(currentNumber); - }else{ + else return newElf(currentCalories); - } } private int newElf(int calories){ - if(calories > 0){ - elfs.add(new Elf(calories)); - if(hasMost(elfs.size() -1) == true){ - greediestIndex = elfs.size() -1; - } - } + if(calories > 0) + insertElf(new Elf(calories)); return 0; } - private boolean hasMost(int elfIndex){ + private void insertElf(Elf newElf){ for(int i = 0; i < elfs.size(); i++){ - if(i != elfIndex){ - if(hasMoreCalories(elfIndex, i) == false){ - return false; - } + if(hasMoreCalories(newElf, elfs.get(i)) == false){ + elfs.add(i, newElf); + return; } } - return true; + elfs.add(newElf); } - private boolean hasMoreCalories(int elfA, int elfB){ - int caloriesA = elfs.get(elfA).getTotalCalories(); - int caloriesB = elfs.get(elfB).getTotalCalories(); - if(caloriesA > caloriesB){ + private boolean hasMoreCalories(Elf elfA, Elf elfB){ + if(elfA.getTotalCalories() > elfB.getTotalCalories()) return true; - } return false; } @@ -108,10 +105,8 @@ class SleighFactory { class Sleight{ private ArrayList elfs; - private int greediest; - public Sleight(ArrayList elfs, int greediest){ - this.greediest = greediest; + public Sleight(ArrayList elfs){ this.elfs = new ArrayList<>(); for(int i = 0; i < elfs.size(); i++){ this.elfs.add(elfs.get(i)); @@ -119,10 +114,10 @@ class Sleight{ } public int getGreediest(){ - return greediest; + return elfs.size() -1; } public int getMostCalories(){ - return elfs.get(greediest).getTotalCalories(); + return elfs.get(getGreediest()).getTotalCalories(); } public int getCalories(int index){ @@ -132,6 +127,14 @@ class Sleight{ 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(); }