Skip to content

Commit 372398b

Browse files
committed
feat: solve day 16 WIP
1 parent d36cf68 commit 372398b

File tree

5 files changed

+211
-7
lines changed

5 files changed

+211
-7
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.adventofcode.flashk.day16;
2+
3+
import com.adventofcode.flashk.common.Vector2;
4+
5+
public class Movement {
6+
7+
private Tile tile;
8+
private Vector2 direction;
9+
private boolean rotate;
10+
11+
public Vector2 getDirection() {
12+
if(rotate) {
13+
return new Vector2(0,0);
14+
}
15+
return new Vector2(direction);
16+
}
17+
18+
19+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.adventofcode.flashk.day16;
2+
3+
import com.adventofcode.flashk.common.Vector2;
4+
5+
import java.util.HashSet;
6+
import java.util.PriorityQueue;
7+
import java.util.Set;
8+
9+
public class ReindeerMaze {
10+
11+
private static final char WALL = '#';
12+
13+
private Tile[][] map;
14+
private int rows;
15+
private int cols;
16+
private Vector2 startPos;
17+
private Vector2 endPos;
18+
private Tile startTile;
19+
20+
private Set<Vector2> directions = Set.of(Vector2.right(), Vector2.left(), Vector2.up(), Vector2.down());
21+
22+
public ReindeerMaze(char[][] inputs) {
23+
rows = inputs.length;
24+
cols = inputs[0].length;
25+
map = new Tile[rows][cols];
26+
27+
for(int row = 0; row < rows; row++) {
28+
for(int col = 0; col < cols; col++) {
29+
map[row][col] = new Tile(new Vector2(col, row), inputs[row][col]);
30+
if(map[row][col].isStart()) {
31+
startPos = map[row][col].getPosition();
32+
startTile = map[row][col];
33+
} else if(map[row][col].isEnd()) {
34+
endPos = map[row][col].getPosition();
35+
}
36+
}
37+
}
38+
39+
}
40+
41+
// Rehacer con cola de prioridad
42+
43+
public long solveA2() {
44+
PriorityQueue<Tile> tiles = new PriorityQueue<>();
45+
46+
47+
// inicio
48+
startTile.setScore(0);
49+
tiles.add(startTile);
50+
while(!tiles.isEmpty()) {
51+
Tile nextTile = tiles.poll();
52+
nextTile.setVisited(true);
53+
54+
// Calcular adyacentes.
55+
Set<Tile> adjacentTiles = getAdjacents(nextTile);
56+
}
57+
}
58+
59+
private Set<Tile> getAdjacents(Tile nextTile) {
60+
61+
}
62+
63+
public long solveA() {
64+
long result = findPath(new Vector2(startPos), Vector2.right(), 0, Long.MAX_VALUE);
65+
66+
//paint();
67+
return result;
68+
}
69+
70+
// Casos base
71+
// - Llegar a E
72+
// - Llegar a un wall
73+
74+
private long findPath(Vector2 position, Vector2 direction, long score, long bestScore) {
75+
if(map[position.getY()][position.getX()].isWall()) {
76+
return Long.MAX_VALUE;
77+
} else if(map[position.getY()][position.getX()].isVisited()) {
78+
return Long.MAX_VALUE; // o puede que score, pero no queremos bucles infinitos
79+
} else if(map[position.getY()][position.getX()].isEnd()) {
80+
/*if(score < bestScore) {
81+
System.out.println("Result: "+score);
82+
paint();
83+
}*/
84+
return Math.min(score, bestScore);
85+
}
86+
87+
// Calcular adyacentes
88+
// - 4 posibles movimientos horizontales
89+
// - 2 posibles giros: izquierda, derecha, no tiene sentido girar 180º
90+
map[position.getY()][position.getX()].setVisited(true);
91+
92+
long newBestScore = bestScore;
93+
for(Vector2 newDir :directions) {
94+
95+
long additionalScore = direction.equals(newDir) ? 1 : 1001;
96+
Vector2 newPos = Vector2.transform(position, newDir);
97+
98+
99+
if(!map[newPos.getY()][newPos.getX()].isVisited()) {
100+
long newScore = findPath(newPos, newDir, score + additionalScore, bestScore);
101+
newBestScore = Math.min(newScore, newBestScore);
102+
}
103+
}
104+
105+
map[position.getY()][position.getX()].setVisited(false);
106+
107+
return newBestScore;
108+
109+
}
110+
111+
112+
private void paint(){
113+
for(int row = 0; row < rows; row++) {
114+
for(int col = 0; col < cols; col++) {
115+
if(map[row][col].isVisited()) {
116+
System.out.print('O');
117+
} else {
118+
System.out.print(map[row][col].getValue());
119+
}
120+
}
121+
System.out.println();
122+
}
123+
}
124+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.adventofcode.flashk.day16;
2+
3+
import com.adventofcode.flashk.common.Vector2;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import org.jetbrains.annotations.NotNull;
7+
8+
@Getter
9+
public class Tile implements Comparable<Tile>{
10+
11+
private static final char WALL = '#';
12+
private static final char START = 'S';
13+
private static final char END = 'E';
14+
15+
private final Vector2 position;
16+
private final char value;
17+
18+
@Setter
19+
private boolean visited = false;
20+
@Setter
21+
private long score = Long.MAX_VALUE;
22+
23+
24+
public Tile(Vector2 position, char value) {
25+
this.position = position;
26+
this.value = value;
27+
}
28+
29+
public boolean isWall() {
30+
return value == WALL;
31+
}
32+
33+
public boolean isStart(){
34+
return value == START;
35+
}
36+
37+
public boolean isEnd() {
38+
return value == END;
39+
}
40+
41+
@Override
42+
public int compareTo(@NotNull Tile o) {
43+
return Long.compare(this.score, o.score);
44+
}
45+
}

src/test/java/com/adventofcode/flashk/day16/Day16Test.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,24 @@ public class Day16Test extends PuzzleTest {
3434
public void testSolvePart1Sample() {
3535

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

39-
assertEquals(0L,0L);
40+
assertEquals(7036L,reindeerMaze.solveA());
41+
}
42+
43+
@Test
44+
@Order(1)
45+
@Tag(TestTag.PART_1)
46+
@Tag(TestTag.SAMPLE)
47+
@DisplayName(TestDisplayName.PART_1_SAMPLE)
48+
public void testSolvePart1Sample2() {
49+
50+
// Read input file
51+
char[][] inputs = Input.read2DCharArray(INPUT_FOLDER, TestFilename.INPUT_FILE_SINGLE_SAMPLE_2);
52+
ReindeerMaze reindeerMaze = new ReindeerMaze(inputs);
53+
54+
assertEquals(11048L,reindeerMaze.solveA());
4055
}
4156

4257
@Test
@@ -47,9 +62,10 @@ public void testSolvePart1Sample() {
4762
public void testSolvePart1Input() {
4863

4964
// Read input file
50-
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
65+
char[][] inputs = Input.read2DCharArray(INPUT_FOLDER, TestFilename.INPUT_FILE);
66+
ReindeerMaze reindeerMaze = new ReindeerMaze(inputs);
5167

52-
System.out.println("Solution: ");
68+
System.out.println("Solution: "+reindeerMaze.solveA());
5369
assertEquals(0L,0L);
5470

5571
}
@@ -62,7 +78,7 @@ public void testSolvePart1Input() {
6278
public void testSolvePart2Sample() {
6379

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

6783
assertEquals(0L,0L);
6884
}
@@ -75,7 +91,7 @@ public void testSolvePart2Sample() {
7591
public void testSolvePart2Input() {
7692

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

8096
System.out.println("Solution: ");
8197
assertEquals(0L,0L);

src/test/resources/inputs

0 commit comments

Comments
 (0)