-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.py
48 lines (38 loc) · 1.11 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
import math
import statistics
import adventutils
fuelcache = dict()
def part1(file):
print("Part 1")
crabposlist = adventutils.file_contents(file)
crabs = [int(crab) for crab in crabposlist[0].split(",")]
crabs.sort()
median = int(statistics.median(crabs))
fueltotal = 0
for crab in crabs:
fueltotal += abs(crab - median)
print(fueltotal)
def part2(file):
print("Part 2")
crabposlist = adventutils.file_contents(file)
crabs = [int(crab) for crab in crabposlist[0].split(",")]
average = statistics.mean(crabs)
fueltotal = 0
fueltotalfloor = 0
for crab in crabs:
fueltotal += crabfuel(abs(crab - math.ceil(average)))
fueltotalfloor += crabfuel(abs(crab - math.floor(average)))
print(min(fueltotal, fueltotalfloor))
def crabfuel(num):
total = 0
if num in fuelcache.keys():
return fuelcache[num]
elif num == 0:
return 0
for step in range(1, num + 1):
total += step
fuelcache[num] = total
return total
if __name__ == "__main__":
part1("./data/day7part1.txt")
part2("./data/day7part1.txt")