-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.py
38 lines (30 loc) · 1.01 KB
/
day07.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
with open("day07-input.txt", "r") as f:
data = f.readlines()
data = [int(x) for x in data[0].split(",")]
def leastFuel(data):
minX = min(data)
maxX = max(data)
iterations = []
for alignPoint in range(minX, maxX):
fuelConsumption = []
for x in data:
fuelConsumption.append(abs(alignPoint - x))
iterations.append(fuelConsumption)
totals = [sum(iteration) for iteration in iterations]
lowest = min(totals)
return lowest
def leastFuelWithResistance(data):
minX = min(data)
maxX = max(data)
lowest = 9999999999
for alignPoint in range(minX, maxX):
fuelConsumption = 0
for x in data:
distance = abs(alignPoint - x)
fuelConsumption += int(distance * (distance + 1) / 2)
if fuelConsumption < lowest:
lowest = fuelConsumption
return lowest
if __name__ == "__main__":
print(f"Day07a: {leastFuel(data)}") # 337833
print(f"Day07b: {leastFuelWithResistance(data)}") # 96678050