Day 1 challenge

This commit is contained in:
Thibaud Gasser 2019-12-03 21:55:23 +01:00
parent 25cbed8b41
commit 1428bb14dc
2 changed files with 141 additions and 0 deletions

41
day1/day1.py Normal file
View File

@ -0,0 +1,41 @@
#! /usr/bin/env python3
def calculate_fuel_iterative(mass):
total = 0
new_mass = mass
while True:
new_mass = new_mass // 3 - 2
if new_mass < 0:
break
total += new_mass
return total
def calculate_total_fuel_mass(input_file, mass_function=lambda x: x // 3 - 2):
total = 0
with open(input_file) as masses:
for mass in masses:
total += mass_function(int(mass))
return total
def test_part2():
inputs = (14, 1969, 100756)
expected = (2, 966, 50346)
for i, inp in enumerate(inputs):
result = calculate_fuel_iterative(inp)
assert result == expected[i], "Result for {} should be {}, was {}".format(
inp, expected[i], result
)
print("All tests passed for part 2.")
if __name__ == "__main__":
print("Part 1 - total mass: ", calculate_total_fuel_mass("./input.txt"))
test_part2()
print(
"Part 2 -- total mass: ",
calculate_total_fuel_mass("./input.txt", calculate_fuel_iterative),
)

100
day1/input.txt Normal file
View File

@ -0,0 +1,100 @@
62259
75368
93740
119724
112546
137714
96999
130673
102398
73819
100734
85337
62764
82115
127696
54391
103213
77954
112513
112392
138404
92989
108521
83163
109720
91918
114443
54306
90623
66833
58505
85919
77539
149419
128385
66452
94677
109179
62072
137245
136226
145783
60689
103320
145931
101286
63458
122468
87858
105675
146185
57417
96883
70739
97494
140951
149416
83137
66122
134319
58511
139600
102929
112240
149634
64142
83332
129526
99058
148889
50087
74961
133606
143518
68849
97045
73920
61357
115941
56740
111773
77880
90792
77103
111355
125898
56547
84918
113822
74113
98557
80928
60519
146379
59354
102490
72584
59000
63151
114253