Skip to content

Commit 37369d3

Browse files
committed
2020 day 20 - trying to piece together the edges, but something about the hash matching and orientation is off
1 parent 0a1847e commit 37369d3

File tree

1 file changed

+111
-26
lines changed
  • src/main/java/com/jeffrpowell/adventofcode/aoc2020

1 file changed

+111
-26
lines changed

src/main/java/com/jeffrpowell/adventofcode/aoc2020/Day20.java

Lines changed: 111 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,115 @@ protected String part2(List<String> input) {
4949
Map<Long, Tile> tiles = parseTiles(input);
5050
Map<Integer, List<Tile>> tilesByHash = buildTilesByHash(tiles.values());
5151
Map<Tile.Location, List<Long>> classifiedTiles = classifyTiles(tiles.values(), tilesByHash);
52-
Map<Point2D, Long> placedTiles = new HashMap<>();
52+
Map<Point2D, Tile> placedTiles = new HashMap<>();
53+
54+
//Set the top-left corner piece
5355
Long firstTile = classifiedTiles.get(Tile.Location.CORNER).get(0);
54-
placedTiles.put(new Point2D.Double(0, 0), firstTile);
56+
placedTiles.put(new Point2D.Double(0, 0), tiles.get(firstTile));
57+
System.out.println(tiles.get(firstTile).raw.stream().collect(Collectors.joining("\n")));
58+
System.out.println();
5559
for (Integer hash : tiles.get(firstTile).hashCircle) {
5660
List<Tile> matchingTiles = tilesByHash.get(hash);
5761
if (matchingTiles.size() > 1) {
5862
tiles.get(firstTile).setOrientation(Direction.RIGHT, hash);
59-
Tile nextTile = matchingTiles.stream().filter(t -> t.id != firstTile).findAny().get();
60-
nextTile.setOrientation(Direction.LEFT, hash);
61-
placedTiles.put(new Point2D.Double(1, 0), nextTile.id);
63+
System.out.println(tiles.get(firstTile).finalOrientation.stream().collect(Collectors.joining("\n")));
64+
System.out.println();
6265
break;
6366
}
6467
}
68+
Tile lastPlacedTile = tiles.get(firstTile);
69+
70+
// Set the top edge pieces
71+
boolean done = false;
72+
int column = 0;
73+
while (!done) {
74+
int hash = lastPlacedTile.getNextHash(Direction.RIGHT); //traveling along the top edge pieces, left-to-right; so reference the last piece's right hash
75+
final long lastId = lastPlacedTile.id;
76+
List<Tile> matchingTiles = tilesByHash.get(hash);
77+
if (matchingTiles.size() == 1) {
78+
done = true;
79+
}
80+
else {
81+
column++;
82+
Tile nextTile = matchingTiles.stream().filter(t -> t.id != lastId).findAny().get();
83+
System.out.println(nextTile.raw.stream().collect(Collectors.joining("\n")));
84+
System.out.println();
85+
nextTile.setOrientation(Direction.LEFT, hash);
86+
System.out.println(nextTile.finalOrientation.stream().collect(Collectors.joining("\n")));
87+
System.out.println();
88+
placedTiles.put(new Point2D.Double(column, 0), nextTile);
89+
lastPlacedTile = nextTile;
90+
}
91+
}
92+
93+
// Set the right edge pieces
94+
done = false;
95+
int row = 0;
96+
while (!done) {
97+
int hash = lastPlacedTile.getNextHash(Direction.UP);
98+
final long lastId = lastPlacedTile.id;
99+
List<Tile> matchingTiles = tilesByHash.get(hash);
100+
if (matchingTiles.size() == 1) {
101+
done = true;
102+
}
103+
else {
104+
row++;
105+
Tile nextTile = matchingTiles.stream().filter(t -> t.id != lastId).findAny().get();
106+
nextTile.setOrientation(Direction.DOWN, hash);
107+
placedTiles.put(new Point2D.Double(column, row), nextTile);
108+
lastPlacedTile = nextTile;
109+
}
110+
}
111+
112+
// Set the bottom edge pieces
113+
done = false;
114+
while (!done) {
115+
int hash = lastPlacedTile.getNextHash(Direction.RIGHT);
116+
final long lastId = lastPlacedTile.id;
117+
List<Tile> matchingTiles = tilesByHash.get(hash);
118+
if (matchingTiles.size() == 1) {
119+
done = true;
120+
}
121+
else {
122+
column--;
123+
Tile nextTile = matchingTiles.stream().filter(t -> t.id != lastId).findAny().get();
124+
nextTile.setOrientation(Direction.LEFT, hash);
125+
placedTiles.put(new Point2D.Double(column, row), nextTile);
126+
lastPlacedTile = nextTile;
127+
}
128+
}
129+
130+
// Set the right edge pieces
131+
done = false;
132+
while (!done) {
133+
int hash = lastPlacedTile.getNextHash(Direction.UP);
134+
final long lastId = lastPlacedTile.id;
135+
List<Tile> matchingTiles = tilesByHash.get(hash);
136+
if (matchingTiles.size() == 1) {
137+
done = true;
138+
}
139+
else {
140+
row--;
141+
Tile nextTile = matchingTiles.stream().filter(t -> t.id != lastId).findAny().get();
142+
nextTile.setOrientation(Direction.DOWN, hash);
143+
placedTiles.put(new Point2D.Double(column, row), nextTile);
144+
lastPlacedTile = nextTile;
145+
}
146+
}
147+
148+
System.out.println("\n\n-------------FINAL----------------");
149+
Tile def = new Tile(-1);
150+
def.finalOrientation = Stream.generate(() -> "..........").limit(10).collect(Collectors.toList());
151+
for (row = 0; row < 12; row++) {
152+
for (int line = 0; line < 10; line++) {
153+
for (int col = 0; col < 12; col++) {
154+
Tile t = placedTiles.getOrDefault(new Point2D.Double(col, row), def);
155+
System.out.print(" " + t.finalOrientation.get(line) + " ");
156+
}
157+
System.out.println();
158+
}
159+
System.out.println();
160+
}
65161
return "";
66162
}
67163

