ah, cephalapod math
This commit is contained in:
107
src/Day06.java
Normal file
107
src/Day06.java
Normal file
@@ -0,0 +1,107 @@
|
||||
package src;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Day06 {
|
||||
public static void main(String[] args) {
|
||||
Instant start = Instant.now();
|
||||
part1();
|
||||
Instant middle = Instant.now();
|
||||
part2();
|
||||
Instant end = Instant.now();
|
||||
IO.println("part 1: " + Duration.between(start, middle).toMillis() + " ms");
|
||||
IO.println("part 2: " + Duration.between(middle, end).toMillis() + " ms");
|
||||
}
|
||||
|
||||
private static void part1() {
|
||||
try (Scanner scanner = new Scanner(new FileReader("resources/inputs/06/input"))) {
|
||||
List<List<Integer>> inputs = new java.util.ArrayList<>();
|
||||
List<String> operations = new java.util.ArrayList<>();
|
||||
while (scanner.hasNextLine()) {
|
||||
String row = scanner.nextLine();
|
||||
if (row.contains("+")) {
|
||||
operations = Arrays.asList(row.split("\s+"));
|
||||
} else {
|
||||
List<Integer> numbers = Arrays.asList(row.split("\s+")).stream().map(Integer::parseInt).toList();
|
||||
inputs.add(numbers);
|
||||
}
|
||||
}
|
||||
Long grandTotal = 0L;
|
||||
for (int i = 0; i < operations.size(); i++) {
|
||||
String operation = operations.get(i);
|
||||
Long total = 0L;
|
||||
for (List<Integer> input : inputs) {
|
||||
Integer number = input.get(i);
|
||||
switch (operation) {
|
||||
case "+" -> total += number;
|
||||
case "*" -> {
|
||||
if (total == 0) {
|
||||
total = 1L;
|
||||
}
|
||||
total *= number;
|
||||
}
|
||||
}
|
||||
}
|
||||
grandTotal += total;
|
||||
}
|
||||
IO.println(grandTotal);
|
||||
} catch (Exception e) {
|
||||
IO.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static void part2() {
|
||||
try (Scanner scanner = new Scanner(new FileReader("resources/inputs/06/input"))) {
|
||||
String operations = "";
|
||||
List<String> numbers = new java.util.ArrayList<>();
|
||||
while (scanner.hasNextLine()) {
|
||||
String row = scanner.nextLine();
|
||||
if (row.contains("+")) {
|
||||
operations = row;
|
||||
} else {
|
||||
numbers.add(row);
|
||||
}
|
||||
}
|
||||
Long grandTotal = 0L;
|
||||
Long total = 0L;
|
||||
char currentOp = ' ';
|
||||
for (int i = 0; i < operations.length(); i++) {
|
||||
char operation = operations.charAt(i);
|
||||
if (operation != ' ') {
|
||||
currentOp = operation;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String row : numbers) {
|
||||
sb.append(row.charAt(i));
|
||||
}
|
||||
String column = sb.toString().trim();
|
||||
if (column.isBlank()) {
|
||||
grandTotal += total;
|
||||
total = 0L;
|
||||
} else {
|
||||
Long number = Long.parseLong(column);
|
||||
switch (currentOp) {
|
||||
case '+' -> total += number;
|
||||
case '*' -> {
|
||||
if (total == 0) {
|
||||
total = 1L;
|
||||
}
|
||||
total *= number;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// catch last problem
|
||||
grandTotal += total;
|
||||
// and done
|
||||
IO.println(grandTotal);
|
||||
} catch (Exception e) {
|
||||
IO.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user