-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.py
57 lines (45 loc) · 1.39 KB
/
day7.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import statistics
def part1():
positions = list()
with open('resources/input7.txt') as f:
lines = f.readlines()
entries = lines[0].strip().split(',')
for entry in entries:
positions.append(int(entry))
prev_fuel_cost = 99999999
for i in range(1, 1300):
fuel_cost = calculate_fuel_cost(positions, i)
if fuel_cost < prev_fuel_cost:
prev_fuel_cost = fuel_cost
else:
print(str(i) + " : " + str(prev_fuel_cost))
break
def calculate_fuel_cost(postisions, i):
total = 0
for position in postisions:
total += abs(position - i)
return total
def part2():
positions = list()
with open('resources/input7.txt') as f:
lines = f.readlines()
entries = lines[0].strip().split(',')
for entry in entries:
positions.append(int(entry))
prev_fuel_cost = 99999999999999
for i in range(1, 1300):
fuel_cost = calculate_fuel_cost2(positions, i)
if fuel_cost < prev_fuel_cost:
prev_fuel_cost = fuel_cost
else:
print(str(i) + " : " + str(prev_fuel_cost))
break
def calculate_fuel_cost2(postisions, i):
total = 0
for position in postisions:
for value in range(1, abs(position - i)+1):
total += value
return total
if __name__ == '__main__':
part1()
part2()