-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAdventOfCode13.java
58 lines (52 loc) · 1.78 KB
/
AdventOfCode13.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.List;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
public class AdventOfCode13 {
static int mirror(List<String> lines) {
return range(0, lines.size()).boxed()
.collect(groupingBy(lines::get, toList()))
.values().stream()
.<Integer>flatMap(indexes -> {
var state = new Object() { int index = -2; };
return indexes.stream().sorted().mapMulti((index, consumer) -> {
if (state.index + 1 == index) {
consumer.accept(state.index);
}
state.index = index;
});
})
.filter(index -> range(0, index).filter(i -> index - i >= 0 && index + 1 + i < lines.size())
.allMatch(i -> lines.get(index - i).equals(lines.get(index + 1 + i))))
.findFirst().map(index -> 1 + index).orElseThrow();
}
static int summarizingMirrors(String input) {
var puzzles = input.split("\n\n");
var lines = puzzles[0].lines().toList();
var puzzle1Columns = range(0, lines.getFirst().length())
.mapToObj(i -> lines.stream().map(line -> "" + line.charAt(i)).collect(joining()))
.toList();
var puzzle2Lines = puzzles[1].lines().toList();
return mirror(puzzle1Columns) + 100 * mirror(puzzle2Lines);
}
public static void main(String[] args) {
var input = """
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
""";
System.out.println(summarizingMirrors(input));
}
}