瀏覽代碼

Move fuel calculation to a function

master
Noëlle Anthony 5 年之前
父節點
當前提交
2b3e93fd00
共有 1 個文件被更改,包括 9 次插入7 次删除
  1. 9
    7
      2019/01b.py

+ 9
- 7
2019/01b.py 查看文件

@@ -3,21 +3,23 @@ def main():
with open("01ain.txt", "r") as file:
lines = file.readlines()
masses = [int(x) for x in lines]
fuels = [(x//3) - 2 for x in masses]
fuels = [getFuel(x) for x in masses]
newfuels = []
for fuel in fuels:
of = fuel
f = fuel
while f > 0:
af = (f//3) - 2
if af > 0:
of += af
f = af
else:
f = 0
f = getFuel(f)
of += f
newfuels.append(of)
#print(newfuels)
print(sum(newfuels))

def getFuel(f):
fuel = (f//3) - 2
if fuel > 0:
return fuel
return 0

if __name__ == "__main__":
main()

Loading…
取消
儲存