-
Notifications
You must be signed in to change notification settings - Fork 17
/
Day12.java
60 lines (51 loc) · 1.77 KB
/
Day12.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
package com.sbaars.adventofcode.year20.days;
import com.sbaars.adventofcode.common.Direction;
import com.sbaars.adventofcode.year20.Day2020;
import java.awt.*;
import java.util.List;
import static com.sbaars.adventofcode.common.Direction.EAST;
import static com.sbaars.adventofcode.common.Direction.turnDegrees;
import static com.sbaars.adventofcode.util.DataMapper.readString;
import static java.lang.Math.abs;
public class Day12 extends Day2020 {
public Day12() {
super(12);
}
public static void main(String[] args) {
new Day12().printParts();
}
@Override
public Object part1() {
List<Flight> input = convertInput();
Direction face = EAST;
Point location = new Point(0, 0);
for (Flight f : input) {
switch (f.dir) {
case 'L', 'R' -> face = face.turnDegrees(f.distance, f.dir == 'R');
case 'F' -> location = face.move(location, f.distance);
default -> location = Direction.getByDir(f.dir).move(location, f.distance);
}
}
return abs(location.x) + abs(location.y);
}
private List<Flight> convertInput() {
return dayStream().map(e -> readString(e, "%c%i", Flight.class)).toList();
}
@Override
public Object part2() {
List<Flight> input = convertInput();
Point waypoint = new Point(10, -1);
Point location = new Point(0, 0);
for (Flight f : input) {
switch (f.dir) {
case 'L', 'R' -> waypoint = turnDegrees(waypoint, f.distance, f.dir == 'R');
case 'F' ->
location = new Point(location.x + (waypoint.x * f.distance), location.y + (waypoint.y * f.distance));
default -> waypoint = Direction.getByDir(f.dir).move(waypoint, f.distance);
}
}
return abs(location.x) + abs(location.y);
}
public record Flight(char dir, int distance) {
}
}