Skip to content

Commit 87f8c15

Browse files
committed
2021 day 25
1 parent 3bbd34f commit 87f8c15

File tree

3 files changed

+280
-1
lines changed

3 files changed

+280
-1
lines changed

src/main/java/com/jeffrpowell/adventofcode/Launcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class Launcher
1111
{
12-
public static final Solution<?> DAY = new Day23();
12+
public static final Solution<?> DAY = new Day25();
1313

1414
public static void main(String[] args) {
1515
InputStream puzzleInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(getInputFileName(DAY));
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package com.jeffrpowell.adventofcode.aoc2021;
2+
3+
import java.awt.geom.Point2D;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Set;
9+
import java.util.stream.Stream;
10+
11+
import com.jeffrpowell.adventofcode.Point2DUtils;
12+
import com.jeffrpowell.adventofcode.inputparser.InputParser;
13+
import com.jeffrpowell.adventofcode.inputparser.InputParserFactory;
14+
15+
public class Day25 extends Solution2021<List<String>> {
16+
Map<Point2D, Occupant> grid;
17+
Set<Point2D> unlockedPts;
18+
boolean changed = true;
19+
int maxX;
20+
int maxY;
21+
22+
@Override
23+
public int getDay() {
24+
return 25;
25+
}
26+
27+
@Override
28+
public InputParser<List<String>> getInputParser() {
29+
return InputParserFactory.getTokenSVParser("");
30+
}
31+
32+
@Override
33+
protected String part1(List<List<String>> input) {
34+
grid = new HashMap<>();
35+
unlockedPts = new HashSet<>();
36+
maxX = input.get(0).size() - 1;
37+
maxY = input.size() - 1;
38+
for (int y = 0; y < input.size(); y++) {
39+
for (int x = 0; x < input.get(0).size(); x++) {
40+
String value = input.get(y).get(x);
41+
Occupant o = Occupant.fromString(value);
42+
Point2D pt = new Point2D.Double(x, y);
43+
grid.put(pt, o);
44+
if (o == Occupant.EMPTY) {
45+
unlockedPts.add(pt);
46+
}
47+
}
48+
}
49+
long i = 0;
50+
while(changed) {
51+
i++;
52+
changed = false;
53+
stepEast();
54+
stepSouth();
55+
}
56+
return Long.toString(i);
57+
}
58+
59+
private void stepEast() {
60+
Map<Point2D, Point2D> moves = new HashMap<>();
61+
for (Point2D dest : unlockedPts) {
62+
Point2D src = getWesternNeighbor(dest);
63+
if (grid.get(src) == Occupant.EAST) {
64+
moves.put(src, dest);
65+
}
66+
}
67+
moves.entrySet().stream().forEach(entry -> {
68+
grid.put(entry.getKey(), Occupant.EMPTY);
69+
grid.put(entry.getValue(), Occupant.EAST);
70+
});
71+
unlockedPts.removeAll(moves.values());
72+
unlockedPts.addAll(moves.keySet());
73+
if (!moves.isEmpty()) {
74+
changed = true;
75+
}
76+
}
77+
78+
private void stepSouth() {
79+
Map<Point2D, Point2D> moves = new HashMap<>();
80+
for (Point2D dest : unlockedPts) {
81+
Point2D src = getNorthernNeighbor(dest);
82+
if (grid.get(src) == Occupant.SOUTH) {
83+
moves.put(src, dest);
84+
}
85+
}
86+
moves.entrySet().stream().forEach(entry -> {
87+
grid.put(entry.getKey(), Occupant.EMPTY);
88+
grid.put(entry.getValue(), Occupant.SOUTH);
89+
});
90+
unlockedPts.removeAll(moves.values());
91+
unlockedPts.addAll(moves.keySet());
92+
if (!moves.isEmpty()) {
93+
changed = true;
94+
}
95+
}
96+
97+
@Override
98+
protected String part2(List<List<String>> input) {
99+
// TODO Auto-generated method stub
100+
return null;
101+
}
102+
103+
enum Occupant {
104+
EMPTY, EAST, SOUTH;
105+
106+
public static Occupant fromString(String value) {
107+
if (value.equals("v")) {
108+
return Occupant.SOUTH;
109+
}
110+
else if (value.equals(">")) {
111+
return Occupant.EAST;
112+
}
113+
else {
114+
return Occupant.EMPTY;
115+
}
116+
}
117+
}
118+
119+
private Stream<Point2D> getNorthernNeighbors(Set<Point2D> pts) {
120+
return pts.stream().map(this::getNorthernNeighbor);
121+
}
122+
123+
private Point2D getNorthernNeighbor(Point2D pt) {
124+
Point2D p = Point2DUtils.applyVectorToPt(new Point2D.Double(0, -1), pt);
125+
if (p.getY() < 0) {
126+
p.setLocation(p.getX(), maxY);
127+
}
128+
return p;
129+
}
130+
131+
private Stream<Point2D> getWesternNeighbors(Set<Point2D> pts) {
132+
return pts.stream().map(this::getWesternNeighbor);
133+
}
134+
135+
private Point2D getWesternNeighbor(Point2D pt) {
136+
Point2D p = Point2DUtils.applyVectorToPt(new Point2D.Double(-1, 0), pt);
137+
if (p.getX() < 0) {
138+
p.setLocation(maxX, p.getY());
139+
}
140+
return p;
141+
}
142+
}

0 commit comments

Comments
 (0)