Lv2 프로그래머스(Programmers)[Python][파이썬] 마법의 엘리베이터

"""
출처:프로그래머스,
https://school.programmers.co.kr/learn/courses/30/lessons/148653
"""
# 풀이 과정
def solution(storey):
x = list(str(storey))
x = list(map(int, x))
count = 0

if len(x) == 1:
return x[0] if x[0] <= 5 else 10 - x[0] + 1

for k in range(len(x) - 1, 0, -1):
a = 10 - x[k]
b = x[k]
if b < 5:
count += b
elif b == 5:
if k - 1 >= 0 and x[k - 1] >= 5:
x[k - 1] = x[k - 1] + 1
count += b
else:
count += b

else:
count += a
if k - 1 >= 0:
x[k - 1] += 1

if x[0] <= 5:
return count + x[0]
else:
return count + 10 - x[0] + 1