-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.rs
323 lines (259 loc) · 9.39 KB
/
part2.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// adventofcode - day 22
// part 2
use std::io::prelude::*;
use std::fs::File;
#[derive(Clone)]
struct Character {
name: String,
hp: u32,
mana: u32,
effects: Vec<Effect>,
}
#[derive(Clone)]
struct Effect {
name: String, // name of the effect
cost: u32, // how much mana does it cost?
dmg: u32, // how much damage does it deal?
def: u32, // how much armor does it provide?
healing: u32, // how much hp does it regenerate?
regen: u32, // how much mana does it regenerate?
turns: u32, // how many turns does it last?
selfcast: bool, // who is the target of this spell? the caster or his
// opponent?
}
impl Character {
fn new(name: String, hp: u32, mana: u32, effects: Option<Vec<Effect>>)
-> Character {
let effects = match effects {
Some(effects) => effects,
None => Vec::new(),
};
Character{
name: name,
hp: hp,
mana: mana,
effects: effects,
}
}
fn cast_spell(&mut self, other: &mut Character, spell: Effect) -> bool {
if spell.cost > self.mana {
// we cannot cast this spell if we don't have enough mana
return false;
}
// pay mana
self.mana -= spell.cost;
// drain is a special spell, because it affects both characters
// thats a somewhat dirty solution but i couldnt come up with a
// simpler/nicer one
if spell.name == "Drain" {
// the only important values are healing and dmg respectively
self.apply_spell( Effect{ name: "Drain".to_string(), cost: 73,
dmg: 0, def: 0, healing: 2, regen: 0,
turns: 1, selfcast: true} );
return
other.apply_spell( Effect{ name: "Drain".to_string(), cost: 73,
dmg: 2, def: 0, healing: 0, regen: 0,
turns: 1, selfcast: false} );
}
// who gets targeted by the spell?
let target = if spell.selfcast {
self
} else {
other
};
target.apply_spell(spell)
}
fn apply_spell(&mut self, spell: Effect) -> bool {
for effect in &self.effects {
// look through the whole list of effects and check whether it is
// already there. Each spell can only be active exactly once
// Note: we need the additional check for `!= 0` because we never
// remove expired spells
if spell.name == effect.name && effect.turns != 0 {
return false;
}
}
self.effects.push( spell );
true
}
#[allow(dead_code)]
fn print_status(&self) {
println!("- {} has {} HP, {} armor, and {} mana",
self.name, self.hp, self.get_armor_value(), self.mana);
println!("- Current Effects:");
for effect in &self.effects {
effect.print();
}
println!("");
}
#[allow(dead_code)]
// used for printing
fn get_armor_value(&self) -> u32{
let mut armor = 0;
for effect in &self.effects {
armor += effect.def;
}
armor
}
fn simulate_turn(&mut self) -> bool {
let mut damage_taken = 0;
let mut damage_blocked = 0;
let mut hp_regen = 0;
let mut mana_regen = 0;
for ref mut effect in &mut self.effects {
if effect.turns == 0 {
// we never remove expired spells, therefore we need to check
// whether they are still active
continue;
}
damage_taken += effect.dmg;
damage_blocked += effect.def;
hp_regen += effect.healing;
mana_regen += effect.regen;
effect.turns -= 1;
}
damage_taken = if damage_taken == 0 {
0
} else if damage_blocked >= damage_taken {
1
} else {
damage_taken - damage_blocked
};
self.mana += mana_regen;
self.hp += hp_regen;
if damage_taken >= self.hp {
self.hp = 0;
return false;
} else {
self.hp -= damage_taken;
return true;
}
}
fn bleed(&mut self) -> bool {
self.hp -= 1;
self.hp > 0
}
}
impl Effect {
#[allow(dead_code)]
fn print(&self) {
print!("-- {}: cost: {}, dmg: {}, armor: {}, healing: {}",
self.name, self.cost, self.dmg, self.def, self.healing);
println!(", mana-regen: {}, turns left: {}",
self.regen, self.turns);
}
}
fn main(){
println!("Advent of Code - day 22 | part 2");
let data = import_data();
let (boss, boss_effect) = create_boss(data);
let player = create_player();
let player_effects = create_player_effects();
// PART2 specific: we need a higher treshold to begin with
let result = round(player, boss, player_effects, &boss_effect, 0, 2000);
println!("Result: {}", result);
}
// current & treshold are required in order to avoid checking absurd and
// expensive routes, by keeping track of our best result we found so far
// and ensuring that we'll only check options which are cheaper
fn round(player: Character, boss: Character, spells: Vec<Effect>,
boss_effect: &Effect, current: u32, treshold: u32) -> u32 {
// create a copy of the spells (probably there's a rust-trick to do this
// without copying the spells over and over)
let spells_ = spells.clone();
let mut min_cost = treshold;
for spell in spells_ {
// we need new instances of both characters, due to the recursive calls
let mut pl = player.clone();
let mut b = boss.clone();
// Player's turn
// first check whether spell would even be a cheaper option
let spellcost = spell.cost;
if current + spellcost > min_cost {
continue;
}
// PART2 specific: Player loses 1 HP at the start of each of his turns
if ! pl.bleed() {
continue;
}
// regenerate mana, use shield, etc.
if ! pl.simulate_turn() {
continue;
}
// now, cast the spell and do your magic!
if ! pl.cast_spell(&mut b, spell) {
continue;
}
// afterwards, check whether we killed the boss!
if ! b.simulate_turn() {
return current + spellcost;
}
// turn of boss
// he always casts his 8 damage spell, and this should never fail
if ! b.cast_spell(&mut pl, boss_effect.clone()) {
unreachable!();
}
// check whether we survived his attack
if ! pl.simulate_turn() {
continue;
}
// check whether the boss died (due to poison, for example)
if ! b.simulate_turn() {
return current + spellcost;
}
// recursive call to the next round!
let cost = round(pl.clone(), b.clone(), spells.clone(), boss_effect,
current + spellcost, min_cost);
// keep track of minimum
if cost < min_cost {
min_cost = cost;
}
}
min_cost
}
fn create_player_effects() -> Vec<Effect> {
let mut effects = Vec::new();
effects.push( Effect{ name: "Magic Missile".to_string(), cost: 53,
dmg: 4, def: 0, healing: 0, regen: 0,
turns: 1, selfcast: false} );
effects.push( Effect{ name: "Drain".to_string(), cost: 73,
dmg: 2, def: 0, healing: 2, regen: 0,
turns: 1, selfcast: false} );
effects.push( Effect{ name: "Shield".to_string(), cost: 113,
dmg: 0, def: 7, healing: 0, regen: 0,
turns: 6, selfcast: true} );
effects.push( Effect{ name: "Poison".to_string(), cost: 173,
dmg: 3, def: 0, healing: 0, regen: 0,
turns: 6, selfcast: false} );
effects.push( Effect{ name: "Recharge".to_string(), cost: 229,
dmg: 0, def: 0, healing: 0, regen: 101,
turns: 5, selfcast: true} );
effects
}
fn create_boss(data: String) -> (Character, Effect) {
let values = data.split('\n')
.flat_map(|s| s.split(": ")).collect::<Vec<&str>>();
let hp = values[1].parse::<u32>().unwrap();
let dmg = values[3].parse::<u32>().unwrap();
let effect = Effect{ name: "Boss Attack".to_string(), cost: 0,
dmg: dmg, def: 0, healing: 0, regen: 0,
turns: 1, selfcast: false};
(Character::new("Boss".to_string(), hp, 42, None), effect)
}
fn create_player() -> Character {
Character::new("Player".to_string(), 50, 500, None)
}
// This function simply imports the data from the input file
fn import_data() -> String {
let mut file = match File::open("../../inputs/22.txt") {
Ok(f) => f,
Err(e) => panic!("file error: {}", e),
};
let mut data = String::new();
match file.read_to_string(&mut data){
Ok(_) => {},
Err(e) => panic!("file error: {}", e),
};
data.pop();
data
}