# Fun fact
Tetris was the first video game played in outer space

In 1993, Russian cosmonaut Aleksandr A. Serebrov spent 196 days on the Mir space station with a very special distraction: a gray Game Boy loaded with Tetris. During that time the game orbited the Earth 3,000 times and became the first video game played in space. The Game Boy was sold in a Bonhams auction for $1,220 during the Space History Sale in 2011.

# Task
Parse the game log and determine how many lines have been cleared through the game. The game ends if all commands from input were interpreted or the maximum field height (30 units) is reached.

A horizontal line, according to the rules of classic Tetris, is considered cleared if it represents a solid line without gaps formed by falling blocks.
When such a line is formed, it disappears and any blocks above it fall down to fill the space.

# Input
```python
['4L2', '3R4', '4L3', '3L4', '4R0', '1L2'] # example
```
As an argument, you are given gamelog - an array of commands which you need to interpret.

Each command has the same form:
* The first character - the type of block (integer from 1 to 4, as in this kata we have only 4 types of blocks). Block types are described below.
* The second - the direction of movement (`"R"` or `"L"` - right or left).
* The third is an offset (integer from 0 to 4, as width of our field 9 units and new block always appears at the center of the field) relative to the starting position. Thus, `L4` means the leftmost position, and `R4` the rightmost, and `L0` is equivalent to `R0`.

# Output
The total number of cleaned horizontal lines (`int`) to the end of the game. Note, if the field height is exceeded, then the game ends immediately.

# Blocks
In this kata we have only 4 types of blocks. Yes, this is not a classic set of shapes, but this is only for simplicity.
```
# and their graphical representation:
             ■
         ■   ■
     ■   ■   ■
 ■   ■   ■   ■
---+---+---+---
#1  #2  #3  #4
```
# Field
Gamefield (a rectangular vertical shaft) has width 9 units and height 30 units.


table, th, td {
  border: 1px solid;
}


Indices can be represented as:


L4
L3
L2
L1
L0/R0
R1
R2
R3
R4



# Example 1
```python
>>> gamelog = ['1R4', '2L3', '3L2', '4L1', '1L0', '2R1', '3R2', '4R3', '1L4']
>>> tetris(gamelog)
1
```
Gamefield before last command (_ means empty space):
```
___■___■_
__■■__■■_
_■■■_■■■_
_■■■■■■■■
```
Gamefield after all commands:
```
___■___■_
__■■__■■_
_■■■_■■■_
```
As you can see, one solid line was cleared. So, answer is 1.

# Example 2
```python
>>> gamelog = ['1L2', '4R2', '3L3', '3L1', '1L4', '1R4']
>>> tetris(gamelog)
0
```
Gamefield after all commands:
```
 _____■__
_■_■__■__
_■_■__■__
■■■■__■_■
```
As you can see, there is no solid lines, so nothing to clear. Our answer is 0, zero cleaned lines.

# Note

Since there is no rotation of blocks in our model and all blocks are very simple, do not overthink the task.

# Other  

If you like the idea: leave feedback, and there will be more katas in the Tetris series.

* 7 kyuTetris Series #1 — Scoring System
* 6 kyuTetris Series #2 — Primitive Gameplay
* 6 kyuTetris Series #3 — Adding Rotation (TBA)
* 5 kyuTetris Series #4 — New Block Types (TBA)
* 4 kyuTetris Series #5 — Complex Block Types (TBA?)

Difficulty: EASY
Starter code: def tetris(arr):
	

Solutions:
pos = {'L4': 0, 'L3': 1, 'L2': 2, 'L1': 3, 'L0': 4, 'R0': 4, 'R1': 5, 'R2': 6, 'R3': 7, 'R4': 8}

def tetris(arr):
	(current, res) = ([0] * 9, 0)
	for x in arr:
		p = pos[x[1:]]
		current[p] += int(x[0])
		if current[p] >= 30:
			break
		y = min(current)
		if y:
			(current, res) = ([v - y for v in current], res + y)
	return res


class Game:

	def __init__(self, arr):
		self.comands = arr
		self.score = 0
		self.step = None
		self.fild = [-1] * 9
		self.over = lambda x: max(x) >= 29

	def __break__(self):
		while -1 not in self.fild:
			self.score += 1
			self.fild = [e - 1 for e in self.fild]
		return self.over(self.fild)

	def __values__(self, comand):
		self.step = 4 + {'R': lambda m: +int(m), 'L': lambda m: -int(m)}[comand[1]](comand[2])
		return int(comand[0])

	def game(self):
		for comand in self.comands:
			block = self.__values__(comand)
			self.fild[self.step] += block
			if self.__break__():
				break
		return self.score