@@ -130,6 +226,7 @@ public Tile(long id) {
130226
this.hashCircle = new ArrayList<>();
131227
this.hashCircleFlipped = new ArrayList<>();
132228
this.usingFlippedHashCircle = false;
229+
this.finalOrientation = null;
133230
}
134231

135232
public void addLine(String line) {
@@ -238,6 +335,9 @@ public void calculateHashes() {
238335
hashCircleFlipped.add(reverseString(raw.get(0)).hashCode());
239336
hashCircleFlipped.add(reverseString(rawRotatedCW.get(0)).hashCode());
240337
hashCircleFlipped.add(raw.get(raw.size() - 1).hashCode());
338+
if (Stream.concat(hashCircle.stream(), hashCircleFlipped.stream()).distinct().count() < 8L) {
339+
System.out.println(this.id + " has duplicate edge hashes!");
340+
}
241341
}
242342

243343
private static String reverseString(String s) {
@@ -267,27 +367,12 @@ private List<String> rotateTileCW() {
267367

268368
@Override
269369
public String toString() {
270-
return Long.toString(id);
271-
}
272-
}
273-
274-
private static class SearchAttempt {
275-
Point2D pt;
276-
Long tile;
277-
278-
public SearchAttempt(Point2D pt, Long tile) {
279-
this.pt = pt;
280-
this.tile = tile;
370+
if (finalOrientation == null) {
371+
return Long.toString(id);
372+
}
373+
else {
374+
return finalOrientation.stream().map(s -> " " + s + " ").collect(Collectors.joining("\n"));
375+
}
281376
}
282-
283-
}
284-
285-
private static class PuzzleMat {
286-
Map<Point2D, Long> placedTiles;
287-
}
288-
289-
private static class Generator {
290-
Map<Long, Tile> tiles;
291-
Map<Tile.Location, List<Long>> classifiedTiles;
292377
}
293378
}

0 commit comments

Comments
 (0)