we did it gamers
This commit is contained in:
BIN
bin/Day1.class
BIN
bin/Day1.class
Binary file not shown.
BIN
bin/Elf.class
BIN
bin/Elf.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,11 +1,22 @@
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
//Note 1:
|
||||||
//Did you know I can't read??. the way I made it allows for any amount of enters.
|
//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.
|
//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.
|
//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
|
//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 {
|
public class Day1 {
|
||||||
private SleighFactory factory;
|
private SleighFactory factory;
|
||||||
@@ -20,34 +31,29 @@ public class Day1 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void printSleight(Sleight sleight){
|
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++){
|
for(int i = 0; i < sleight.getElfs(); i++){
|
||||||
System.out.println("["+i+"] calories: "+sleight.getCalories(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 {
|
class SleighFactory {
|
||||||
private ArrayList<Elf> elfs;
|
private ArrayList<Elf> elfs;
|
||||||
private int greediestIndex;
|
|
||||||
|
|
||||||
public SleighFactory(){
|
public SleighFactory(){
|
||||||
elfs = new ArrayList<>();
|
elfs = new ArrayList<>();
|
||||||
reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Sleight parse(String file){
|
public Sleight parse(String file){
|
||||||
parseFile(file);
|
parseFile(file);
|
||||||
Sleight newSleight = new Sleight(elfs, greediestIndex);
|
Sleight newSleight = new Sleight(elfs);
|
||||||
reset();
|
elfs.clear();
|
||||||
return newSleight;
|
return newSleight;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reset(){
|
|
||||||
greediestIndex = 0;
|
|
||||||
elfs.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void parseFile(String file){
|
private void parseFile(String file){
|
||||||
String currentNumber = "";
|
String currentNumber = "";
|
||||||
@@ -67,40 +73,31 @@ class SleighFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int calcCalories(String currentNumber, int currentCalories){
|
private int calcCalories(String currentNumber, int currentCalories){
|
||||||
if(currentNumber != ""){
|
if(currentNumber != "")
|
||||||
return currentCalories + Integer.parseInt(currentNumber);
|
return currentCalories + Integer.parseInt(currentNumber);
|
||||||
}else{
|
else
|
||||||
return newElf(currentCalories);
|
return newElf(currentCalories);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int newElf(int calories){
|
private int newElf(int calories){
|
||||||
if(calories > 0){
|
if(calories > 0)
|
||||||
elfs.add(new Elf(calories));
|
insertElf(new Elf(calories));
|
||||||
if(hasMost(elfs.size() -1) == true){
|
|
||||||
greediestIndex = elfs.size() -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasMost(int elfIndex){
|
private void insertElf(Elf newElf){
|
||||||
for(int i = 0; i < elfs.size(); i++){
|
for(int i = 0; i < elfs.size(); i++){
|
||||||
if(i != elfIndex){
|
if(hasMoreCalories(newElf, elfs.get(i)) == false){
|
||||||
if(hasMoreCalories(elfIndex, i) == false){
|
elfs.add(i, newElf);
|
||||||
return false;
|
return;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
elfs.add(newElf);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasMoreCalories(int elfA, int elfB){
|
private boolean hasMoreCalories(Elf elfA, Elf elfB){
|
||||||
int caloriesA = elfs.get(elfA).getTotalCalories();
|
if(elfA.getTotalCalories() > elfB.getTotalCalories())
|
||||||
int caloriesB = elfs.get(elfB).getTotalCalories();
|
|
||||||
if(caloriesA > caloriesB){
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,10 +105,8 @@ class SleighFactory {
|
|||||||
|
|
||||||
class Sleight{
|
class Sleight{
|
||||||
private ArrayList<Elf> elfs;
|
private ArrayList<Elf> elfs;
|
||||||
private int greediest;
|
|
||||||
|
|
||||||
public Sleight(ArrayList<Elf> elfs, int greediest){
|
public Sleight(ArrayList<Elf> elfs){
|
||||||
this.greediest = greediest;
|
|
||||||
this.elfs = new ArrayList<>();
|
this.elfs = new ArrayList<>();
|
||||||
for(int i = 0; i < elfs.size(); i++){
|
for(int i = 0; i < elfs.size(); i++){
|
||||||
this.elfs.add(elfs.get(i));
|
this.elfs.add(elfs.get(i));
|
||||||
@@ -119,10 +114,10 @@ class Sleight{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int getGreediest(){
|
public int getGreediest(){
|
||||||
return greediest;
|
return elfs.size() -1;
|
||||||
}
|
}
|
||||||
public int getMostCalories(){
|
public int getMostCalories(){
|
||||||
return elfs.get(greediest).getTotalCalories();
|
return elfs.get(getGreediest()).getTotalCalories();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCalories(int index){
|
public int getCalories(int index){
|
||||||
@@ -132,6 +127,14 @@ class Sleight{
|
|||||||
return -1;
|
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(){
|
public int getElfs(){
|
||||||
return elfs.size();
|
return elfs.size();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user