# Your task
Oh no... more lemmings!! And in Lemmings Planet a huge battle
is being fought between the two great rival races: the green
lemmings and the blue lemmings. Everybody was now assigned
to battle and they will fight until one of the races completely
dissapears: the Deadly War has begun!

Every single lemming has a power measure that describes its
ability to fight. When two single lemmings fight with each one,
the lemming with more power survives and the other one dies.
However, the power of the living lemming gets smaller after the
fight, exactly in the value of the power of the lemming that died.
For example, if we have a green lemming with power ``50`` and a
blue lemming with power ``40``, the blue one dies and the green one
survives, but having only power 10 after the battle ``(50-40=10)``.
If two lemmings have the same power when they fight, both of
them die.

In the fight between the two races, there are a certain number of battlefields. Each race assigns one
lemming for each battlefield, starting with the most powerful. So for example, if a race has 5 lemmings
with power ```{50, 50, 40, 40, 30}``` and we have `3` battlefields, then a lemming with power `50` will be assigned
to battlefield 1, another with `50` power will be assigned to battlefield 2 and last a lemming with power `40` will go to battlefield 3. The other race will do the same.

The Deadly War is processed by having each race send its best soldiers as described to the battle-
fields, making a battle round. Then, all battles process at the same time, and some of the lemmings
will emerge victorious (but with less power) and some of them will die. The surviving ones will return to their race’s army and then a new round will begin, with each race sending again its best remaining soldiers to the battlefields. If at some point a race does not have enough soldiers to fill all battlefields, then only the ones with soldiers from both races will have a fight.

The Deadly War ends when one of the races has no more lemmings or when both of them disappear
at the same time. For example, imagine a war with 2 battlefields and a green army with powers `{20,10}` and a blue army with powers `{10, 10, 15}`. The first round will have `20 vs 15` in battlefield 1 and `10 vs 10` in battlefield 2. After these battles, green race will still have a power `5` lemming (that won on battlefield 1) and blue race will have one with power 10 (that did not fight). The ones in battlefield 2
died, since they both had the same power. Afer that comes a second round, and only battlefield 1 will have a fight, being `5 vs 10`. The blue lemming wins, killing the last green soldier and giving the victory to the blue race!

But in the real battle, will victory be green or blue?

Given the number of battefields and the armies of both races, your task is to discover which race
will win the Deadly War and show the power of the surviving soldiers.

## Input

You are given B, SG and SB, representing respectively the number of battlefields available, a vector of integers size `n` of lemmings in the green army 
and a vector of integers size `n` of lemmings in the blue army  (1 ≤ B, SG, SB ≤ 100000).

The lemmings in each army do not need to come in any particular order.

## Output
For each test case you should return :

• "Green and Blue died" if both races died in the same round

• "Green wins : Each surviving soldier in descending order" if the green army won the Deadly War

• "Blue wins : Each surviving soldier in descending order" if the blue army won the Deadly War

## Example

```python
lemming_battle(5, [10], [10]) == "Green and Blue died"
lemming_battle(2, [20,10], [10,10,15]) == "Blue wins: 5"
lemming_battle(3, [50,40,30,40,50], [50,30,30,20,60]) == "Green wins: 10 10"
```

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Game theory

Starter code: def lemming_battle(battlefield, green, blue):
	

Solutions:
from heapq import *

def lemming_battle(battlefield, green, blue):
	(hg, hb) = ([-v for v in lst] for lst in (green, blue))
	heapify(hg)
	heapify(hb)
	while hb and hg:
		(tmp_b, tmp_g) = ([], [])
		for _ in range(min(battlefield, len(hg), len(hb))):
			cmp = heappop(hg) - heappop(hb)
			if cmp < 0:
				tmp_g.append(cmp)
			elif cmp > 0:
				tmp_b.append(-cmp)
		for lem in tmp_b:
			heappush(hb, lem)
		for lem in tmp_g:
			heappush(hg, lem)
	(winner, lst) = ('Green', hg) if hg else ('Blue', hb)
	survivors = ' '.join((str(-v) for v in sorted(lst)))
	return 'Green and Blue died' if not hg and (not hb) else f'{winner} wins: {survivors}'


def lemming_battle(battlefield, green, blue):
	battlefield = min(battlefield, len(green), len(blue))
	while sum(green) and sum(blue):
		(green, blue) = (sorted(green, reverse=True), sorted(blue, reverse=True))
		for i in range(battlefield):
			(green[i], blue[i]) = (max(0, green[i] - blue[i]), max(0, blue[i] - green[i]))
	if not sum(green) + sum(blue):
		return 'Green and Blue died'
	winner = 'green' if sum(green) else 'blue'
	survivors = ' '.join((str(lemming) for lemming in sorted(locals()[winner], reverse=True) if lemming))
	return f'{winner.title()} wins: {survivors}'


def lemming_battle(battlefield, green, blue):
	while True:
		(green, blue) = (sorted(green, reverse=True), sorted(blue, reverse=True))
		if not (green or blue):
			return 'Green and Blue died'
		if not green:
			return 'Blue wins: ' + ' '.join(map(str, blue))
		if not blue:
			return 'Green wins: ' + ' '.join(map(str, green))
		left = min(len(green), len(blue), battlefield)
		fight = list(map(int.__sub__, green[:left], blue[:left]))
		green = green[left:] + [x for x in fight if x > 0]
		blue = blue[left:] + [-x for x in fight if x < 0]


