Skip to content

Commit

Permalink
feat: solve day 16 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 16, 2024
1 parent d36cf68 commit 372398b
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 7 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/adventofcode/flashk/day16/Movement.java
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 src/main/java/com/adventofcode/flashk/day16/ReindeerMaze.java
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();
}
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/adventofcode/flashk/day16/Tile.java
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);
}
}
28 changes: 22 additions & 6 deletions src/test/java/com/adventofcode/flashk/day16/Day16Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,24 @@ public class Day16Test extends PuzzleTest {
public void testSolvePart1Sample() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
char[][] inputs = Input.read2DCharArray(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
ReindeerMaze reindeerMaze = new ReindeerMaze(inputs);

assertEquals(0L,0L);
assertEquals(7036L,reindeerMaze.solveA());
}

@Test
@Order(1)
@Tag(TestTag.PART_1)
@Tag(TestTag.SAMPLE)
@DisplayName(TestDisplayName.PART_1_SAMPLE)
public void testSolvePart1Sample2() {

// Read input file
char[][] inputs = Input.read2DCharArray(INPUT_FOLDER, TestFilename.INPUT_FILE_SINGLE_SAMPLE_2);
ReindeerMaze reindeerMaze = new ReindeerMaze(inputs);

assertEquals(11048L,reindeerMaze.solveA());
}

@Test
Expand All @@ -47,9 +62,10 @@ public void testSolvePart1Sample() {
public void testSolvePart1Input() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
char[][] inputs = Input.read2DCharArray(INPUT_FOLDER, TestFilename.INPUT_FILE);
ReindeerMaze reindeerMaze = new ReindeerMaze(inputs);

System.out.println("Solution: ");
System.out.println("Solution: "+reindeerMaze.solveA());
assertEquals(0L,0L);

}
Expand All @@ -62,7 +78,7 @@ public void testSolvePart1Input() {
public void testSolvePart2Sample() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
char[][] inputs = Input.read2DCharArray(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);

assertEquals(0L,0L);
}
Expand All @@ -75,7 +91,7 @@ public void testSolvePart2Sample() {
public void testSolvePart2Input() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
char[][] inputs = Input.read2DCharArray(INPUT_FOLDER, TestFilename.INPUT_FILE);

System.out.println("Solution: ");
assertEquals(0L,0L);
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/inputs

0 comments on commit 372398b

Please sign in to comment.