-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay15.java
68 lines (59 loc) · 2.49 KB
/
Day15.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
59
60
61
62
63
64
65
66
67
68
package com.sbaars.adventofcode.year22.days;
import com.sbaars.adventofcode.common.grid.InfiniteGrid;
import com.sbaars.adventofcode.common.location.Loc;
import com.sbaars.adventofcode.common.location.Range;
import com.sbaars.adventofcode.year22.Day2022;
import java.util.List;
import java.util.stream.IntStream;
import static com.sbaars.adventofcode.common.Direction.fourDirections;
import static com.sbaars.adventofcode.common.grid.InfiniteGrid.toInfiniteGrid;
import static com.sbaars.adventofcode.util.AoCUtils.allPairs;
import static com.sbaars.adventofcode.util.DataMapper.readString;
import static java.util.Arrays.stream;
public class Day15 extends Day2022 {
public Day15() {
super(15);
}
public static void main(String[] args) {
new Day15().printParts();
System.out.println(new Day15().part3());
}
@Override
public Object part1() {
List<Range> posList = input();
InfiniteGrid g = posList.stream().flatMap(Range::flatten).collect(toInfiniteGrid('X'));
return IntStream.range(-1000000, 5000000) // These values were found by trial-and-error
.mapToObj(i -> new Loc(i, 2000000))
.filter(l -> posList.stream().anyMatch(p -> p.inDiamond(l) && g.get(l).isEmpty()))
.count();
}
@Override
public Object part2() {
List<Range> posList = input();
Range target = new Range(new Loc(0, 0), new Loc(4000000, 4000000));
List<Range> lines = posList.stream()
.flatMap(p -> stream(fourDirections()).map(d -> new Range(d.move(p.start, p.distance()), d.turn().move(p.start, p.distance()))))
.toList();
return allPairs(lines).flatMap(p -> p.a().intersectsWith(p.b()).stream())
.filter(target::inRange)
.flatMap(Loc::fourDirs)
.filter(l -> posList.stream().noneMatch(p -> p.inDiamond(l)))
.mapToLong(l -> l.x * 4000000 + l.y)
.findAny()
.getAsLong();
}
public Object part3() {
List<Range> posList = input();
Range target = new Range(new Loc(0, 0), new Loc(4000000, 4000000));
return input().stream()
.flatMap(p -> stream(fourDirections()).flatMap(d -> d.move(p.start, p.distance() + 1).walk(d.turnSteps(3), p.distance() + 1)))
.filter(target::inRange)
.filter(l -> posList.stream().noneMatch(p -> p.inDiamond(l)))
.mapToLong(l -> l.x * 4000000 + l.y)
.findAny()
.getAsLong();
}
private List<Range> input() {
return dayStream().map(s -> readString(s, "Sensor at x=%n, y=%n: closest beacon is at x=%n, y=%n", Range.class)).toList();
}
}