2022-12-05 14:46:13 +01:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
2022-12-05 15:30:45 +01:00
|
|
|
//Note 1:
|
2022-12-05 14:46:13 +01:00
|
|
|
//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
|
2022-12-05 15:30:45 +01:00
|
|
|
//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.
|
2022-12-05 14:46:13 +01:00
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
2022-12-05 15:30:45 +01:00
|
|
|
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));
|
2022-12-05 14:46:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class SleighFactory {
|
|
|
|
|
private ArrayList<Elf> elfs;
|
|
|
|
|
|
|
|
|
|
public SleighFactory(){
|
|
|
|
|
elfs = new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Sleight parse(String file){
|
|
|
|
|
parseFile(file);
|
2022-12-05 15:30:45 +01:00
|
|
|
Sleight newSleight = new Sleight(elfs);
|
|
|
|
|
elfs.clear();
|
2022-12-05 14:46:13 +01:00
|
|
|
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){
|
2022-12-05 15:30:45 +01:00
|
|
|
if(currentNumber != "")
|
2022-12-05 14:46:13 +01:00
|
|
|
return currentCalories + Integer.parseInt(currentNumber);
|
2022-12-05 15:30:45 +01:00
|
|
|
else
|
2022-12-05 14:46:13 +01:00
|
|
|
return newElf(currentCalories);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int newElf(int calories){
|
2022-12-05 15:30:45 +01:00
|
|
|
if(calories > 0)
|
|
|
|
|
insertElf(new Elf(calories));
|
2022-12-05 14:46:13 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 15:30:45 +01:00
|
|
|
private void insertElf(Elf newElf){
|
2022-12-05 14:46:13 +01:00
|
|
|
for(int i = 0; i < elfs.size(); i++){
|
2022-12-05 15:30:45 +01:00
|
|
|
if(hasMoreCalories(newElf, elfs.get(i)) == false){
|
|
|
|
|
elfs.add(i, newElf);
|
|
|
|
|
return;
|
2022-12-05 14:46:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-12-05 15:30:45 +01:00
|
|
|
elfs.add(newElf);
|
2022-12-05 14:46:13 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-05 15:30:45 +01:00
|
|
|
private boolean hasMoreCalories(Elf elfA, Elf elfB){
|
|
|
|
|
if(elfA.getTotalCalories() > elfB.getTotalCalories())
|
2022-12-05 14:46:13 +01:00
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Sleight{
|
|
|
|
|
private ArrayList<Elf> elfs;
|
|
|
|
|
|
2022-12-05 15:30:45 +01:00
|
|
|
public Sleight(ArrayList<Elf> elfs){
|
2022-12-05 14:46:13 +01:00
|
|
|
this.elfs = new ArrayList<>();
|
|
|
|
|
for(int i = 0; i < elfs.size(); i++){
|
|
|
|
|
this.elfs.add(elfs.get(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getGreediest(){
|
2022-12-05 15:30:45 +01:00
|
|
|
return elfs.size() -1;
|
2022-12-05 14:46:13 +01:00
|
|
|
}
|
|
|
|
|
public int getMostCalories(){
|
2022-12-05 15:30:45 +01:00
|
|
|
return elfs.get(getGreediest()).getTotalCalories();
|
2022-12-05 14:46:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getCalories(int index){
|
|
|
|
|
if(index >= 0 && index < elfs.size()){
|
|
|
|
|
return elfs.get(index).getTotalCalories();
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 15:30:45 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 14:46:13 +01:00
|
|
|
public int getElfs(){
|
|
|
|
|
return elfs.size();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Elf {
|
|
|
|
|
private int totalCalories;
|
|
|
|
|
|
|
|
|
|
public Elf(int totalCalories){
|
|
|
|
|
this.totalCalories = totalCalories;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getTotalCalories(){
|
|
|
|
|
return totalCalories;
|
|
|
|
|
}
|
|
|
|
|
}
|