-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay7.py
More file actions
45 lines (38 loc) · 1.34 KB
/
Day7.py
File metadata and controls
45 lines (38 loc) · 1.34 KB
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
import statistics
def crabsubmarines():
f = open("input day 7.txt", "r")
lines = f.readlines()
locations = []
for location in lines[0].split(','):
locations.append(int(location))
preflocation = int(statistics.median(locations))
fuel = 0
for location in locations:
if preflocation > location:
fuel += preflocation - location
if location > preflocation:
fuel += location - preflocation
print(fuel)
def crabsubmarines_highfuel():
f = open("input day 7.txt", "r")
lines = f.readlines()
locations = []
for location in lines[0].split(','):
locations.append(int(location))
preflocation = []
#because of rounding shenanigans I have to do this
preflocation.append(round(sum(locations) / len(locations)) - 1)
preflocation.append(round(sum(locations) / len(locations)))
preflocation.append(round(sum(locations) / len(locations)) + 1)
totalfuel = []
for ideallocation in preflocation:
fuel = 0
for location in locations:
if ideallocation > location:
n = ideallocation - location
if location > ideallocation:
n = location - ideallocation
for num in range(0, n + 1, 1):
fuel = fuel + num
totalfuel.append(fuel)
print(min(totalfuel))