-
Notifications
You must be signed in to change notification settings - Fork 1
/
day22.rb
109 lines (90 loc) · 2.24 KB
/
day22.rb
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
require 'pry'
queue = [];
queue << {
player: { hp: 10, armor: 0, mana: 250, effects: {} },
boss: { hp: 13, effects: {} },
moves: []
}
turn = :player
wins = []
def cloneState(state)
Marshal.load(Marshal.dump(state))
end
def add_effect(state, target, spell, duration)
return state if state[target][:effects][spell]
state[target][:effects][spell] = duration
state
end
# BFS
while (state = queue.shift())
move = state[:moves].last
if move
# Effects
[:player, :boss].each do |target|
state[target][:effects].keys.each do |effect|
state[target][:effects][effect] -= 1
case effect
when :recharge
state[target][:mana] += 101
when :poison
state[target][:hp] -= 3
when :shield
state[target][:armor] = 7
end
if state[target][:effects][effect] < 0
state[target][:effects].delete(effect)
state[target][:armor] = 0 if effect == :shield
end
end
end
# Actions
actor = move[:actor]
target = (actor == :player ? :boss : :player)
case move[:action]
when :attack
state[target][:hp] -= (8 - state[target][:armor])
when :magic_missile
state[actor][:mana] -= 53
state[target][:hp] -= 4
when :drain
state[actor][:mana] -= 73
state[actor][:hp] += 2
state[target][:hp] -= 2
when :shield
state[actor][:mana] -= 113
add_effect(state, actor, :shield, 6)
when :poison
state[actor][:mana] -= 173
add_effect(state, target, :poison, 6)
when :recharge
state[actor][:mana] -= 229
add_effect(state, actor, :recharge, 5)
end
end
if state[:boss][:hp] <= 0
binding.pry
wins << state
elsif state[:player][:hp] <= 0
# Dead
next
elsif state[:player][:mana] <= 0
# Dead
next
end
# next turn
nextMoves = []
turn = (turn == :player ? :boss : :player)
if turn == :player
[:magic_missile, :drain, :shield, :poison, :recharge].each do |spell|
nextMoves << { actor: :player, action: spell }
end
else
nextMoves << { actor: :boss, action: :attack }
end
nextMoves.each do |move|
nextState = cloneState(state)
nextState[:moves] << move
queue << nextState
end
end
puts wins