-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC172021.java
79 lines (67 loc) · 2.4 KB
/
AoC172021.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
69
70
71
72
73
74
75
76
77
78
79
package com.adventofcode.aoc2021;
import static java.lang.Math.max;
import static com.adventofcode.utils.Utils.atoi;
import static com.adventofcode.utils.Utils.itoa;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import com.adventofcode.Solution;
import com.adventofcode.utils.Pair;
class AoC172021 implements Solution {
private static final Pattern TARGET_AREA_REGEX = Pattern.compile(
"target area: x=(\\d+)..(\\d+), y=(-?\\d+)..(-?\\d+)" );
@Override
public String solveFirstPart( final Stream<String> input ) {
return solve( input, true );
}
@Override
public String solveSecondPart( final Stream<String> input ) {
return solve( input, false );
}
private String solve( final Stream<String> input, final boolean first ) {
final var targetArea = getTargetArea( input.findFirst().orElseThrow() );
int highestY = 0;
int count = 0;
for ( int velocityX = 0; velocityX <= targetArea.getSecond().x; velocityX++ ) {
for ( int velocityY = targetArea.getSecond().y; velocityY < -targetArea.getSecond().y; velocityY++ ) {
final var hit = launchProbe( new Pair<>( velocityX, velocityY ), targetArea );
if ( hit.isPresent() ) {
highestY = max( highestY, hit.get() );
count++;
}
}
}
return itoa( first ? highestY : count );
}
private Optional<Integer> launchProbe( final Pair<Integer, Integer> velocities,
final Pair<Point, Point> targetArea ) {
int velocityX = velocities.getFirst();
int velocityY = velocities.getSecond();
final Point topLeft = targetArea.getFirst();
final Point bottomRight = targetArea.getSecond();
int x = 0;
int y = 0;
int highestY = 0;
while ( x <= bottomRight.x && y >= bottomRight.y ) {
if ( topLeft.x <= x && y <= topLeft.y ) {
return Optional.of( highestY );
}
x += velocityX;
y += velocityY;
highestY = max( highestY, y );
velocityX -= velocityX > 0 ? 1 : 0;
velocityY -= 1;
}
return Optional.empty();
}
private Pair<Point, Point> getTargetArea( final String input ) {
final var matcher = TARGET_AREA_REGEX.matcher( input );
if ( !matcher.matches() ) {
throw new IllegalArgumentException();
}
final var topLeft = new Point( atoi( matcher.group( 1 ) ), atoi( matcher.group( 4 ) ) );
final var bottomRight = new Point( atoi( matcher.group( 2 ) ), atoi( matcher.group( 3 ) ) );
return new Pair<>( topLeft, bottomRight );
}
private record Point(int x, int y) {}
}