@@ -45,40 +45,14 @@ - (void)day22:(NSArray *)inputs part:(NSNumber *)part
45
45
}
46
46
47
47
int bestManaSpent = 5000000 ;
48
-
49
- NSMutableArray *spellList = nil ;
50
-
51
- if (NO )
52
- {
53
- // bossHp = 14;
54
- // bossDamage = 8;
55
- // mana = 250;
56
- // hp = 10;
57
-
58
- spellList = [[NSMutableArray alloc ] initWithArray: @[@" poison" ,@" recharge" ,@" shield" ,@" poison" ,@" recharge" ,@" shield" ,@" poison" ,@" recharge" ,@" shield" ,@" magic missile" ,@" poison" ,@" magic missile" ]];
59
- }
60
-
48
+
61
49
for (NSString *spell in [spells allKeys ])
62
50
{
63
- NSString *spellToPerform = spell;
64
-
65
- if (spellList != nil )
66
- {
67
- if ([spellList count ] != 0 )
68
- {
69
- spellToPerform = spellList[0 ];
70
- [spellList removeObjectAtIndex: 0 ];
71
- }
72
- else
73
- {
74
- spellToPerform = nil ;
75
- }
76
- }
77
51
78
52
NSMutableArray *spellsPerformed = [[NSMutableArray alloc ] init ];
79
53
80
54
[self doRPGTurn: spells
81
- spellToPerform: spellToPerform
55
+ spellToPerform: spell
82
56
hpRemaining: hp
83
57
manaRemaining: mana
84
58
armor: 0
@@ -87,7 +61,6 @@ - (void)day22:(NSArray *)inputs part:(NSNumber *)part
87
61
currentStatusEffects: nil
88
62
totalManaSpent: 0
89
63
bestManaSpent: &bestManaSpent
90
- spellList: spellList
91
64
spellsPerformed: spellsPerformed
92
65
hardMode: ([part intValue ] ==2 )
93
66
];
@@ -108,7 +81,6 @@ - (void) doRPGTurn:(NSDictionary*)spells
108
81
currentStatusEffects:(NSMutableArray *)currentStatusEffects
109
82
totalManaSpent:(int )totalManaSpent
110
83
bestManaSpent:(int *)bestManaSpent
111
- spellList:(NSMutableArray *)spellList
112
84
spellsPerformed:(NSArray *)spellsPerformed
113
85
hardMode:(BOOL )hardMode
114
86
{
@@ -134,18 +106,14 @@ - (void) doRPGTurn:(NSDictionary*)spells
134
106
}
135
107
}
136
108
137
- // NSLog(@"\n\n");
138
- // NSLog(@"Player turn p(%d,%d,%d) vs b(%d)\n",hpRemaining,manaRemaining,armor,bossHpRemaining);
139
109
// check status effects
140
110
currentStatusEffects = [self doStatusEffects: currentStatusEffects spells: spells hpRemaining: &hpRemaining manaRemaining: &manaRemaining armor: &armor bossHpRemaining: &bossHpRemaining];
141
111
142
112
// check if boss is dead
143
- // NSLog(@"After status effects, p(%d,%d,%d) vs b(%d)\n",hpRemaining,manaRemaining,armor,bossHpRemaining);
144
113
if (bossHpRemaining <= 0 )
145
114
{
146
115
if (totalManaSpent < *bestManaSpent)
147
116
{
148
- // NSLog(@"%@ led to boss death at cost of %d (vs %d) (%d,%d,%d)\n",newSpellsPerformed,totalManaSpent,*bestManaSpent,hpRemaining,manaRemaining,armor);
149
117
*bestManaSpent = totalManaSpent;
150
118
}
151
119
return ;
@@ -162,7 +130,7 @@ - (void) doRPGTurn:(NSDictionary*)spells
162
130
armor += [spells[spellToPerform][@" armor" ] intValue ];
163
131
hpRemaining += [spells[spellToPerform][@" hpGain" ] intValue ];
164
132
bossHpRemaining -= [spells[spellToPerform][@" damage" ] intValue ];
165
- // NSLog(@"cast %@ for %d mana (%d left): %d armor, %d hp, %d damage\n",spellToPerform,[spells[spellToPerform][@"cost"] intValue],manaRemaining,[spells[spellToPerform][@"armor"] intValue],[spells[spellToPerform][@"hpGain"] intValue],[spells[spellToPerform][@"damage"] intValue]);
133
+
166
134
int turns = [spells[spellToPerform][@" turns" ] intValue ];
167
135
if (turns != 0 )
168
136
{
@@ -180,42 +148,34 @@ - (void) doRPGTurn:(NSDictionary*)spells
180
148
{
181
149
if (totalManaSpent < *bestManaSpent)
182
150
{
183
- // NSLog(@"%@ led to boss death at cost of %d (vs %d) (%d,%d,%d)\n",newSpellsPerformed,totalManaSpent,*bestManaSpent,hpRemaining,manaRemaining,armor);
184
151
*bestManaSpent = totalManaSpent;
185
152
}
186
153
return ;
187
154
}
188
- // NSLog(@"end player turn p(%d,%d,%d) vs b(%d)\n",hpRemaining,manaRemaining,armor,bossHpRemaining);
189
155
// end player turn
190
156
191
157
192
158
193
159
// do boss turn
194
- // NSLog(@"Boss turn p(%d,%d,%d) vs b(%d)\n",hpRemaining,manaRemaining,armor,bossHpRemaining);
195
-
196
160
// check status effects
197
161
currentStatusEffects = [self doStatusEffects: currentStatusEffects spells: spells hpRemaining: &hpRemaining manaRemaining: &manaRemaining armor: &armor bossHpRemaining: &bossHpRemaining];
198
162
199
163
// check if boss is dead
200
- // NSLog(@"After status effects, p(%d,%d,%d) vs b(%d)\n",hpRemaining,manaRemaining,armor,bossHpRemaining);
201
164
if (bossHpRemaining <= 0 )
202
165
{
203
166
if (totalManaSpent < *bestManaSpent)
204
167
{
205
- // NSLog(@"%@ led to boss death at cost of %d (vs %d) (%d,%d,%d)\n",newSpellsPerformed,totalManaSpent,*bestManaSpent,hpRemaining,manaRemaining,armor);
206
168
*bestManaSpent = totalManaSpent;
207
169
}
208
170
return ;
209
171
}
210
172
211
173
// boss attack
212
174
hpRemaining -= max (1 ,bossDamage - armor);
213
- // NSLog(@"boss does %d damage, player now at %d\n",max(1,bossDamage-armor),hpRemaining);
214
175
215
176
// check if player is dead
216
177
if (hpRemaining <= 0 )
217
178
{
218
- // NSLog(@"%@ led to player death\n",spellsPerformed);
219
179
return ;
220
180
}
221
181
// end boss turn
@@ -238,19 +198,6 @@ - (void) doRPGTurn:(NSDictionary*)spells
238
198
{
239
199
NSString *nextSpellToPerform = spell;
240
200
241
- if (spellList != nil )
242
- {
243
- if ([spellList count ] != 0 )
244
- {
245
- nextSpellToPerform = spellList[0 ];
246
- [spellList removeObjectAtIndex: 0 ];
247
- }
248
- else
249
- {
250
- nextSpellToPerform = nil ;
251
- }
252
- }
253
-
254
201
BOOL cantPerform = NO ;
255
202
for (NSDictionary *effect in currentStatusEffects)
256
203
{
@@ -272,7 +219,6 @@ - (void) doRPGTurn:(NSDictionary*)spells
272
219
currentStatusEffects: currentStatusEffects
273
220
totalManaSpent: totalManaSpent
274
221
bestManaSpent: bestManaSpent
275
- spellList: spellList
276
222
spellsPerformed: newSpellsPerformed
277
223
hardMode: hardMode
278
224
];
@@ -285,11 +231,11 @@ - (void) doRPGTurn:(NSDictionary*)spells
285
231
286
232
287
233
- (NSMutableArray *)doStatusEffects:(NSArray *)currentStatusEffects
288
- spells:(NSDictionary *)spells
289
- hpRemaining:(int *)hpRemaining
290
- manaRemaining:(int *)manaRemaining
291
- armor:(int *)armor
292
- bossHpRemaining:(int *)bossHpRemaining
234
+ spells:(NSDictionary *)spells
235
+ hpRemaining:(int *)hpRemaining
236
+ manaRemaining:(int *)manaRemaining
237
+ armor:(int *)armor
238
+ bossHpRemaining:(int *)bossHpRemaining
293
239
{
294
240
NSMutableArray *statusEffects;
295
241
if (currentStatusEffects == nil )
@@ -314,34 +260,16 @@ - (NSMutableArray *)doStatusEffects:(NSArray*)currentStatusEffects
314
260
315
261
if (turnsRemaining >= 1 )
316
262
{
317
- if ([statusEffect[@" name" ] isEqualToString: @" poison" ])
318
- {
319
- *bossHpRemaining -= [spells[@" poison" ][@" damagePerTurn" ] intValue ];
320
-
321
- // NSLog(@"boss takes poison damage for %d, left at %d (%d remaining)\n",[spells[@"poison"][@"damagePerTurn"] intValue],*bossHpRemaining,turnsRemaining-1);
322
-
323
- }
324
- else if ([statusEffect[@" name" ] isEqualToString: @" shield" ])
325
- {
326
- // NSLog(@"shield still active (%d remaining)\n",turnsRemaining-1);
327
- }
328
- else if ([statusEffect[@" name" ] isEqualToString: @" recharge" ])
329
- {
330
- *manaRemaining += [spells[@" recharge" ][@" manaGainPerTurn" ] intValue ];
331
- // NSLog(@"you recharge %d mana, now at %d (%d remaining)\n",[spells[@"recharge"][@"manaGainPerTurn"] intValue],*manaRemaining,turnsRemaining-1);
332
- }
263
+ *bossHpRemaining -= [spells[statusEffect[@" name" ]][@" damagePerTurn" ] intValue ];
264
+ *manaRemaining += [spells[statusEffect[@" name" ]][@" manaGainPerTurn" ] intValue ];
333
265
334
266
turnsRemaining--;
335
267
statusEffect[@" turnsRemaining" ] = [NSNumber numberWithInt: turnsRemaining];
336
268
337
269
}
338
270
else
339
271
{
340
- if ([statusEffect[@" name" ] isEqualToString: @" shield" ])
341
- {
342
- // NSLog(@"shield wears off\n");
343
- *armor -= [spells[@" shield" ][@" armor" ] intValue ];
344
- }
272
+ *armor -= [spells[statusEffect[@" name" ]][@" armor" ] intValue ];
345
273
346
274
[statusEffectsToRemove addObject: statusEffect];
347
275
}
0 commit comments