150 lines
3.8 KiB
Java
150 lines
3.8 KiB
Java
|
|
|
||
|
|
import java.util.ArrayList;
|
||
|
|
|
||
|
|
//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!!.
|
||
|
|
|
||
|
|
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){
|
||
|
|
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));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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();
|
||
|
|
return newSleight;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void reset(){
|
||
|
|
greediestIndex = 0;
|
||
|
|
elfs.clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
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){
|
||
|
|
elfs.add(new Elf(calories));
|
||
|
|
if(hasMost(elfs.size() -1) == true){
|
||
|
|
greediestIndex = elfs.size() -1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
private boolean hasMost(int elfIndex){
|
||
|
|
for(int i = 0; i < elfs.size(); i++){
|
||
|
|
if(i != elfIndex){
|
||
|
|
if(hasMoreCalories(elfIndex, i) == false){
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
private boolean hasMoreCalories(int elfA, int elfB){
|
||
|
|
int caloriesA = elfs.get(elfA).getTotalCalories();
|
||
|
|
int caloriesB = elfs.get(elfB).getTotalCalories();
|
||
|
|
if(caloriesA > caloriesB){
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
class Sleight{
|
||
|
|
private ArrayList<Elf> elfs;
|
||
|
|
private int greediest;
|
||
|
|
|
||
|
|
public Sleight(ArrayList<Elf> elfs, int greediest){
|
||
|
|
this.greediest = greediest;
|
||
|
|
this.elfs = new ArrayList<>();
|
||
|
|
for(int i = 0; i < elfs.size(); i++){
|
||
|
|
this.elfs.add(elfs.get(i));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public int getGreediest(){
|
||
|
|
return greediest;
|
||
|
|
}
|
||
|
|
public int getMostCalories(){
|
||
|
|
return elfs.get(greediest).getTotalCalories();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int getCalories(int index){
|
||
|
|
if(index >= 0 && index < elfs.size()){
|
||
|
|
return elfs.get(index).getTotalCalories();
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
public int getElfs(){
|
||
|
|
return elfs.size();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class Elf {
|
||
|
|
private int totalCalories;
|
||
|
|
|
||
|
|
public Elf(int totalCalories){
|
||
|
|
this.totalCalories = totalCalories;
|
||
|
|
}
|
||
|
|
|
||
|
|
public int getTotalCalories(){
|
||
|
|
return totalCalories;
|
||
|
|
}
|
||
|
|
}
|