def tetris(arr) -> int:
	play = Game(arr)
	return play.game()


def tetris(arr) -> int:
	(bd, lines) = ([0] * 9, 0)
	for (n, d, m) in arr:
		i = int(m) * (-1) ** (d == 'R')
		bd[i] += int(n)
		if bd[i] > 29:
			h = min(bd)
			if bd[i] - h > 29:
				break
			lines += h
			bd = [v - h for v in bd]
	lines += min(bd)
	return lines


from collections import Counter

def tetris(arr):
	fields = Counter()
	clear = 0
	for log in arr:
		pos = log[1:].replace('R0', 'L0')
		fields[pos] += int(log[0])
		if fields[pos] >= 30:
			break
		elif len(fields) == 9:
			c = min(fields.values())
			clear += c
			for pos in fields:
				fields[pos] -= c
	return clear


def tetris(arr) -> int:
	lines_cleared = 0
	sum = [0, 0, 0, 0, 0, 0, 0, 0, 0]
	dict = {'L4': 0, 'L3': 1, 'L2': 2, 'L1': 3, 'L0': 4, 'R0': 4, 'R1': 5, 'R2': 6, 'R3': 7, 'R4': 8}
	for i in arr:
		sum[dict[i[-2:]]] += int(i[0])
		lines = min(sum)
		sum = [i - lines for i in sum]
		lines_cleared += lines
		if max(sum) >= 30:
			break
	return lines_cleared


import numpy as np
col_dict = {'L4': 0, 'L3': 1, 'L2': 2, 'L1': 3, 'L0': 4, 'R0': 4, 'R1': 5, 'R2': 6, 'R3': 7, 'R4': 8}

def tetris(arr) -> int:
	field = np.zeros(9, dtype=np.int8)
	counter = 0
	for turn in arr:
		block_height = int(turn[0])
		index = col_dict[turn[1:]]
		field[index] += block_height
		if field[index] >= 30:
			return counter
		else:
			counter += field.min()
			field -= field.min()
	return counter


def tetris(lst):
	(field, lines) = ([0] * 9, 0)
	for (h, s, m) in lst:
		i = 4 + int(m) * (-1 if s == 'L' else 1)
		field[i] += int(h)
		while all((col > 0 for col in field)):
			(field, lines) = ([col - 1 for col in field], lines + 1)
		if field[i] > 29:
			break
	return lines


def tetris(a):
	(b, glob_min) = ([0] * 9, 29)
	for (x, y, z) in a:
		i = int(z) * (-1) ** (y == 'R')
		b[i] += int(x)
		if b[i] > glob_min:
			n = min(b)
			if b[i] - n > 29:
				return n
			glob_min = n + 29
	return min(b)


import numpy as np

def columnDecode(code):
	code = code.replace('R', '+')
	code = code.replace('L', '-')
	return int(code) + 4

def placePiece(colList, size, i):
	for x in range(i, size + i):
		colList[x] = 1
	return colList

def lineCheck(board):
	i = 0
	for i in range(5):
		if not all(board[:, i]):
			break
	return i

def tetris(arr):
	totalLines = 0
	board = np.zeros((9, 29), dtype='int')
	for piece in arr:
		col = columnDecode(piece[1:])
		size = int(piece[0])
		for i in range(len(board[col]) - 1, -1, -1):
			if board[col][i - 1] == 1 or i <= 0:
				try:
					board[col] = placePiece(board[col], size, i)
				except:
					return totalLines
				lines = lineCheck(board)
				if lines > 0:
					board = board[:, lines:]
					board = np.hstack((board, np.zeros((board.shape[0], lines), dtype='int')))
					totalLines += lines
					lines = 0
				break
	return totalLines


def tetris(arr):
	(d, c) = ({}, 0)
	for (i, j) in enumerate(arr):
		(t, dt) = (j[1:].replace('R0', 'L0'), d.values())
		d[t] = d.get(t, 0) + int(j[0])
		if len(dt) == 9 and all(dt):
			m = min(d.values())
			d = {k: l - m for (k, l) in d.items()}
			c += m
		if d[t] >= 30:
			break
	return c