added rest of the code
This commit is contained in:
110
22/part1.py
Normal file
110
22/part1.py
Normal file
@ -0,0 +1,110 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import re
|
||||
from copy import deepcopy
|
||||
from sys import maxsize
|
||||
|
||||
RE_INPUT = re.compile(r'^(.*):\s+(\d+)$')
|
||||
|
||||
spells = [
|
||||
{'name': 'Magic Missile', 'costs': 53, 'damage': 4, 'turns': 0},
|
||||
{'name': 'Drain', 'costs': 73, 'damage': 2, 'heal': 2, 'turns': 0},
|
||||
{'name': 'Shield', 'costs': 113, 'armor': 7, 'turns': 6},
|
||||
{'name': 'Poison', 'costs': 173, 'damage': 3, 'turns': 6},
|
||||
{'name': 'Recharge', 'costs': 229, 'mana': 101, 'turns': 5}
|
||||
]
|
||||
minManaUsed = maxsize
|
||||
|
||||
def read_file(filename):
|
||||
file = open(filename, 'r')
|
||||
while True:
|
||||
line = file.readline()
|
||||
if not line:
|
||||
break
|
||||
yield line
|
||||
|
||||
def printSpells(s):
|
||||
name = []
|
||||
for (s1,t1) in s:
|
||||
name.append("%s (%d)" % (spells[s1]['name'], t1))
|
||||
print ", ".join(name)
|
||||
|
||||
def sim(me, boss, activeSpells, meTurn, manaUsed):
|
||||
global minManaUsed
|
||||
|
||||
newSpells = []
|
||||
newBoss = deepcopy(boss)
|
||||
newMe = deepcopy(me)
|
||||
|
||||
newMe['Armor'] = 0
|
||||
|
||||
for (s, t) in activeSpells:
|
||||
if t >= 0:
|
||||
newBoss['Hit Points'] -= spells[s]['damage']
|
||||
newMe['Hit Points'] += spells[s]['heal']
|
||||
newMe['Armor'] += spells[s]['armor']
|
||||
newMe['Mana'] += spells[s]['mana']
|
||||
t -= 1
|
||||
if t > 0:
|
||||
newSpells.append((s, t))
|
||||
else:
|
||||
newSpells.append((s, -1))
|
||||
|
||||
if newBoss['Hit Points'] <= 0:
|
||||
if manaUsed < minManaUsed:
|
||||
minManaUsed = manaUsed
|
||||
print manaUsed
|
||||
printSpells(activeSpells)
|
||||
return True
|
||||
|
||||
if manaUsed > minManaUsed:
|
||||
return False
|
||||
|
||||
if meTurn:
|
||||
for s1 in range(len(spells)):
|
||||
spell = spells[s1]
|
||||
isSpellUsed = False
|
||||
for (s2, t2) in newSpells:
|
||||
if s1 == s2 and t2 >= 0:
|
||||
isSpellUsed = True
|
||||
break
|
||||
if spell['costs'] <= newMe['Mana'] and not isSpellUsed:
|
||||
a = deepcopy(newSpells)
|
||||
a.append((s1, spell['turns']))
|
||||
m = deepcopy(newMe)
|
||||
m['Mana'] -= spell['costs']
|
||||
sim(m, newBoss, a, False, manaUsed+spell['costs'])
|
||||
else:
|
||||
if newMe['Armor'] >= boss['Damage']:
|
||||
newMe['Hit Points'] -= 1
|
||||
else:
|
||||
newMe['Hit Points'] += newMe['Armor'] - newBoss['Damage']
|
||||
if newMe['Hit Points'] > 0:
|
||||
sim(newMe, newBoss, newSpells, True, manaUsed)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
|
||||
for i in range(len(spells)):
|
||||
for k in ['costs', 'damage', 'armor', 'heal', 'mana', 'turns']:
|
||||
if k not in spells[i].keys():
|
||||
spells[i][k] = 0
|
||||
|
||||
boss = dict()
|
||||
me = {
|
||||
# 'Hit Points': 10,
|
||||
# 'Mana': 250
|
||||
'Hit Points': 50,
|
||||
'Mana': 500
|
||||
}
|
||||
|
||||
for line in read_file('input'):
|
||||
match = re.match(RE_INPUT, line)
|
||||
boss[match.group(1)] = int(match.group(2))
|
||||
|
||||
sim(me, boss, [], True, 0)
|
||||
print minManaUsed
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
66
22/part1_x.py
Normal file
66
22/part1_x.py
Normal file
@ -0,0 +1,66 @@
|
||||
from copy import deepcopy
|
||||
from sys import maxsize
|
||||
# 0=manacost, 1=dmg, 2=hp, 3=armour, 4=mana, 5=turns, 6=index
|
||||
missile = (53,4,0,0,0,0,0)
|
||||
drain = (73,2,2,0,0,0,1)
|
||||
shield = (113,0,0,7,0,6,2)
|
||||
poison = (173,3,0,0,0,6,3)
|
||||
recharge = (229,0,0,0,101,5,4)
|
||||
spells = [missile, drain, shield, poison, recharge]
|
||||
leastManaUsed = maxsize
|
||||
partTwo = False
|
||||
|
||||
def main():
|
||||
sim(55,50,500,[],True,0)
|
||||
print leastManaUsed
|
||||
|
||||
def sim(bossHP, myHP, myMana, activespells, playerTurn, manaUsed):
|
||||
bossDmg = 8
|
||||
myArmour = 0
|
||||
|
||||
if partTwo and playerTurn:
|
||||
myHP -= 1
|
||||
if myHP <= 0:
|
||||
return False
|
||||
|
||||
newActiveSpells = []
|
||||
for activespell in activespells:
|
||||
if activespell[5] >= 0: # spell effect applies now
|
||||
bossHP -= activespell[1]
|
||||
myHP += activespell[2]
|
||||
myArmour += activespell[3]
|
||||
myMana += activespell[4]
|
||||
|
||||
newActiveSpell = (activespell[0], activespell[1], activespell[2], activespell[3], activespell[4], activespell[5]-1, activespell[6])
|
||||
if newActiveSpell[5] > 0: # spell carries over
|
||||
newActiveSpells.append(newActiveSpell)
|
||||
|
||||
if bossHP <= 0:
|
||||
global leastManaUsed
|
||||
if manaUsed < leastManaUsed:
|
||||
leastManaUsed = manaUsed
|
||||
return True
|
||||
|
||||
if manaUsed >= leastManaUsed:
|
||||
return False
|
||||
|
||||
if (playerTurn):
|
||||
for i in range(len(spells)):
|
||||
spell = spells[i]
|
||||
spellAlreadyActive = False
|
||||
for j in range(len(newActiveSpells)):
|
||||
if newActiveSpells[j][6] == spell[6]:
|
||||
spellAlreadyActive = True
|
||||
break
|
||||
|
||||
spellManaCost = spell[0]
|
||||
if spellManaCost <= myMana and not spellAlreadyActive:
|
||||
a = deepcopy(newActiveSpells)
|
||||
a.append(spell)
|
||||
sim(bossHP, myHP, myMana - spellManaCost, a, False, manaUsed+spellManaCost)
|
||||
else:
|
||||
myHP += myArmour-bossDmg if myArmour-bossDmg < 0 else -1
|
||||
if myHP > 0:
|
||||
sim(bossHP,myHP,myMana,newActiveSpells, True,manaUsed)
|
||||
|
||||
main()
|
||||
113
22/part2.py
Normal file
113
22/part2.py
Normal file
@ -0,0 +1,113 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import re
|
||||
from copy import deepcopy
|
||||
from sys import maxsize
|
||||
|
||||
RE_INPUT = re.compile(r'^(.*):\s+(\d+)$')
|
||||
|
||||
spells = [
|
||||
{'name': 'Magic Missile', 'costs': 53, 'damage': 4, 'turns': 0},
|
||||
{'name': 'Drain', 'costs': 73, 'damage': 2, 'heal': 2, 'turns': 0},
|
||||
{'name': 'Shield', 'costs': 113, 'armor': 7, 'turns': 6},
|
||||
{'name': 'Poison', 'costs': 173, 'damage': 3, 'turns': 6},
|
||||
{'name': 'Recharge', 'costs': 229, 'mana': 101, 'turns': 5}
|
||||
]
|
||||
minManaUsed = maxsize
|
||||
|
||||
def read_file(filename):
|
||||
file = open(filename, 'r')
|
||||
while True:
|
||||
line = file.readline()
|
||||
if not line:
|
||||
break
|
||||
yield line
|
||||
|
||||
def printSpells(s):
|
||||
name = []
|
||||
for (s1,t1) in s:
|
||||
name.append("%s (%d)" % (spells[s1]['name'], t1))
|
||||
print ", ".join(name)
|
||||
|
||||
def sim(me, boss, activeSpells, meTurn, manaUsed):
|
||||
global minManaUsed
|
||||
|
||||
newSpells = []
|
||||
newBoss = deepcopy(boss)
|
||||
newMe = deepcopy(me)
|
||||
|
||||
newMe['Armor'] = 0
|
||||
|
||||
newMe['Hit Points'] -= 1
|
||||
if newMe['Hit Points'] <= 0:
|
||||
return False
|
||||
|
||||
for (s, t) in activeSpells:
|
||||
if t >= 0:
|
||||
newBoss['Hit Points'] -= spells[s]['damage']
|
||||
newMe['Hit Points'] += spells[s]['heal']
|
||||
newMe['Armor'] += spells[s]['armor']
|
||||
newMe['Mana'] += spells[s]['mana']
|
||||
t -= 1
|
||||
if t > 0:
|
||||
newSpells.append((s, t))
|
||||
else:
|
||||
newSpells.append((s, -1))
|
||||
|
||||
if newBoss['Hit Points'] <= 0:
|
||||
if manaUsed < minManaUsed:
|
||||
minManaUsed = manaUsed
|
||||
print manaUsed
|
||||
printSpells(activeSpells)
|
||||
return True
|
||||
|
||||
if manaUsed > minManaUsed:
|
||||
return False
|
||||
|
||||
if meTurn:
|
||||
for s1 in range(len(spells)):
|
||||
spell = spells[s1]
|
||||
isSpellUsed = False
|
||||
for (s2, t2) in newSpells:
|
||||
if s1 == s2 and t2 >= 0:
|
||||
isSpellUsed = True
|
||||
break
|
||||
if spell['costs'] <= newMe['Mana'] and not isSpellUsed:
|
||||
s = deepcopy(newSpells)
|
||||
s.append((s1, spell['turns']))
|
||||
m = deepcopy(newMe)
|
||||
m['Mana'] -= spell['costs']
|
||||
sim(m, newBoss, s, False, manaUsed+spell['costs'])
|
||||
else:
|
||||
if newMe['Armor'] >= boss['Damage']:
|
||||
newMe['Hit Points'] -= 1
|
||||
else:
|
||||
newMe['Hit Points'] += newMe['Armor']
|
||||
newMe['Hit Points'] -= newBoss['Damage']
|
||||
if newMe['Hit Points'] > 0:
|
||||
sim(newMe, newBoss, newSpells, True, manaUsed)
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
for i in range(len(spells)):
|
||||
for k in ['costs', 'damage', 'armor', 'heal', 'mana', 'turns']:
|
||||
if k not in spells[i].keys():
|
||||
spells[i][k] = 0
|
||||
|
||||
boss = dict()
|
||||
me = {
|
||||
'Hit Points': 50,
|
||||
'Mana': 500
|
||||
}
|
||||
|
||||
for line in read_file('input'):
|
||||
match = re.match(RE_INPUT, line)
|
||||
boss[match.group(1)] = int(match.group(2))
|
||||
|
||||
sim(me, boss, [], True, 0)
|
||||
print minManaUsed
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
66
22/part2_x.py
Normal file
66
22/part2_x.py
Normal file
@ -0,0 +1,66 @@
|
||||
from copy import deepcopy
|
||||
from sys import maxsize
|
||||
# 0=manacost, 1=dmg, 2=hp, 3=armour, 4=mana, 5=turns, 6=index
|
||||
missile = (53,4,0,0,0,0,0)
|
||||
drain = (73,2,2,0,0,0,1)
|
||||
shield = (113,0,0,7,0,6,2)
|
||||
poison = (173,3,0,0,0,6,3)
|
||||
recharge = (229,0,0,0,101,5,4)
|
||||
spells = [missile, drain, shield, poison, recharge]
|
||||
leastManaUsed = maxsize
|
||||
partTwo = True
|
||||
|
||||
def main():
|
||||
sim(55,50,500,[],True,0)
|
||||
print leastManaUsed
|
||||
|
||||
def sim(bossHP, myHP, myMana, activespells, playerTurn, manaUsed):
|
||||
bossDmg = 8
|
||||
myArmour = 0
|
||||
|
||||
if partTwo and playerTurn:
|
||||
myHP -= 1
|
||||
if myHP <= 0:
|
||||
return False
|
||||
|
||||
newActiveSpells = []
|
||||
for activespell in activespells:
|
||||
if activespell[5] >= 0: # spell effect applies now
|
||||
bossHP -= activespell[1]
|
||||
myHP += activespell[2]
|
||||
myArmour += activespell[3]
|
||||
myMana += activespell[4]
|
||||
|
||||
newActiveSpell = (activespell[0], activespell[1], activespell[2], activespell[3], activespell[4], activespell[5]-1, activespell[6])
|
||||
if newActiveSpell[5] > 0: # spell carries over
|
||||
newActiveSpells.append(newActiveSpell)
|
||||
|
||||
if bossHP <= 0:
|
||||
global leastManaUsed
|
||||
if manaUsed < leastManaUsed:
|
||||
leastManaUsed = manaUsed
|
||||
return True
|
||||
|
||||
if manaUsed >= leastManaUsed:
|
||||
return False
|
||||
|
||||
if (playerTurn):
|
||||
for i in range(len(spells)):
|
||||
spell = spells[i]
|
||||
spellAlreadyActive = False
|
||||
for j in range(len(newActiveSpells)):
|
||||
if newActiveSpells[j][6] == spell[6]:
|
||||
spellAlreadyActive = True
|
||||
break
|
||||
|
||||
spellManaCost = spell[0]
|
||||
if spellManaCost <= myMana and not spellAlreadyActive:
|
||||
a = deepcopy(newActiveSpells)
|
||||
a.append(spell)
|
||||
sim(bossHP, myHP, myMana - spellManaCost, a, False, manaUsed+spellManaCost)
|
||||
else:
|
||||
myHP += myArmour-bossDmg if myArmour-bossDmg < 0 else -1
|
||||
if myHP > 0:
|
||||
sim(bossHP,myHP,myMana,newActiveSpells, True,manaUsed)
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user