Skip to content

Instantly share code, notes, and snippets.

@karapetyan
Created April 11, 2018 14:20
Show Gist options
  • Select an option

  • Save karapetyan/6292536513addab3d2758d9b7907e033 to your computer and use it in GitHub Desktop.

Select an option

Save karapetyan/6292536513addab3d2758d9b7907e033 to your computer and use it in GitHub Desktop.
function canCast(aPool, ...aCasts) {
let pool = aPool.split('');
let casts = toFlatArray(aCasts);
casts = castLetters(pool, casts);
if (!(stillHaveLettersMana(casts))) {
let requiredCastPoints = countCastPoints(casts);
let availableCastPoints = countPoolPoints(pool);
return (availableCastPoints >= requiredCastPoints) ?
true :
false
} else {
return false;
}
}
function stillHaveLettersMana(casts) {
return casts.find(cast =>
isColoredMana(cast)
)
}
function toFlatArray(casts) {
let arr = casts.map(cast =>
cast.split('')
)
return [].concat.apply([], arr);
}
function castLetters(pool, casts) {
return casts.filter ( letter => {
if (isColoredMana(letter)) {
return (castPool(pool, letter)) ?
false :
true
} else {
return true
}
})
}
function castPool(pool, letter) {
if (pool.indexOf(letter) > -1) {
pool.splice(pool.indexOf(letter), 1)
return true
} else {
return false
}
}
function isColoredMana(letter) {
let coloredMana = ["B", "G", "R", "U", "W"]
return coloredMana.indexOf(letter) > -1 ?
true :
false
}
function countCastPoints(casts) {
return casts.reduce((first, second) =>
parseInt(first) + parseInt(second)
, 0)
}
function countPoolPoints(pool = [...pool]) {
let filteredMana = 0;
pool = pool.filter(item => {
if (isColoredMana(item)) {
filteredMana++;
return false
} else {
return true
}
})
let points = pool.reduce((first, second) =>
parseInt(first) + parseInt(second)
, filteredMana)
return points;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment