we did it gamers

This commit is contained in:
Optioneel
2022-12-05 15:30:45 +01:00
parent 02b334479b
commit 318871d679
5 changed files with 39 additions and 36 deletions

View File

@@ -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<Elf> 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<Elf> elfs;
private int greediest;
public Sleight(ArrayList<Elf> elfs, int greediest){
this.greediest = greediest;
public Sleight(ArrayList<Elf> 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();
}