-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay25.java
92 lines (74 loc) · 2.68 KB
/
Day25.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
80
81
82
83
84
85
86
87
88
89
90
91
92
package net.eugenpaul.adventofcode.y2021.day25;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import lombok.Getter;
import net.eugenpaul.adventofcode.helper.Direction;
import net.eugenpaul.adventofcode.helper.MapOfSimplePos;
import net.eugenpaul.adventofcode.helper.SimplePos;
import net.eugenpaul.adventofcode.helper.SolutionTemplate;
public class Day25 extends SolutionTemplate {
@Getter
private long steps;
public static void main(String[] args) {
Day25 puzzle = new Day25();
puzzle.doPuzzleFromFile("y2021/day25/puzzle1.txt");
}
@Override
public boolean doEvent(List<String> eventData) {
Map<SimplePos, Direction> area = MapOfSimplePos.initMap(eventData, v -> {
try {
return Direction.fromArrow(v);
} catch (IllegalArgumentException e) {
return null;
}
});
steps = 0;
boolean moved = false;
final int maxX = eventData.get(0).length();
final int maxY = eventData.size();
do {
Map<SimplePos, Direction> nextStep = new HashMap<>();
moved = doStep(maxX, maxY, area, nextStep, Direction.E);
area = nextStep;
nextStep = new HashMap<>();
moved = doStep(maxX, maxY, area, nextStep, Direction.S) || moved;
area = nextStep;
steps++;
} while (moved);
logger.log(Level.INFO, () -> "steps : " + getSteps());
return true;
}
private boolean doStep(int maxX, int maxY, Map<SimplePos, Direction> area, Map<SimplePos, Direction> nextStep, Direction currentDirection) {
boolean moved = false;
for (int x = 0; x < maxX; x++) {
for (int y = 0; y < maxY; y++) {
var pos = new SimplePos(x, y);
var type = area.get(pos);
if (type == currentDirection) {
var newPos = coputeNewPos(maxX, maxY, pos, type);
if (!area.containsKey(newPos)) {
nextStep.put(newPos, type);
moved = true;
} else {
nextStep.put(pos, type);
}
} else if (type != null) {
nextStep.put(pos, type);
}
}
}
return moved;
}
private SimplePos coputeNewPos(int maxX, int maxY, SimplePos pos, Direction type) {
var newPos = pos.moveNew(type);
if (newPos.getX() == maxX) {
newPos.setX(0);
}
if (newPos.getY() == maxY) {
newPos.setY(0);
}
return newPos;
}
}