-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay11.java
58 lines (48 loc) · 1.37 KB
/
Day11.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
package com.sbaars.adventofcode.year17.days;
import com.sbaars.adventofcode.year17.Day2017;
public class Day11 extends Day2017 {
public Day11() {
super(11);
}
public static void main(String[] args) {
new Day11().printParts();
}
private int calculateDistance(int x, int y, int z) {
return Math.max(Math.abs(x), Math.max(Math.abs(y), Math.abs(z)));
}
@Override
public Object part1() {
String[] steps = day().trim().split(",");
// Using cube coordinates (x, y, z) where x + y + z = 0
int x = 0, y = 0, z = 0;
for (String step : steps) {
switch (step) {
case "n" -> { y++; z--; }
case "s" -> { y--; z++; }
case "ne" -> { x++; z--; }
case "sw" -> { x--; z++; }
case "nw" -> { x--; y++; }
case "se" -> { x++; y--; }
}
}
return calculateDistance(x, y, z);
}
@Override
public Object part2() {
String[] steps = day().trim().split(",");
int x = 0, y = 0, z = 0;
int maxDistance = 0;
for (String step : steps) {
switch (step) {
case "n" -> { y++; z--; }
case "s" -> { y--; z++; }
case "ne" -> { x++; z--; }
case "sw" -> { x--; z++; }
case "nw" -> { x--; y++; }
case "se" -> { x++; y--; }
}
maxDistance = Math.max(maxDistance, calculateDistance(x, y, z));
}
return maxDistance;
}
}