Add day 1 in Java

Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
akp 2022-12-01 16:46:59 +00:00
parent 652bf4833b
commit 8c3a1c867c
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
3 changed files with 60 additions and 1 deletions

View 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;
}
}

View 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());
}
}

View file

@ -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 -->