-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday7!.py
More file actions
38 lines (30 loc) · 729 Bytes
/
day7!.py
File metadata and controls
38 lines (30 loc) · 729 Bytes
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("day7full.txt") as f:
str = f.readline()
startPos = list(map(int, str.split(',')))
def sign(n):
if n >= 0:
return 1
return -1
middle = int(len(startPos) / 2)
midPos = sorted(startPos)[middle]
def fuelCost(distance):
cost = 0
for c in range(1, distance + 1):
cost += c
return cost
fuel = 0
for pos in startPos:
diff = abs(pos - midPos)
fuel += fuelCost(diff)
print(fuel)
for i in range(20):
posCopy = startPos.copy()
for pos in startPos:
for i in range( midPos + sign(pos - midPos), pos, sign(pos - midPos) ):
posCopy.append(i)
fuel = 0
for pos in posCopy:
fuel += abs(midPos - pos)
print(fuel)
middle = int(len(posCopy) / 2)
midPos = sorted(posCopy)[middle]