me when bruh
This commit is contained in:
7
.vscode/settings.json
vendored
Normal file
7
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"java.project.sourcePaths": ["src"],
|
||||
"java.project.outputPath": "bin",
|
||||
"java.project.referencedLibraries": [
|
||||
"lib/**/*.jar"
|
||||
]
|
||||
}
|
||||
18
README.md
Normal file
18
README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
## Getting Started
|
||||
|
||||
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
|
||||
|
||||
## Folder Structure
|
||||
|
||||
The workspace contains two folders by default, where:
|
||||
|
||||
- `src`: the folder to maintain sources
|
||||
- `lib`: the folder to maintain dependencies
|
||||
|
||||
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
|
||||
|
||||
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
|
||||
|
||||
## Dependency Management
|
||||
|
||||
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
|
||||
BIN
bin/App.class
Normal file
BIN
bin/App.class
Normal file
Binary file not shown.
BIN
bin/Day1.class
Normal file
BIN
bin/Day1.class
Normal file
Binary file not shown.
BIN
bin/Elf.class
Normal file
BIN
bin/Elf.class
Normal file
Binary file not shown.
BIN
bin/SleighFactory.class
Normal file
BIN
bin/SleighFactory.class
Normal file
Binary file not shown.
BIN
bin/Sleight.class
Normal file
BIN
bin/Sleight.class
Normal file
Binary file not shown.
2264
res/day1.txt
Normal file
2264
res/day1.txt
Normal file
File diff suppressed because it is too large
Load Diff
29
src/App.java
Normal file
29
src/App.java
Normal file
@@ -0,0 +1,29 @@
|
||||
import java.io.File; // Import the File class
|
||||
import java.io.FileNotFoundException; // Import this class to handle errors
|
||||
import java.util.Scanner; // Import the Scanner class to read text files
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Day1 day1 = new Day1();
|
||||
day1.show(loadFile("res/day1.txt"));
|
||||
System.out.println("Hello, World!");
|
||||
}
|
||||
|
||||
public static String loadFile(String url){
|
||||
try {
|
||||
File myObj = new File(url);
|
||||
Scanner myReader = new Scanner(myObj);
|
||||
String data = "";
|
||||
while (myReader.hasNextLine()) {
|
||||
data += myReader.nextLine() + "\n";
|
||||
}
|
||||
System.out.println(data);
|
||||
myReader.close();
|
||||
return data;
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("An error occurred.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
150
src/Day1.java
Normal file
150
src/Day1.java
Normal file
@@ -0,0 +1,150 @@
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user