-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay13.java
55 lines (46 loc) · 1.69 KB
/
Day13.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
package com.sbaars.adventofcode.year23.days;
import com.sbaars.adventofcode.common.Pair;
import com.sbaars.adventofcode.common.grid.InfiniteGrid;
import com.sbaars.adventofcode.year23.Day2023;
import java.util.List;
import java.util.Map;
import static com.sbaars.adventofcode.util.AoCUtils.connectedPairs;
import static com.sbaars.adventofcode.util.AoCUtils.zip;
import static java.util.Arrays.stream;
import static java.util.stream.LongStream.range;
public class Day13 extends Day2023 {
public Day13() {
super(13);
}
public static void main(String[] args) {
new Day13().printParts();
}
@Override
public Object part1() {
return solve(0);
}
@Override
public Object part2() {
return solve(1);
}
private long solve(long errorsAllowed) {
return stream(day().split("\n\n"))
.map(InfiniteGrid::new)
.mapToLong(g -> findReflection(g.columnValues(), errorsAllowed) + findReflection(g.rowValues(), errorsAllowed) * 100)
.sum();
}
private long findReflection(Map<Long, List<Character>> chars, long errorsAllowed) {
return connectedPairs(chars.entrySet().stream())
.filter(r -> zip(r.a().getValue().stream(), r.b().getValue().stream()).filter(c -> c.a() != c.b()).count() +
range(0, r.a().getKey())
.mapToObj(i -> new Pair<>(r.a().getKey() - i - 1, r.b().getKey() + i + 1))
.filter(p -> chars.containsKey(p.a()) && chars.containsKey(p.b()))
.flatMap(p -> zip(chars.get(p.a()).stream(), chars.get(p.b()).stream()))
.filter(p -> p.a() != p.b())
.count() == errorsAllowed
)
.mapToLong(p -> p.b().getKey())
.max()
.orElse(0);
}
}