def fight(green, blue):
	xs = [(max(g - b, 0), max(b - g, 0)) for (g, b) in zip(green, blue)]
	return ([x for x in ys if x] for ys in zip(*xs))

def lemming_battle(battlefield, green, blue):
	while green and blue:
		green = sorted(green, reverse=True)
		blue = sorted(blue, reverse=True)
		n = min(battlefield, len(green), len(blue))
		(green[:n], blue[:n]) = fight(green[:n], blue[:n])
	soldiers = green or blue
	if soldiers:
		return '{} wins: {}'.format('Green' if green else 'Blue', ' '.join(map(str, sorted(soldiers, reverse=True))))
	return 'Green and Blue died'


def text(green, blue):
	if green:
		return 'Green wins: {0}'.format(' '.join((str(x) for x in green)))
	elif blue:
		return 'Blue wins: {0}'.format(' '.join((str(x) for x in blue)))
	return 'Green and Blue died'

def lemming_battle(battlefield, green, blue):
	(green, blue) = (sorted(green, reverse=True), sorted(blue, reverse=True))
	if not green or not blue:
		return text(green, blue)
	for i in range(battlefield):
		try:
			res = green[i] - blue[i]
			if res == 0:
				blue[i] = False
				green[i] = False
			elif res > 0:
				green[i] = res
				blue[i] = False
			else:
				blue[i] = abs(res)
				green[i] = False
		except IndexError:
			break
	return lemming_battle(battlefield, [x for x in green if x], [x for x in blue if x])


def lemming_battle(N, G, B):
	while G and B:
		G.sort()
		B.sort()
		current = []
		for i in range(min(N, len(G), len(B))):
			current.append(G.pop() - B.pop())
		while current:
			x = current.pop()
			if x:
				(G if x > 0 else B).append(abs(x))
	if not (G or B):
		return 'Green and Blue died'
	return '{} wins: {}'.format('Green' if G else 'Blue', ' '.join((str(x) for x in sorted(G + B, reverse=True))))


def lemming_battle(k, a, b):
	a.sort(reverse=True)
	b.sort(reverse=True)
	while a and b:
		for (i, j) in zip(a[:k], b[:k]):
			temp = max(i, j)
			a.remove(i)
			b.remove(j)
			if i == j:
				continue
			if temp == i:
				a.append(i - j)
			elif temp == j:
				b.append(j - i)
		a.sort(reverse=True)
		b.sort(reverse=True)
	return [[f"Green wins: {' '.join(map(str, a))}", f"Blue wins: {' '.join(map(str, b))}"][len(b) > 0], 'Green and Blue died'][len(a) == len(b)]


import sys
sys.setrecursionlimit(15000)

def lemming_battle(battlefield, green, blue):
	(green, blue) = [p[::-1] for p in map(sorted, (green, blue))]
	k = min(battlefield, len(min(green, blue, key=len)))
	(g, b) = (green[:k], blue[:k])
	(a_green, a_blue) = ([], [])
	for (a, b) in zip(g, b):
		if abs(a) - abs(b):
			if a > b:
				a_green.append(a - b)
			else:
				a_blue.append(b - a)
	(green, blue) = (sorted(a_green + green[k:], reverse=True), sorted(a_blue + blue[k:], reverse=True))
	if not green + blue:
		return 'Green and Blue died'
	if not blue:
		return f"Green wins: {' '.join(map(str, sorted(green, reverse=1)))}"
	if not green:
		return f"Blue wins: {' '.join(map(str, sorted(blue, reverse=1)))}"
	else:
		return lemming_battle(battlefield, green, blue)


def fight(fields, green, blue):
	(green, blue) = (sorted(green, reverse=True), sorted(blue, reverse=True))
	for i in range(min(len(green), len(blue), fields)):
		(soldier_g, soldier_b) = (green[i], blue[i])
		if soldier_g > soldier_b:
			(green[i], blue[i]) = (soldier_g - soldier_b, 0)
		elif soldier_g < soldier_b:
			(green[i], blue[i]) = (0, soldier_b - soldier_g)
		else:
			(green[i], blue[i]) = (0, 0)
	(green, blue) = ([s for s in green if s > 0], [s for s in blue if s > 0])
	return (green, blue)

def lemming_battle(battlefields, green, blue):
	while green != [] and blue != []:
		(green, blue) = fight(battlefields, green, blue)
	(green, blue) = (sorted(green, reverse=True), sorted(blue, reverse=True))
	if green or blue:
		return ('Green' if green else 'Blue') + ' wins: ' + ' '.join((str(s) for s in green or blue))
	return 'Green and Blue died'


import heapq

def lemming_battle(n, g, b):
	g = [i * -1 for i in g]
	b = [i * -1 for i in b]
	heapq.heapify(g)
	heapq.heapify(b)
	while g and b:
		(t1, t2) = ([], [])
		for _ in range(n):
			if len(g) == 0 or len(b) == 0:
				break
			x = heapq.heappop(g) * -1
			y = heapq.heappop(b) * -1
			if x > y:
				t1.append(x - y)
			elif x < y:
				t2.append(y - x)
		for i in t1:
			heapq.heappush(g, -1 * i)
		for i in t2:
			heapq.heappush(b, -1 * i)
	if len(g) == 0 and len(b) == 0:
		return 'Green and Blue died'
	if len(g) == 0:
		x = ' '.join(map(str, map(lambda x: x * -1, sorted(b))))
		return 'Blue wins: {}'.format(x)
	else:
		x = ' '.join(map(str, map(lambda x: x * -1, sorted(g))))
		return 'Green wins: {}'.format(x)