Skip to content

Commit 4dc226e

Browse files
committed
Implemented 2017 Day 22 Part 2
1 parent 7e5d196 commit 4dc226e

File tree

1 file changed

+56
-1
lines changed
  • src/main/java/com/sbaars/adventofcode/year17/days

1 file changed

+56
-1
lines changed

src/main/java/com/sbaars/adventofcode/year17/days/Day22.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ public class Day22 extends Day2017 {
88
private static final int RIGHT = 1;
99
private static final int DOWN = 2;
1010
private static final int LEFT = 3;
11+
private static final int CLEAN = 0;
12+
private static final int WEAKENED = 1;
13+
private static final int INFECTED = 2;
14+
private static final int FLAGGED = 3;
1115

1216
private static class Point {
1317
int x, y;
@@ -91,6 +95,57 @@ public Object part1() {
9195

9296
@Override
9397
public Object part2() {
94-
return 0;
98+
String[] grid = dayStrings();
99+
Map<Point, Integer> nodes = new HashMap<>();
100+
101+
// Parse initial grid
102+
int centerY = grid.length / 2;
103+
int centerX = grid[0].length() / 2;
104+
for (int y = 0; y < grid.length; y++) {
105+
for (int x = 0; x < grid[y].length(); x++) {
106+
if (grid[y].charAt(x) == '#') {
107+
nodes.put(new Point(x - centerX, y - centerY), INFECTED);
108+
}
109+
}
110+
}
111+
112+
// Start virus carrier at center facing up
113+
Point carrier = new Point(0, 0);
114+
int direction = UP;
115+
int infections = 0;
116+
117+
// Simulate 10000000 bursts
118+
for (int burst = 0; burst < 10000000; burst++) {
119+
int currentState = nodes.getOrDefault(carrier, CLEAN);
120+
121+
// Turn based on current node state
122+
switch (currentState) {
123+
case CLEAN -> direction = (direction + 3) % 4; // Turn left
124+
case INFECTED -> direction = (direction + 1) % 4; // Turn right
125+
case FLAGGED -> direction = (direction + 2) % 4; // Reverse
126+
// WEAKENED: Don't turn
127+
}
128+
129+
// Update node state
130+
int newState = (currentState + 1) % 4;
131+
if (newState == INFECTED) {
132+
infections++;
133+
}
134+
if (newState == CLEAN) {
135+
nodes.remove(carrier);
136+
} else {
137+
nodes.put(new Point(carrier.x, carrier.y), newState);
138+
}
139+
140+
// Move forward
141+
switch (direction) {
142+
case UP -> carrier.y--;
143+
case RIGHT -> carrier.x++;
144+
case DOWN -> carrier.y++;
145+
case LEFT -> carrier.x--;
146+
}
147+
}
148+
149+
return infections;
95150
}
96151
}

0 commit comments

Comments
 (0)