Skip to content

Commit 0650bfd

Browse files
committed
feat: solve day 15
1 parent 24f41bc commit 0650bfd

File tree

4 files changed

+482
-8
lines changed

4 files changed

+482
-8
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
package com.adventofcode.flashk.day15;
2+
3+
import com.adventofcode.flashk.common.Array2DUtil;
4+
import com.adventofcode.flashk.common.Vector2;
5+
import org.apache.commons.lang3.StringUtils;
6+
7+
import java.util.HashMap;
8+
import java.util.Iterator;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class ScaledWarehouseWoes {
13+
14+
private static final char ROBOT = '@';
15+
private static final char EMPTY = '.';
16+
private static final char WALL = '#';
17+
private static final char BOX = 'O';
18+
private static final char BOX_START = '[';
19+
private static final char BOX_END = ']';
20+
21+
private static final char UP = '^';
22+
private static final char DOWN = 'v';
23+
private static final char LEFT = '<';
24+
private static final char RIGHT = '>';
25+
26+
27+
private Map<Character, Vector2> directions = new HashMap<>();
28+
29+
private int rows = 0;
30+
private int cols = 0;
31+
private char[][] map;
32+
private final char[] movements;
33+
private Vector2 robotPos;
34+
35+
public ScaledWarehouseWoes(List<String> inputs) {
36+
37+
boolean isMap = true;
38+
int row = 0;
39+
40+
// Map
41+
Iterator<String> inputIterator = inputs.iterator();
42+
43+
while(inputIterator.hasNext() && isMap) {
44+
String input = inputIterator.next();
45+
if(input.equals(StringUtils.EMPTY)) {
46+
isMap = false;
47+
} else {
48+
addRow(input, row);
49+
row++;
50+
}
51+
inputIterator.remove();
52+
}
53+
54+
// movements
55+
56+
movements = String.join(StringUtils.EMPTY, inputs).toCharArray();
57+
58+
// Robot directions
59+
directions.put(UP, Vector2.down());
60+
directions.put(DOWN, Vector2.up());
61+
directions.put(LEFT, Vector2.left());
62+
directions.put(RIGHT, Vector2.right());
63+
}
64+
65+
private void addRow(String input, int row) {
66+
67+
if(map == null) {
68+
rows = input.length();
69+
cols = rows * 2;
70+
map = new char[rows][cols];
71+
}
72+
73+
char[] inputRow = input.toCharArray();
74+
int col = 0;
75+
for(char tile : inputRow) {
76+
switch (tile) {
77+
case ROBOT -> {
78+
map[row][col++] = ROBOT;
79+
map[row][col++] = EMPTY;
80+
}
81+
case BOX -> {
82+
map[row][col++] = BOX_START;
83+
map[row][col++] = BOX_END;
84+
}
85+
default -> {
86+
map[row][col++] = tile;
87+
map[row][col++] = tile;
88+
}
89+
}
90+
91+
}
92+
93+
String scaledInput = new String(map[row]);
94+
int robotCol = scaledInput.indexOf(ROBOT);
95+
if(robotCol != -1) {
96+
robotPos = new Vector2(robotCol, row);
97+
}
98+
99+
}
100+
101+
public long solveB() {
102+
int movementIndex = 0;
103+
Array2DUtil.paint(map);
104+
while(movementIndex < movements.length) {
105+
System.out.print("Move: "+ String.valueOf(movements[movementIndex]));
106+
move(movements[movementIndex]);
107+
Array2DUtil.paint(map);
108+
movementIndex++;
109+
}
110+
111+
return sumGPS();
112+
}
113+
114+
private void move(char movement) {
115+
Vector2 direction = directions.get(movement);
116+
boolean canMove;
117+
if(movement == LEFT || movement == RIGHT) {
118+
canMove = moveH(direction, robotPos.getY(), robotPos.getX());
119+
} else {
120+
canMove = moveV(direction, robotPos.getY(), robotPos.getX());
121+
if(canMove) {
122+
commitMoveV(direction, robotPos.getY(), robotPos.getX());
123+
}
124+
}
125+
126+
// Update old robot position and move it
127+
128+
if(canMove) {
129+
map[robotPos.getY()][robotPos.getX()] = EMPTY;
130+
robotPos.transform(direction);
131+
}
132+
133+
}
134+
135+
private boolean moveH(Vector2 direction, int row, int col) {
136+
137+
if(map[row][col] == EMPTY) {
138+
return true;
139+
} else if(map[row][col] == WALL) {
140+
return false;
141+
}
142+
143+
int nextRow = direction.getY()+row;
144+
int nextCol = direction.getX()+col;
145+
146+
boolean canMove = moveH(direction, nextRow, nextCol);
147+
148+
if(canMove) {
149+
map[nextRow][nextCol] = map[row][col];
150+
}
151+
152+
return canMove;
153+
}
154+
155+
private boolean moveV(Vector2 direction, int row, int col) {
156+
if(map[row][col] == EMPTY) {
157+
return true;
158+
} else if(map[row][col] == WALL) {
159+
return false;
160+
}
161+
162+
int nextRow = direction.getY()+row;
163+
164+
boolean canMove;
165+
if(map[row][col] == BOX_START) {
166+
canMove = moveV(direction, nextRow, col) && moveV(direction, nextRow,col+1);
167+
} else if(map[row][col] == BOX_END){
168+
canMove = moveV(direction, nextRow, col) && moveV(direction, nextRow, col-1);
169+
} else {
170+
// ROBOT
171+
canMove = moveV(direction, nextRow, col);
172+
}
173+
174+
return canMove;
175+
176+
}
177+
178+
private void commitMoveV(Vector2 direction, int row, int col) {
179+
// TODO Puede que esto no haga falta
180+
if(map[row][col] == EMPTY) {
181+
return;
182+
} else if(map[row][col] == WALL) {
183+
return;
184+
}
185+
186+
int nextRow = direction.getY()+row;
187+
188+
//boolean canMove;
189+
if(map[row][col] == BOX_START) {
190+
commitMoveV(direction, nextRow, col);
191+
commitMoveV(direction, nextRow,col+1);
192+
193+
map[nextRow][col] = map[row][col];
194+
map[nextRow][col+1] = map[row][col+1];
195+
map[row][col] = EMPTY;
196+
map[row][col+1] = EMPTY;
197+
} else if(map[row][col] == BOX_END){
198+
commitMoveV(direction, nextRow, col);
199+
commitMoveV(direction, nextRow, col-1);
200+
map[nextRow][col] = map[row][col];
201+
map[nextRow][col-1] = map[row][col-1];
202+
map[row][col] = EMPTY;
203+
map[row][col-1] = EMPTY;
204+
205+
} else {
206+
// ROBOT
207+
commitMoveV(direction, nextRow, col);
208+
map[nextRow][col] = map[row][col];
209+
map[row][col] = EMPTY;
210+
}
211+
212+
}
213+
214+
private long sumGPS() {
215+
long result = 0;
216+
217+
for(int row = 0; row < rows; row++){
218+
for(int col = 0; col < cols; col++) {
219+
if(map[row][col] == BOX_START) {
220+
result += (100L*row)+col;
221+
}
222+
}
223+
}
224+
return result;
225+
}
226+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.adventofcode.flashk.day15;
2+
3+
import com.adventofcode.flashk.common.Vector2;
4+
import org.apache.commons.lang3.StringUtils;
5+
6+
import java.util.HashMap;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
public class WarehouseWoes {
12+
13+
private static final char EMPTY = '.';
14+
private static final char WALL = '#';
15+
private static final char BOX = 'O';
16+
private static final char UP = '^';
17+
private static final char DOWN = 'v';
18+
private static final char LEFT = '<';
19+
private static final char RIGHT = '>';
20+
21+
22+
private Map<Character,Vector2> directions = new HashMap<>();
23+
24+
private int rows = 0;
25+
private int cols = 0;
26+
private char[][] map;
27+
private final char[] movements;
28+
private Vector2 robotPos;
29+
30+
public WarehouseWoes(List<String> inputs) {
31+
32+
boolean isMap = true;
33+
int row = 0;
34+
35+
// Map
36+
Iterator<String> inputIterator = inputs.iterator();
37+
38+
while(inputIterator.hasNext() && isMap) {
39+
String input = inputIterator.next();
40+
if(input.equals(StringUtils.EMPTY)) {
41+
isMap = false;
42+
} else {
43+
addRow(input, row);
44+
row++;
45+
}
46+
inputIterator.remove();
47+
}
48+
49+
// movements
50+
51+
movements = String.join(StringUtils.EMPTY, inputs).toCharArray();
52+
53+
// Robot directions
54+
directions.put(UP, Vector2.down());
55+
directions.put(DOWN, Vector2.up());
56+
directions.put(LEFT, Vector2.left());
57+
directions.put(RIGHT, Vector2.right());
58+
}
59+
60+
private void addRow(String input, int row) {
61+
62+
if(map == null) {
63+
rows = input.length();
64+
cols = rows;
65+
map = new char[rows][];
66+
}
67+
68+
map[row] = input.toCharArray();
69+
70+
int robotCol = input.indexOf("@");
71+
if(robotCol != -1) {
72+
robotPos = new Vector2(robotCol, row);
73+
}
74+
75+
}
76+
77+
public long solveA() {
78+
79+
int movementIndex = 0;
80+
//Array2DUtil.paint(map);
81+
while(movementIndex < movements.length) {
82+
83+
Vector2 direction = directions.get(movements[movementIndex]);
84+
// while si el robot pudiese repetir el mismo movimiento hasta encontrar un bache
85+
// if si solo realiza un movimiento una vez
86+
if(move(direction, robotPos.getY(), robotPos.getX())){
87+
map[robotPos.getY()][robotPos.getX()] = EMPTY;
88+
robotPos.transform(direction);
89+
//Array2DUtil.paint(map);
90+
}
91+
92+
movementIndex++;
93+
}
94+
95+
return sumGPS();
96+
}
97+
98+
private long sumGPS() {
99+
long result = 0;
100+
for(int row = 0; row < rows; row++){
101+
for(int col = 0; col < cols; col++) {
102+
if(map[row][col] == BOX) {
103+
result += (100L*row)+col;
104+
}
105+
}
106+
}
107+
return result;
108+
}
109+
110+
111+
private boolean move(Vector2 direction, int row, int col) {
112+
113+
if(map[row][col] == EMPTY) {
114+
return true;
115+
} else if(map[row][col] == WALL) {
116+
return false;
117+
}
118+
119+
int nextRow = direction.getY()+row;
120+
int nextCol = direction.getX()+col;
121+
122+
boolean canMove = move(direction, nextRow, nextCol);
123+
124+
if(canMove) {
125+
map[nextRow][nextCol] = map[row][col];
126+
}
127+
128+
return canMove;
129+
}
130+
}

0 commit comments

Comments
 (0)