-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.py
47 lines (31 loc) · 1.19 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
from math import ceil, floor
import statistics
with open("./input7.txt") as file:
positions = [int(number) for number in file.readline().split(",")]
def count_moves(positions, target):
moves = 0
for position in positions:
moves += abs(target - position)
return moves
def triangle(number):
return sum(range(number + 1))
def count_moves_triangle(positions, target):
moves = 0
for position in positions:
moves += triangle(abs(target - position))
return moves
def find_least_moves(positions, around):
left, *_, right = range(around - 10, around + 11)
min_moves = 1_000_000_000_000_000
min_position = 0
for target in range(left, right):
if (current := count_moves_triangle(positions, target)) < min_moves:
min_moves = current
min_position = target
return min_position, min_moves
if __name__ == "__main__":
median = int(statistics.median(positions))
print(f"Part 1\nPosition: {median}, Fuel used: {count_moves(positions, median)}")
mean = round(statistics.mean(positions))
position, fuel_used = find_least_moves(positions, mean)
print(f"Part 2\nPosition: {position}, Fuel used: {fuel_used}")