Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def gift_distribution_np(data: np.ndarray, start_idx: int = 0):
- # length of street:
- street_length = len(data)
- if start_idx == street_length:
- start_idx = 0
- n_gifts = data[start_idx]
- data[start_idx] = 0
- # each address gets at least this many gifts:
- all_gifts = n_gifts // street_length
- if all_gifts > 0:
- data += all_gifts
- # Distrbute rest of gifts over houses:
- rest_of_gifts = n_gifts % street_length
- # fill rest of street:
- end_idx = start_idx + 1 + rest_of_gifts
- if end_idx <= street_length - 1:
- data[start_idx + 1 : end_idx] += 1
- new_idx = end_idx
- else:
- data[start_idx + 1 :] += 1
- rest_of_gifts -= street_length - (start_idx + 1)
- data[0:rest_of_gifts] += 1
- new_idx = rest_of_gifts
- return new_idx, n_gifts
- if __name__ == "__main__":
- # with open("./test_input.txt") as f:
- with open("./input.txt") as f:
- data = list(map(int, f.read().split()))
- data = np.array(data)
- steps = 0
- new_start_idx = 0
- while True:
- new_start_idx, n_gifts = gift_distribution_np(data, start_idx=new_start_idx)
- start_idx = new_start_idx
- steps += n_gifts + 1
- if n_gifts == 0:
- break
- print(f"Number of steps until no gifts are left: {steps - 1}")
- with open("./input.txt") as f:
- data = list(map(lambda x: int(x) * 2025, f.read().split()))
- data = np.array(data)
- steps = 0
- new_start_idx = 0
- while True:
- new_start_idx, n_gifts = gift_distribution_np(data, start_idx=new_start_idx)
- start_idx = new_start_idx
- steps += n_gifts + 1
- if n_gifts == 0:
- break
- print(f"Number of steps until no gifts are left: {steps - 1}")
Advertisement
Add Comment
Please, Sign In to add comment