advent-of-code/2021/day7.py

22 lines
529 B
Python
Raw Permalink Normal View History

2023-12-09 10:01:24 +00:00
import sys
2023-12-09 11:52:46 +00:00
import numpy as np
positions = np.asarray([int(c) for c in sys.stdin.read().strip().split(",")])
min_position, max_position = positions.min(), positions.max()
2023-12-09 10:01:24 +00:00
# part 1
2023-12-09 11:52:46 +00:00
answer_1 = min(
np.sum(np.abs(positions - position))
for position in range(min_position, max_position + 1)
)
2023-12-09 10:01:24 +00:00
print(f"answer 1 is {answer_1}")
# part 2
2023-12-09 11:52:46 +00:00
answer_2 = min(
np.sum(abs(positions - position) * (abs(positions - position) + 1) // 2)
for position in range(min_position, max_position + 1)
)
2023-12-09 10:01:24 +00:00
print(f"answer 2 is {answer_2}")