비트 마스킹
을 생각했다.알고리즘 문제에서 비트 마스킹의 단점이 있다면,
가독성과 디버깅 용이성인것 같다.
너무 사소한 것에서 한참 헤맸다..
import sys
input = sys.stdin.readline
def getResult():
result = 0
scores = tuple((1,2,4,8))
for gear, score in zip(gears, scores):
if gear & (1 << 7):
result += score
return result
def setLeftDirection(gear, directions):
for i in reversed(range(1, gear+1)):
left = right = 'N'
if gears[i-1] & (1 << 5): left = 'S'
if gears[i] & (1 << 1): right = 'S'
if left == right: break
directions[i-1] = -directions[i]
def setRightDirection(gear, directions):
for i in range(gear, 3):
left = right = 'N'
if gears[i] & (1 << 5): left = 'S'
if gears[i+1] & (1 << 1): right = 'S'
if left == right: break
directions[i+1] = -directions[i]
def rotate(directions):
for i in range(4):
if directions[i] == 1:
lastBit = True if gears[i] & 1 else False
gears[i] >>= 1
if lastBit:
gears[i] |= (1 << 7)
if directions[i] == -1:
firstBit = True if gears[i] & (1 << 7) else False
gears[i] <<= 1
if firstBit:
gears[i] &= ~(1 << 8)
gears[i] += 1
def roll(gear, direction):
directions = [0]*4
directions[gear] = direction
setLeftDirection(gear, directions)
setRightDirection(gear, directions)
rotate(directions)
gears = [int(input(), 2) for _ in range(4)]
k = int(input())
for _ in range(k):
gear, direction = map(int, input().split())
roll(gear-1, direction)
print(getResult())