-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay17.java
61 lines (50 loc) · 1.86 KB
/
Day17.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
package com.sbaars.adventofcode.year21.days;
import com.sbaars.adventofcode.year21.Day2021;
import java.awt.*;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import static com.sbaars.adventofcode.util.DataMapper.readString;
import static java.lang.Long.MIN_VALUE;
import static java.lang.Math.toIntExact;
public class Day17 extends Day2021 {
public Day17() {
super(17);
}
public static void main(String[] args) {
new Day17().printParts();
}
@Override
public Object part1() {
return getSol().max().getAsLong();
}
private LongStream getSol() {
var in = readString(day().trim(), "target area: x=%n..%n, y=%n..%n", Target.class);
var area = new Area(new Point(toIntExact(in.xStart), toIntExact(in.yStart)), new Point(toIntExact(in.xEnd), toIntExact(in.yEnd)));
return IntStream.range(-200, 300).boxed().flatMap(x -> IntStream.range(-200, 300).mapToObj(y -> new Point(x, y))).mapToLong(p -> simulateSteps(area, p));
}
public long simulateSteps(Area target, Point p) {
Point curr = new Point(0, 0);
long highest = 0;
while (curr.y > target.topLeft.y && !target.inArea(curr)) {
curr = new Point(curr.x + p.x, curr.y + p.y);
p = new Point(p.x > 0 ? p.x - 1 : (p.x < 0 ? p.x - 1 : p.x), p.y - 1);
if (curr.y > highest) highest = curr.y;
if (curr.x == 0 && (curr.y < target.topLeft.y || curr.y > target.bottomRight.y)) break;
}
if (target.inArea(curr)) {
return highest;
}
return MIN_VALUE;
}
@Override
public Object part2() {
return getSol().filter(e -> e != MIN_VALUE).count();
}
public record Target(long xStart, long xEnd, long yStart, long yEnd) {
}
public record Area(Point topLeft, Point bottomRight) {
public boolean inArea(Point p) {
return p.x >= topLeft.x && p.y >= topLeft.y && p.x <= bottomRight.x && p.y <= bottomRight.y;
}
}
}