--- id: 6a151d32d772271dd2448c2d title: "Challenge 311: Spellcaster" challengeType: 29 dashedName: challenge-311 --- # --description-- Given a string of spell codes you are casting, calculate the total score. Each character in the string represents a spell: | Code | Spell| Category | Base Score | | - | - | - | - | | `"f"` | Fire | Destruction | 3 | | `"l"` | Lightning | Destruction | 3 | | `"i"` | Ice | Control | 2 | | `"w"` | Wind | Control | 2 | | `"h"` | Heal | Restoration | 1 | | `"s"` | Shield | Restoration | 1 | A combo multiplier is applied based on how many spells in a row have been cast from different categories: - The first spell always scores at base value. - Each consecutive spell from a different category than the previous increases the multiplier by 1. - Casting a spell from the same category as the previous resets the multiplier back to 1. - The score for each spell is its base score multiplied by the current multiplier. Return the total score from the sequence of spells. # --hints-- `cast("fihwl")` should return `33`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(cast("fihwl"), 33)`) }}) ``` `cast("lwswfi")` should return `45`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(cast("lwswfi"), 45)`) }}) ``` `cast("wislhfl")` should return `37`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(cast("wislhfl"), 37)`) }}) ``` `cast("sihwlih")` should return `50`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(cast("sihwlih"), 50)`) }}) ``` `cast("wishlfihwslwifihl")` should return `101`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(cast("wishlfihwslwifihl"), 101)`) }}) ``` # --seed-- ## --seed-contents-- ```py def cast(spells): return spells ``` # --solutions-- ```py def cast(spells): lookup = { "f": ("destruction", 3), "l": ("destruction", 3), "i": ("control", 2), "w": ("control", 2), "h": ("restoration", 1), "s": ("restoration", 1) } total = 0 multiplier = 1 prev_category = None for spell in spells: category, score = lookup[spell] if prev_category and category != prev_category: multiplier += 1 else: multiplier = 1 total += score * multiplier prev_category = category return total ```