



문제점
연쇄적으로 일어나는 주변 톱니바퀴의 회전이 중복으로 되지 않게, 그리고 회전한 것으로 인해 정보가 꼬이지 않도록 회전을 일괄적으로 시켜야 한다.
해결 방법
그래서 사용한 것이 타켓 톱니바퀴를 기준으로 왼쪽을 for문으로 한번 쫙 돌리고, 오른쪽도 for문으로 한번 쫙 돌린다.
> 회전 정보는 하나씩 받아와서 회전을 수행하기 때문에 이후 다른 위치에 있는 톱니바퀴의 회전 정보에는 영향을 미치지 않는다.
모든 주변의 회전 정보에 대한 입력을 마치면 일괄적으로 회전을 시키기 위해 회전 정보를 기반으로 톱니바퀴 상태 정보가 담겨져 있는 리스트에 적용한다.
from collections import deque
all_cogwheel=deque()
for _ in range(4):
temp=input().strip()
each_cogwheel=deque()
for el in temp:
each_cogwheel.append(el)
all_cogwheel.append(each_cogwheel)
times=int(input().strip())
for _ in range(times):
target,direction=map(int,input().split())
target-=1
curr_left_el=all_cogwheel[target][6]
curr_right_el=all_cogwheel[target][2]
# 왼쪽 톱니바퀴가 존재하고, 현재 톱니바퀴의 왼쪽 상태와 왼쪽 톱니바퀴의 오른쪽 상태가 일치하지 않으면, 왼쪽 톱니바퀴 반대방향으로 돌리기
if target-1>=0 and all_cogwheel[target-1][2]!=curr_left_el:
# -1: 반시계방향, 1: 시계방향
if direction==-1:
counter_el=all_cogwheel[target-1].pop()
all_cogwheel[target-1].insert(0,clockwise_el)
else:
clockwise_el=all_cogwheel[target-1].popleft()
all_cogwheel[target-1].append(clockwise_el)
if direction==-1:
counter_el=all_cogwheel[target].popleft()
all_cogwheel[target].append(counter_el)
else:
clockwise_el=all_cogwheel[target].pop()
all_cogwheel[target].insert(0,clockwise_el)
# 오른쪽 톱니바퀴가 존재하고, 현재 톱니바퀴의 오른쪽 상태와 오른쪽 톱니바퀴의 오른쪽 상태가 일치하지 않으면, 오른쪽 톱니바퀴 반대방향으로 돌리기
if target+1>=0 and all_cogwheel[target+1][6]!=curr_right_el:
# -1: 반시계방향, 1: 시계방향
if direction==-1:
counter_el=all_cogwheel[target+1].pop()
all_cogwheel[target+1].insert(0,clockwise_el)
else:
clockwise_el=all_cogwheel[target+1].popleft()
all_cogwheel[target+1].append(clockwise_el)
if direction==-1:
counter_el=all_cogwheel[target].popleft()
all_cogwheel[target].append(counter_el)
else:
clockwise_el=all_cogwheel[target].pop()
all_cogwheel[target].insert(0,clockwise_el)
print('all_cogwheel',all_cogwheel)
from collections import deque
cogwheel=[deque(map(int,input().strip())) for _ in range(4)]
k=int(input().strip())
for _ in range(k):
target,direction=map(int,input().split())
target-=1
rotation_directions=[0]*4
rotation_directions[target]=direction
# 왼쪽 톱니바퀴 회전
for i in range(target-1,-1,-1):
if cogwheel[i][2]!=cogwheel[i+1][6]:
rotation_directions[i]=-rotation_directions[i+1]
else:
break
# 오른쪽 톱니바퀴 회전
for i in range(target+1,4):
if cogwheel[i-1][2]!=cogwheel[i][6]:
rotation_directions[i]=-rotation_directions[i-1]
else:
break
# 모든 톱니바퀴 회전
for i in range(4):
if rotation_directions[i]!=0:
cogwheel[i].rotate(rotation_directions[i])
# 점수 산정
score=0
for i in range(4):
if cogwheel[i][0]==1:
score+=pow(2,i)
print(score)