Created
April 13, 2014 16:59
-
-
Save mattboehm/10592525 to your computer and use it in GitHub Desktop.
Pycon 2014 Thumbtack solution ("dense" version)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, re #thumbtack pycon tetris challenge. "dense" solution by @mattboehm | |
def shift_down(col):#shift piece down 1 space | |
loc = "".join(col[::-1]) | |
space = loc.find("#") - 1 | |
return col if loc == -2 else list(loc[:space] + loc[space+1:] + ".")[::-1] | |
def top_row_blank(board):#check if the top row has only . | |
return all(tile == "." for tile in board[0]) | |
def find_piece(board):#Replace the X's for the piece with # | |
board, marked_board = board[:], [] | |
while top_row_blank(board):#skip top blank rows | |
marked_board.append(board.pop(0)) | |
while not top_row_blank(board):#for each row with a piece, replace X's | |
marked_board.append(board.pop(0).replace("X", "#")) | |
return marked_board + board #board is >=1 blank row followed by ground | |
if __name__ == '__main__': | |
board = [list(row) for row in find_piece([line.rstrip("\n") for line in sys.stdin.readlines()])] | |
while not any(col[-1]=="#" or "#X" in "".join(col) for col in zip(*board)): | |
board = zip(*(shift_down(col) for col in zip(*board))) | |
print len([row for row in board if "." not in row]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment