Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save inspirit941/4526c37247524ae10670f9182971ccfa to your computer and use it in GitHub Desktop.
Save inspirit941/4526c37247524ae10670f9182971ccfa to your computer and use it in GitHub Desktop.
from itertools import chain
# 2d Array에서 네 개의 문자열이 사각형 모양으로 일치할 경우를 파악하기
def find_square(maps):
table = [[0 for _ in range(len(maps))] for _ in range(len(maps))]
for y in range(1, len(maps)):
for x in range(1, len(maps)):
if maps[y-1][x-1] == maps[y-1][x] == maps[y][x-1] == maps[y][x] and maps[y][x] != "#":
table[y-1][x-1] += 1
table[y-1][x] += 1
table[y][x-1] += 1
table[y][x] += 1
for y in range(len(table)):
for x in range(len(table)):
maps[y][x] = 0 if table[y][x] != 0 else maps[y][x]
return maps
# 블록이 아래로 떨어지는 걸 구현하기 위해 배열을 뒤집는 함수
def rotate(maps):
return list(map(list, zip(*maps[::-1])))
# 0 개수를 세는 함수
def zero_count(maps):
return list(chain.from_iterable(maps)).count(0)
# 블록이 아래로 떨어지는 걸 구현하는 함수
def shift_blocks(maps):
for y in range(len(maps)):
arr = [i for i in maps[y] if i != 0]
for _ in range(len(maps) - len(arr)):
arr.append("#")
maps[y] = arr[::-1]
return maps
def solution(m, n, board):
board = [list(i) for i in board]
answer = 0
while True:
check_same = find_square(board)
counts = zero_count(check_same)
# print(check_same, counts)
answer += counts
if counts == 0:
break
check_same = rotate(check_same)
# print(check_same)
board = shift_blocks(check_same)
# 다음 연산을 수행하기 위해 배열을 원래대로 복구한다.
board = list(map(list,zip(*board)))
return answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment