-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday7.py
More file actions
76 lines (50 loc) · 1.71 KB
/
day7.py
File metadata and controls
76 lines (50 loc) · 1.71 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
""" Advent of Code 2021 day 7 """
import numpy as np
INPUT_FILE = "input7.txt"
def get_fuel(pos, target_pos):
return abs(pos - target_pos)
def get_increasing_fuel(pos, target_pos):
return sum(range(1, abs(pos - target_pos) + 1))
def get_sum_fuel_part2(positions):
avg = int(np.average(positions))
fuel = 0
for pos in positions:
fuel += get_increasing_fuel(pos, avg)
return fuel
def get_sum_fuel_part1(positions):
median = int(np.median(positions))
fuel = 0
for pos in positions:
fuel += get_fuel(pos, median)
return fuel
def read_input(file_name):
return np.loadtxt(file_name, dtype=int, delimiter=",")
if __name__ == "__main__":
positions = read_input(INPUT_FILE)
print("Part 1:\t", get_sum_fuel_part1(positions))
print("Part 2:\t", get_sum_fuel_part2(positions))
exit()
# very messy first attempt
positions = np.loadtxt(INPUT_FILE, dtype=int, delimiter=",")
print(np.mean(positions), "aaa")
print(np.median(positions))
print(positions)
print(max(positions))
maximum = max(positions)
minimum = min(positions)
min_fuel = maximum * len(positions)
for i in range(minimum, maximum + 1):
this_fuel = 0
for pos in positions:
this_fuel += abs(pos - i)
min_fuel = min(min_fuel, this_fuel)
print(min_fuel)
min_fuel_2 = maximum * len(positions) * len(positions)
for i in range(minimum, maximum + 1):
this_fuel = 0
for pos in positions:
# for j in range(abs(pos-i+1)):
# this_fuel += j
this_fuel += sum(list(range(1, abs(pos - i) + 1)))
min_fuel_2 = min(min_fuel_2, this_fuel)
print("2:", min_fuel_2)