Skip to content

Instantly share code, notes, and snippets.

@ShinJJang
Last active January 3, 2020 18:19
Show Gist options
  • Save ShinJJang/1c40904861d79b06db6ec5a1643192ad to your computer and use it in GitHub Desktop.
Save ShinJJang/1c40904861d79b06db6ec5a1643192ad to your computer and use it in GitHub Desktop.
def solution(bridge_length, weight, truck_weights):
pos = []
wts = []
answer = 0
while(True):
if sum(wts) == 0 and len(truck_weights) == 0:
break
print(answer, pos, wts, truck_weights)
first_weight = truck_weights[0] if len(truck_weights) > 0 else 0
move = 1
if first_weight == 0 or len(pos) == bridge_length or sum(wts) + first_weight > weight:
move = bridge_length + 1 - pos[0]
pos = [p + move for p in pos]
if len(pos) > 0 and pos[0] > bridge_length:
pos.pop(0)
wts.pop(0)
if first_weight > 0 and sum(wts) + first_weight <= weight:
pos.insert(len(pos), 1)
wts.insert(len(wts), truck_weights.pop(0))
answer += move
return answer
#print(solution(2, 10, [7, 4, 5, 6])) # 8
print(solution(5, 5, [2, 2, 2, 2, 1, 1, 1, 1, 1])) # 19
#print(solution(100, 100, [10, 10, 10, 10, 10, 10, 10, 10, 10, 10])) # 110
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment