Add day 1 in Java
Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
parent
652bf4833b
commit
8c3a1c867c
3 changed files with 60 additions and 1 deletions
50
challenges/2022/01-calorieCounting/java/src/Challenge.java
Normal file
50
challenges/2022/01-calorieCounting/java/src/Challenge.java
Normal file
|
@ -0,0 +1,50 @@
|
|||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Challenge {
|
||||
private ArrayList<Integer> parsedInput;
|
||||
|
||||
public Challenge(String filename) throws FileNotFoundException {
|
||||
parsedInput = new ArrayList<>();
|
||||
|
||||
File inputFile = new File(filename);
|
||||
Scanner reader = new Scanner(inputFile);
|
||||
|
||||
int acc = 0;
|
||||
while (reader.hasNextLine()) {
|
||||
String thisLine = reader.nextLine();
|
||||
|
||||
if ("".equals(thisLine)) {
|
||||
if (acc != 0) {
|
||||
parsedInput.add(acc);
|
||||
acc = 0;
|
||||
}
|
||||
} else {
|
||||
acc += Integer.parseInt(thisLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int partOne() {
|
||||
int largest = 0;
|
||||
for (Integer x : parsedInput) {
|
||||
if (x > largest) {
|
||||
largest = x;
|
||||
}
|
||||
}
|
||||
return largest;
|
||||
}
|
||||
|
||||
public int partTwo() {
|
||||
ArrayList<Integer> clonedInput = (ArrayList<Integer>) parsedInput.clone();
|
||||
clonedInput.sort(Integer::compareTo);
|
||||
int acc = 0;
|
||||
for (int i = clonedInput.size()-1; i >= clonedInput.size() - 3; i -= 1) {
|
||||
acc += clonedInput.get(i);
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
}
|
9
challenges/2022/01-calorieCounting/java/src/Main.java
Normal file
9
challenges/2022/01-calorieCounting/java/src/Main.java
Normal file
|
@ -0,0 +1,9 @@
|
|||
import java.io.FileNotFoundException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws FileNotFoundException {
|
||||
Challenge c = new Challenge("input.txt");
|
||||
System.out.println(c.partOne());
|
||||
System.out.println(c.partTwo());
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ Solutions to the [2022 Advent of Code](https://adventofcode.com/2022).
|
|||
|
||||
| Day | Status | Solutions | Notes |
|
||||
| ----------------------------------- | ------------------ | ---------- | ------ |
|
||||
| 01 - Calorie Counting | ★ ★ | [Python](01-calorieCounting/py), [Nim](01-calorieCounting/nim) | Summing numbers |
|
||||
| 01 - Calorie Counting | ★ ★ | [Python](01-calorieCounting/py), [Nim](01-calorieCounting/nim), [Java](01-calorieCounting/java/src) | Summing numbers |
|
||||
|
||||
<!-- PARSE END -->
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue