-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC112017.java
66 lines (54 loc) · 2.44 KB
/
AoC112017.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
package com.adventofcode.aoc2017;
import static com.adventofcode.aoc2017.AoC112017.Direction.N;
import static com.adventofcode.aoc2017.AoC112017.Direction.NE;
import static com.adventofcode.aoc2017.AoC112017.Direction.NW;
import static com.adventofcode.aoc2017.AoC112017.Direction.S;
import static com.adventofcode.aoc2017.AoC112017.Direction.SE;
import static com.adventofcode.aoc2017.AoC112017.Direction.SW;
import static com.adventofcode.utils.Utils.getFirstString;
import static com.adventofcode.utils.Utils.itoa;
import static com.adventofcode.utils.Utils.splitOnRegex;
import static java.lang.Math.abs;
import static java.lang.Math.max;
import com.adventofcode.Solution;
import com.adventofcode.utils.Pair;
import java.util.Map;
import java.util.stream.Stream;
class AoC112017 implements Solution {
private static final Map<Direction, Pair<Integer, Integer>> NEIGHBOURS = Map.of( N,
new Pair<>( 0, -2 ), NE, new Pair<>( +1, -1 ), SE, new Pair<>( +1, +1 ), S,
new Pair<>( 0, +2 ), SW, new Pair<>( -1, +1 ), NW, new Pair<>( -1, -1 ) );
private static final Pair<Integer, Integer> ZERO = new Pair<>( 0, 0 );
@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) {
//using "doubled coordinates"
var position = ZERO;
var maxDistance = 0;
final var directions = splitOnRegex( getFirstString( input ), "," ).map( String::toUpperCase )
.map( Direction::valueOf ).map( NEIGHBOURS::get ).toList();
for ( final var direction : directions ) {
position = addTiles( position, direction );
maxDistance = max( maxDistance, getDistance( ZERO, position ) );
}
return itoa( first ? getDistance( ZERO, position ) : maxDistance );
}
private Pair<Integer, Integer> addTiles(final Pair<Integer, Integer> tileA,
final Pair<Integer, Integer> tileB) {
return new Pair<>( tileA.getFirst() + tileB.getFirst(), tileA.getSecond() + tileB.getSecond() );
}
private int getDistance(final Pair<Integer, Integer> tileA, final Pair<Integer, Integer> tileB) {
final int dCol = abs( tileA.getFirst() - tileB.getFirst() );
final int dRow = abs( tileA.getSecond() - tileB.getSecond() );
return dCol + max( 0, (dRow - dCol) / 2 );
}
enum Direction {
N, NE, SE, S, SW, NW,
}
}