Skip to content

Instantly share code, notes, and snippets.

@jalasem
Created October 7, 2019 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalasem/1b70d50d5b6529d6c1d33375b8ae491a to your computer and use it in GitHub Desktop.
Save jalasem/1b70d50d5b6529d6c1d33375b8ae491a to your computer and use it in GitHub Desktop.
Hacking Winterfell Problem
/**
* Requirement
Having had a restless nights sleep, you’ ve woken up in a daze and have found yourself in Westeros.Confused, the sun is just beginning to rise and yet in the distance, you can hear crowds jeering, roaring battle cries and a stampede of horses charging into what seems like a long - awaited battle.
Suddenly, you realise what’ s happening.The time has
finally come.The Seven Kingdoms are at war and with one common goal;
to defeat the White Walkers and the dreaded Night King who leads them.
At this point, sides do not matter and you know what you need to do.Your mission ? To calculate the chances of the Seven Kingdoms defeating the White Walkers, sending them back beyond the wall and to sleep beneath the ice once more.
You’ ll need to base your calculation on the following facts:
Seven Kingdom Army:
-{
no_of_dragons
}
Dragons,
each one has 600 damage and 600 defence
-
5000 Infantry, each one has 2 damage and 2 defence
White Walker Army:
-{
no_of_white_lords
}
White Lords, each one has 50 damage and 100 defence
-
10000 Walkers Infantry, each one has 1 damage and 3 defence
The attacker cannot have passive units(units which are not attacking in one turn)
One unit cannot remain with unconsumed damage as long as there are still enemies remaining
Each attacking unit can strike multiple defenders in the same turn until its damage is consumed
When an attacking unit is striking, its damage is reduced in the current turn with the defense value of the defender
The turn ends when the cumulated damage of the attacker reaches 0
After an attack, the defenders which are not killed(defence > 0) will replenish their defence to 100 %
Find the fastest way to victory, no matter which team wins
Example: A Dragon(600 damage) attacks a White Lord(defence 100)
The Dragon has 500 damage remaining and can strike 5 more White Lords in the same turn
The White Lord dies
How long will the battle last(how many turns) and who will emerge victorious ?
INPUT
string first_strike_army_name
int no_of_dragons
int no_of_white_lords
OUTPUT
string result(formatted as army_name | turns)
EXAMPLE 1
Input: "Seven Kingdom Army", 4, 5
Output: White Walker Army | 6
EXAMPLE 2
Input: "Seven Kingdom Army", -1, 5
Output: Invalid parameter provided
*/
function Batalion(no_of_army, variant) {
this.army = [no_of_army * 600, no_of_army * 600]
this.infantry = variant === 'seven' ? [5000 * 2, 5000 * 2] : [10000 * 1, 10000 * 3]
this.strength = [
this.army[0] + this.infantry[0],
this.army[1] + this.infantry[1]
]
return {
army: this.army,
infantry: this.infantry,
attack(defence) {
this.strength[0] -= defence
// still working....
}
}
}
function sevenStrength(no_of_dragons) {
const army = [no_of_dragons * 600, no_of_dragons * 600];
const infantry = [5000 * 2, 5000 * 2];
return {
army,
infantry,
strength: [
army[0] + infantry[0],
army[1] + infantry[1]
]
}
}
function whiteStrength (no_of_white_lords) {
const army = [no_of_white_lords * 50, no_of_white_lords * 100];
const infantry = [10000 * 1, 10000 * 3];
return {
army,
infantry,
strength: [
army[0] + infantry[0],
army[1] + infantry[1]
]
}
}
function run(first_strike_army_name, no_of_dragons, no_of_white_lords) {
/*
* Some work here; return type and arguments should be according to the problem's requirements
*/
let seven = sevenStrength(no_of_dragons)
let white = whiteStrength(no_of_white_lords)
// console.log({ seven, white })
let result = null;
const army1 = 'Seven Kingdom Army'
const army2 = 'White Walker Army'
let turns = 0
let winner = ''
if (first_strike_army_name === army1) {
// calc
let strong = true
while (strong) {
// army strike
white.army[1] -= seven.army[0]
if (white.army[1] > 0) { }
strong = false
}
} else {
// calc
}
console.log({
seven: seven.strength, white: white.strength,
turns, winner
})
return result;
}
run('Seven Kingdom Army', 4, 1) // White Walker Army | 6
// run('Seven Kingdom Army', 10, 5) // Seven Kingdom Army | 5
// run('Seven Kingdom Army', 16, 18) // Seven Kingdom Army | 3
// run('Seven Kingdom Army', 2, 6) // Seven Kingdom Army | 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment