Compare commits

...

6 Commits

4 changed files with 308 additions and 42 deletions

View File

@@ -9,10 +9,9 @@ import java.util.Scanner;
public class Day04 { public class Day04 {
private static int neighbors = 0;
private static int score = 0; private static int score = 0;
private static int round = 0; private static int round = 0;
private static Map<String, Boolean> floor = new java.util.HashMap<>(); private static Map<String, Integer> floor = new java.util.HashMap<>();
public static void main(String[] args) { public static void main(String[] args) {
Instant start = Instant.now(); Instant start = Instant.now();
@@ -37,33 +36,20 @@ public class Day04 {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
String key = "%s-%s".formatted(i, rowIndex); String key = "%s-%s".formatted(i, rowIndex);
if (row[i].equals("@")) { if (row[i].equals("@")) {
floor.put(key, true); floor.put(key, 1);
} else {
floor.put(key, false);
} }
} }
rowIndex++; rowIndex++;
} }
floor.forEach((key, value) -> { floor.forEach((key, value) -> {
if (value) { if (value == 1) {
String[] coords = key.split("-"); String[] coords = key.split("-");
int x = Integer.parseInt(coords[0]); int x = Integer.parseInt(coords[0]);
int y = Integer.parseInt(coords[1]); int y = Integer.parseInt(coords[1]);
neighbors = 0; int neighbors = generateNeighborKeys(x, y).stream().reduce(
List.of( 0,
"%s-%s".formatted(x - 1, y), (t, coord) -> (t + floor.getOrDefault(coord, 0)),
"%s-%s".formatted(x + 1, y), Integer::sum);
"%s-%s".formatted(x, y - 1),
"%s-%s".formatted(x, y + 1),
"%s-%s".formatted(x + 1, y - 1),
"%s-%s".formatted(x - 1, y - 1),
"%s-%s".formatted(x + 1, y + 1),
"%s-%s".formatted(x - 1, y + 1)).forEach((coord) -> {
if (floor.getOrDefault(coord, false)) {
neighbors += 1;
}
});
if (neighbors < 4) { if (neighbors < 4) {
score += 1; score += 1;
} }
@@ -87,38 +73,25 @@ public class Day04 {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
String key = "%s-%s".formatted(i, rowIndex); String key = "%s-%s".formatted(i, rowIndex);
if (row[i].equals("@")) { if (row[i].equals("@")) {
floor.put(key, true); floor.put(key, 1);
} else {
floor.put(key, false);
} }
} }
rowIndex++; rowIndex++;
} }
while (true) { while (true) {
Map<String, Boolean> newFloor = new java.util.HashMap<>(floor); Map<String, Integer> newFloor = new java.util.HashMap<>(floor);
floor.forEach((key, value) -> { floor.forEach((key, value) -> {
if (value) { if (value == 1) {
String[] coords = key.split("-"); String[] coords = key.split("-");
int x = Integer.parseInt(coords[0]); int x = Integer.parseInt(coords[0]);
int y = Integer.parseInt(coords[1]); int y = Integer.parseInt(coords[1]);
neighbors = 0; int neighbors = generateNeighborKeys(x, y).stream().reduce(
List.of( 0,
"%s-%s".formatted(x - 1, y), (t, coord) -> (t + floor.getOrDefault(coord, 0)),
"%s-%s".formatted(x + 1, y), Integer::sum);
"%s-%s".formatted(x, y - 1),
"%s-%s".formatted(x, y + 1),
"%s-%s".formatted(x + 1, y - 1),
"%s-%s".formatted(x - 1, y - 1),
"%s-%s".formatted(x + 1, y + 1),
"%s-%s".formatted(x - 1, y + 1)).forEach((coord) -> {
if (floor.getOrDefault(coord, false)) {
neighbors += 1;
}
});
if (neighbors < 4) { if (neighbors < 4) {
round += 1; round += 1;
newFloor.put(key, false); newFloor.put(key, 0);
} }
} }
}); });
@@ -135,4 +108,16 @@ public class Day04 {
IO.println(e.getMessage()); IO.println(e.getMessage());
} }
} }
private static List<String> generateNeighborKeys(int x, int y) {
return List.of(
"%s-%s".formatted(x - 1, y),
"%s-%s".formatted(x + 1, y),
"%s-%s".formatted(x, y - 1),
"%s-%s".formatted(x, y + 1),
"%s-%s".formatted(x + 1, y - 1),
"%s-%s".formatted(x - 1, y - 1),
"%s-%s".formatted(x + 1, y + 1),
"%s-%s".formatted(x - 1, y + 1));
}
} }

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