generated from Flashky/advent-of-code-yyyy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
211 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.adventofcode.flashk.day16; | ||
|
||
import com.adventofcode.flashk.common.Vector2; | ||
|
||
public class Movement { | ||
|
||
private Tile tile; | ||
private Vector2 direction; | ||
private boolean rotate; | ||
|
||
public Vector2 getDirection() { | ||
if(rotate) { | ||
return new Vector2(0,0); | ||
} | ||
return new Vector2(direction); | ||
} | ||
|
||
|
||
} |
124 changes: 124 additions & 0 deletions
124
src/main/java/com/adventofcode/flashk/day16/ReindeerMaze.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package com.adventofcode.flashk.day16; | ||
|
||
import com.adventofcode.flashk.common.Vector2; | ||
|
||
import java.util.HashSet; | ||
import java.util.PriorityQueue; | ||
import java.util.Set; | ||
|
||
public class ReindeerMaze { | ||
|
||
private static final char WALL = '#'; | ||
|
||
private Tile[][] map; | ||
private int rows; | ||
private int cols; | ||
private Vector2 startPos; | ||
private Vector2 endPos; | ||
private Tile startTile; | ||
|
||
private Set<Vector2> directions = Set.of(Vector2.right(), Vector2.left(), Vector2.up(), Vector2.down()); | ||
|
||
public ReindeerMaze(char[][] inputs) { | ||
rows = inputs.length; | ||
cols = inputs[0].length; | ||
map = new Tile[rows][cols]; | ||
|
||
for(int row = 0; row < rows; row++) { | ||
for(int col = 0; col < cols; col++) { | ||
map[row][col] = new Tile(new Vector2(col, row), inputs[row][col]); | ||
if(map[row][col].isStart()) { | ||
startPos = map[row][col].getPosition(); | ||
startTile = map[row][col]; | ||
} else if(map[row][col].isEnd()) { | ||
endPos = map[row][col].getPosition(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
// Rehacer con cola de prioridad | ||
|
||
public long solveA2() { | ||
PriorityQueue<Tile> tiles = new PriorityQueue<>(); | ||
|
||
|
||
// inicio | ||
startTile.setScore(0); | ||
tiles.add(startTile); | ||
while(!tiles.isEmpty()) { | ||
Tile nextTile = tiles.poll(); | ||
nextTile.setVisited(true); | ||
|
||
// Calcular adyacentes. | ||
Set<Tile> adjacentTiles = getAdjacents(nextTile); | ||
} | ||
} | ||
|
||
private Set<Tile> getAdjacents(Tile nextTile) { | ||
|
||
} | ||
|
||
public long solveA() { | ||
long result = findPath(new Vector2(startPos), Vector2.right(), 0, Long.MAX_VALUE); | ||
|
||
//paint(); | ||
return result; | ||
} | ||
|
||
// Casos base | ||
// - Llegar a E | ||
// - Llegar a un wall | ||
|
||
private long findPath(Vector2 position, Vector2 direction, long score, long bestScore) { | ||
if(map[position.getY()][position.getX()].isWall()) { | ||
return Long.MAX_VALUE; | ||
} else if(map[position.getY()][position.getX()].isVisited()) { | ||
return Long.MAX_VALUE; // o puede que score, pero no queremos bucles infinitos | ||
} else if(map[position.getY()][position.getX()].isEnd()) { | ||
/*if(score < bestScore) { | ||
System.out.println("Result: "+score); | ||
paint(); | ||
}*/ | ||
return Math.min(score, bestScore); | ||
} | ||
|
||
// Calcular adyacentes | ||
// - 4 posibles movimientos horizontales | ||
// - 2 posibles giros: izquierda, derecha, no tiene sentido girar 180º | ||
map[position.getY()][position.getX()].setVisited(true); | ||
|
||
long newBestScore = bestScore; | ||
for(Vector2 newDir :directions) { | ||
|
||
long additionalScore = direction.equals(newDir) ? 1 : 1001; | ||
Vector2 newPos = Vector2.transform(position, newDir); | ||
|
||
|
||
if(!map[newPos.getY()][newPos.getX()].isVisited()) { | ||
long newScore = findPath(newPos, newDir, score + additionalScore, bestScore); | ||
newBestScore = Math.min(newScore, newBestScore); | ||
} | ||
} | ||
|
||
map[position.getY()][position.getX()].setVisited(false); | ||
|
||
return newBestScore; | ||
|
||
} | ||
|
||
|
||
private void paint(){ | ||
for(int row = 0; row < rows; row++) { | ||
for(int col = 0; col < cols; col++) { | ||
if(map[row][col].isVisited()) { | ||
System.out.print('O'); | ||
} else { | ||
System.out.print(map[row][col].getValue()); | ||
} | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.adventofcode.flashk.day16; | ||
|
||
import com.adventofcode.flashk.common.Vector2; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
@Getter | ||
public class Tile implements Comparable<Tile>{ | ||
|
||
private static final char WALL = '#'; | ||
private static final char START = 'S'; | ||
private static final char END = 'E'; | ||
|
||
private final Vector2 position; | ||
private final char value; | ||
|
||
@Setter | ||
private boolean visited = false; | ||
@Setter | ||
private long score = Long.MAX_VALUE; | ||
|
||
|
||
public Tile(Vector2 position, char value) { | ||
this.position = position; | ||
this.value = value; | ||
} | ||
|
||
public boolean isWall() { | ||
return value == WALL; | ||
} | ||
|
||
public boolean isStart(){ | ||
return value == START; | ||
} | ||
|
||
public boolean isEnd() { | ||
return value == END; | ||
} | ||
|
||
@Override | ||
public int compareTo(@NotNull Tile o) { | ||
return Long.compare(this.score, o.score); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule inputs
updated
from f0b2be to 331405