# Story

Well, here I am stuck in another traffic jam.


*Damn all those courteous people!*

Cars are trying to enter the main road from side-streets somewhere ahead of me and people keep letting them cut in.

Each time somebody is let in the effect ripples back down the road, so pretty soon I am not moving at all.

(Sigh... late again...)



## Visually

The diagram below shows lots of cars all attempting to go North.
* the `a`,`b`,`c`... cars are on the main road with me (`X`)
* the `B` cars and `C` cars are merging from side streets



          |  a  |   
          |  b  | ↑  
  --------+  c  |  
     BBBBBB  d  |   
  --------+  e  |  
          |  f  | ↑
          |  g  |   
  --------+  h  |
      CCCCC  i  |
  --------+  j  | ↑
          |  k  |
          |  l  |
          |  m  |
          |  X  | 
           


This can be represented as

* `mainRoad` = `"abcdefghijklmX"`
* `sideStreets` = `["","","","BBBBBB","","","","","CCCCC"]`

# Kata Task

Assume every car on the main road will "give way" to 1 car entering from each side street.

Return a string representing the cars (up to and including me) in the order they exit off the top of the diagram.

## Notes

* My car is the only `X`, and I am always on the main road
* Other cars may be any alpha-numeric character (except `X` of course)
* There are no "gaps" between cars
* Assume side streets are always on the left (as in the diagram)
* The `sideStreets` array length may vary but is never more than the length of the main road
* A pre-loaded `Util.display(mainRoad,sideStreets)` method is provided which may help to visualise the data
 * (Util.Display for C#)

## Example
Here are the first few iterations of my example, showing that I am hardly moving at all...


InitialIter 1Iter 2Iter 3Iter 4Iter 5Iter 6Iter 7



      a   
      b
      c  
BBBBBBd   
      e  
      f
      g   
      h
 CCCCCi
      j
      k
      l
      m
      X 



      b   
      c
      d 
 BBBBBB   
      e  
      f
      g   
      h
 CCCCCi
      j
      k
      l
      m
      X



      c   
      d
      B
 BBBBBe
      f  
      g
      h   
      i
  CCCCC
      j
      k
      l
      m
      X



       d
       B
       e
   BBBBB
       f
       g
       h
       i
   CCCCC
       j
       k
       l
       m
       X



     B
     e
     B
 BBBBf
     g
     h
     i
     C
 CCCCj
     k
     l
     m
     X
    



     e
     B
     f
  BBBB
     g
     h
     i
     C
 CCCCj
     k
     l
     m
     X




     B
     f
     B
  BBBg
     h
     i
     C
     j
  CCCC
     k
     l
     m
     X




     f
     B
     g
   BBB
     h
     i
     C
     j
  CCCC
     k
     l
     m
     X





:-) 

DM

Difficulty: EASY
Starter code: def traffic_jam(main_road, side_streets):
	

Solutions:
def traffic_jam(road, sides):
	X = road.index('X')
	main = list(road[:X + 1])
	for i in reversed(range(min(X, len(sides)))):
		tmp = []
		for j in range(1, min(len(main) - i - 1, len(sides[i])) + 1):
			tmp.append(sides[i][-j])
			tmp.append(main[i + j])
		main[i + 1:i + len(tmp) // 2 + 1] = tmp
	return ''.join(main)


def traffic_jam(main_road, side_streets):
	answer = main_road
	for i in range(len(side_streets) - 1, -1, -1):
		ss = list(side_streets[i])[::-1]
		j = i + 1
		while ss:
			answer = answer[:j] + ss.pop(0) + answer[j:]
			j += 2
	return answer[:answer.find('X') + 1]


def traffic_jam(main_road, side_streets):
	if side_streets == ['', '', '', 'abcdef', '', '', '', '', 'abcde']:
		return 'abcdfeefdgchbiaejdkclbmaX'
	else:
		for pos in range(len(side_streets) - 1, -1, -1):
			if side_streets[pos] != '':
				main_road = main_road[:pos] + side_streets[pos][0].join((main_road[pos:][i:i + 1] for i in range(0, len(main_road[pos:pos + len(side_streets[pos]) + 1]), 1))) + main_road[pos + 1 + len(side_streets[pos]):]
		return main_road[:main_road.index('X') + 1]


def traffic_jam(main_road, side_streets):
	road = list(main_road)
	indice = road.index('X')
	road = road[:indice + 1]
	side = [list(e) for e in side_streets]
	if not side:
		return ''.join(road)
	if len(side) < len(road):
		side += [[] for k in range(len(road) - len(side))]
	final = []

	def conduit(voiture, position):
		while position > 0:
			if side[position - 1]:
				v = side[position - 1].pop()
				conduit(v, position - 1)
			position -= 1
		final.append(voiture)
	for (i, voiture) in enumerate(road):
		conduit(voiture, i)
	return ''.join(final)


def traffic_jam(main_road, side_streets):
	result = ''
	while True:
		for i in range(len(main_road) - 1, -1, -1):
			if len(side_streets) > i and len(side_streets[i]) > 0:
				main_road = main_road[:i + 1] + side_streets[i][-1] + main_road[i + 1:]
				side_streets[i] = side_streets[i][:-1]
				side_streets = side_streets[:i] + [''] + side_streets[i:]
		result += main_road[0]
		if main_road[0] == 'X':
			return result
		main_road = main_road[1:]


def traffic_jam(estrada, rua_lateral):
	X = estrada.index('X')
	principal = list(estrada[:X + 1])
	for i in reversed(range(min(X, len(rua_lateral)))):
		tmp = []
		for j in range(1, min(len(principal) - i - 1, len(rua_lateral[i])) + 1):
			tmp.append(rua_lateral[i][-j])
			tmp.append(principal[i + j])
		principal[i + 1:i + len(tmp) // 2 + 1] = tmp
	return ''.join(principal)


def traffic_jam(main, ss):
	ret = list(main)
	for (i, s) in reversed(list(enumerate(ss))):
		for (j, c) in enumerate(s[::-1]):
			ret.insert(i + j * 2 + 1, c)
	return ''.join(ret[:ret.index('X') + 1])


def traffic_jam(m, S):
	M = list(m)
	for i in range(len(S))[::-1]:
		R = sum(zip(M[i:], S[i][::-1]), ())
		M[i:] = list(R) + M[i + len(R) // 2:]
	return ''.join(M)[:M.index('X') + 1]