@@ -8,6 +8,10 @@ public class Day22 extends Day2017 {
8
8
private static final int RIGHT = 1 ;
9
9
private static final int DOWN = 2 ;
10
10
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 ;
11
15
12
16
private static class Point {
13
17
int x , y ;
@@ -91,6 +95,57 @@ public Object part1() {
91
95
92
96
@ Override
93
97
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 ;
95
150
}
96
151
}
0 commit comments