백준
Python
import sys
from collections import deque
input = sys.stdin.readline
g_list = [deque(list(map(int, input().rstrip()))) for _ in range(4)]
r = int(input())
r_list = []
for _ in range(r):
num, direction = map(int, input().split())
r_list.append((num - 1, direction))
def left(n, d):
if n < 0:
return
if g_list[n][2] != g_list[n + 1][6]:
left(n - 1, -d)
g_list[n].rotate(d)
def right(n, d):
if n > 3:
return
if g_list[n][6] != g_list[n - 1][2]:
right(n + 1, -d)
g_list[n].rotate(d)
for n, d in r_list:
left(n - 1, -d)
right(n + 1, -d)
g_list[n].rotate(d)
print(g_list[0][0] * 1 + g_list[1][0] * 2 + g_list[2][0] * 4 + g_list[3][0] * 8)