got it right the first time... except for overflow. Gotta remember Long exists.
This commit is contained in:
76
src/Day07.java
Normal file
76
src/Day07.java
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package src;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user