Compare commits

..

4 Commits

Author SHA1 Message Date
6a55ce95f1 clean up imports 2025-12-07 22:52:16 -05:00
609571d004 got it right the first time... except for overflow. Gotta remember Long exists. 2025-12-07 22:49:46 -05:00
8804c70a53 ah, cephalapod math 2025-12-06 08:36:05 -05:00
e79e352d7a day five in the books 2025-12-05 22:52:49 -05:00
3 changed files with 281 additions and 0 deletions

100
src/Day05.java Normal file
View File

@@ -0,0 +1,100 @@
package src;
import java.io.FileReader;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Day05 {
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/05/input"))) {
List<Pair<Long, Long>> ranges = new ArrayList<>();
List<Long> ingredients = new ArrayList<>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.contains("-")) {
String[] parts = line.split("-");
long first = Long.parseLong(parts[0]);
long second = Long.parseLong(parts[1]);
ranges.add(new Pair<>(first, second));
} else if (!line.isBlank()) {
ingredients.add(Long.parseLong(line));
}
}
int count = 0;
for (Long ingredient : ingredients) {
for (Pair<Long, Long> range : ranges) {
if (ingredient >= range.head && ingredient <= range.tail) {
count++;
break;
}
}
}
IO.println(count);
} catch (Exception e) {
IO.println(e.getMessage());
}
}
private static void part2() {
try (Scanner scanner = new Scanner(new FileReader("resources/inputs/05/input"))) {
List<Pair<Long, Long>> ranges = new ArrayList<>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.contains("-")) {
String[] parts = line.split("-");
long first = Long.parseLong(parts[0]);
long second = Long.parseLong(parts[1]);
ranges.add(new Pair<>(first, second));
}
}
Pair<Long, Long> result = ranges.stream()
.sorted((a, b) -> Long.compare(a.head, b.head))
.reduce(
// first is count, second is end of range covered so far
new Pair<>(0L, 0L),
(total, curr) -> {
if (total.tail >= curr.tail) {
// this range was subsumed by a previous range
return total;
}
if (total.tail >= curr.head) {
// this range was partially included
return new Pair<>(total.head + (curr.tail - total.tail), curr.tail);
}
// this range was not previously included at all
// +1 to include both endpoints
return new Pair<>(total.head + (curr.tail - curr.head) + 1, curr.tail);
},
// merge function doesn't actually get used here but might as well write it
(a, b) -> new Pair<>(a.head + b.head, Long.max(a.tail, b.tail)));
IO.println(result.head);
} catch (Exception e) {
IO.println(e.getMessage());
}
}
static class Pair<T, U> {
public final T head;
public final U tail;
public Pair(T first, U second) {
this.head = first;
this.tail = second;
}
}
}

107
src/Day06.java Normal file
View 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());
}
}
}

74
src/Day07.java Normal file
View File

@@ -0,0 +1,74 @@
package src;
import java.io.FileReader;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Day07 {
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/07/input"))) {
int i = 0;
int splits = 0;
Set<Integer> beams = new HashSet<>();
while (scanner.hasNextLine()) {
String row = scanner.nextLine();
if (i == 0) {
beams.add(row.indexOf("S"));
} else {
for (int j = 0; j < row.length(); j++) {
if (row.charAt(j) == '^' && beams.contains(j)) {
beams.add(j - 1);
beams.add(j + 1);
beams.remove(j);
splits++;
}
}
}
i++;
}
IO.println(splits);
} catch (Exception e) {
IO.println(e.getMessage());
}
}
private static void part2() {
try (Scanner scanner = new Scanner(new FileReader("resources/inputs/07/input"))) {
int i = 0;
Map<Integer, Long> beams = new HashMap<>();
while (scanner.hasNextLine()) {
String row = scanner.nextLine();
if (i == 0) {
beams.put(row.indexOf("S"), 1L);
} else {
for (int j = 0; j < row.length(); j++) {
if (row.charAt(j) == '^' && beams.containsKey(j)) {
Long count = beams.remove(j);
beams.merge(j - 1, count, (a, b) -> a + b);
beams.merge(j + 1, count, (a, b) -> a + b);
}
}
}
i++;
}
IO.println(beams.values().stream().reduce(0L, Long::sum));
} catch (Exception e) {
IO.println(e.getMessage());
}
}
}