| 7 | |
| 8 | @dataclass |
| 9 | class Unit: |
| 10 | units: int |
| 11 | hp: int |
| 12 | dmg: int |
| 13 | dmgtype: str |
| 14 | initiative: int |
| 15 | weaknesses: set[str] |
| 16 | immunities: set[str] |
| 17 | |
| 18 | @staticmethod |
| 19 | def parse(s: str) -> 'Unit': |
| 20 | units, hp, resistances, dmg, dmgtype, initiative = re.findall(PARSE_REGEX, s)[0] |
| 21 | weaknesses, immunities = set(), set() |
| 22 | if resistances: |
| 23 | for s in resistances[2:-1].split(';'): |
| 24 | t, _, *types = [w.rstrip(',') for w in s.strip().split(' ')] |
| 25 | if t == "weak": |
| 26 | weaknesses = set(types) |
| 27 | if t == "immune": |
| 28 | immunities = set(types) |
| 29 | return Unit(int(units), int(hp), int(dmg), dmgtype, int(initiative), weaknesses, immunities) |
| 30 | |
| 31 | def effective_power(self) -> int: |
| 32 | return self.units * self.dmg |
| 33 | |
| 34 | def dmg_against(self, u: 'Unit') -> int: |
| 35 | if self.dmgtype in u.immunities: |
| 36 | return 0 |
| 37 | if self.dmgtype in u.weaknesses: |
| 38 | return self.dmg * 2 * self.units |
| 39 | return self.dmg * self.units |
| 40 | |
| 41 | def choose_targets(attackers: list[Unit], defenders: list[Unit]) -> dict[int,int]: |
| 42 | targets = dict[int,int]() |