-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay1.java
64 lines (57 loc) · 1.53 KB
/
Day1.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
package com.sbaars.adventofcode.year16.days;
import com.sbaars.adventofcode.common.Day;
import com.sbaars.adventofcode.common.Direction;
import com.sbaars.adventofcode.year16.Day2016;
import java.awt.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Day1 extends Day2016 {
public Day1() {
super(1);
}
public static void main(String[] args) {
Day d = new Day1();
d.downloadIfNotDownloaded();
d.printParts();
// d.submitPart1();
d.submitPart2();
}
record Move(boolean right, int dist) {
}
@Override
public Object part1() {
List<Move> moves = dayStream(", ")
.map(String::trim)
.map(s -> new Move(s.charAt(0) == 'R', Integer.parseInt(s.substring(1))))
.toList();
Point p = new Point(0, 0);
Direction dir = Direction.NORTH;
for (Move m : moves) {
dir = dir.turn(m.right);
p = dir.moveFix(p, m.dist);
}
return Math.abs(p.x) + Math.abs(p.y);
}
@Override
public Object part2() {
List<Move> moves = dayStream(", ")
.map(String::trim)
.map(s -> new Move(s.charAt(0) == 'R', Integer.parseInt(s.substring(1))))
.toList();
Point p = new Point(0, 0);
Direction dir = Direction.NORTH;
Set<Point> visited = new HashSet<>();
visited.add(p);
for (Move m : moves) {
dir = dir.turn(m.right);
for (int i = 0; i < m.dist; i++) {
p = dir.moveFix(p, 1);
if (!visited.add(p)) {
return Math.abs(p.x) + Math.abs(p.y);
}
}
}
return 0;
}
}