Guest User

Untitled

a guest
Nov 28th, 2025
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. import numpy as np
  2.  
  3.  
  4.  
  5. def gift_distribution_np(data: np.ndarray, start_idx: int = 0):
  6.     # length of street:
  7.     street_length = len(data)
  8.     if start_idx == street_length:
  9.         start_idx = 0
  10.     n_gifts = data[start_idx]
  11.     data[start_idx] = 0
  12.     # each address gets at least this many gifts:
  13.     all_gifts = n_gifts // street_length
  14.     if all_gifts > 0:
  15.         data += all_gifts
  16.     # Distrbute rest of gifts over houses:
  17.     rest_of_gifts = n_gifts % street_length
  18.     # fill rest of street:
  19.     end_idx = start_idx + 1 + rest_of_gifts
  20.     if end_idx <= street_length - 1:
  21.         data[start_idx + 1 : end_idx] += 1
  22.         new_idx = end_idx
  23.     else:
  24.         data[start_idx + 1 :] += 1
  25.         rest_of_gifts -= street_length - (start_idx + 1)
  26.         data[0:rest_of_gifts] += 1
  27.         new_idx = rest_of_gifts
  28.     return new_idx, n_gifts
  29.  
  30.  
  31. if __name__ == "__main__":
  32.     # with open("./test_input.txt") as f:
  33.     with open("./input.txt") as f:
  34.         data = list(map(int, f.read().split()))
  35.     data = np.array(data)
  36.  
  37.     steps = 0
  38.     new_start_idx = 0
  39.     while True:
  40.         new_start_idx, n_gifts = gift_distribution_np(data, start_idx=new_start_idx)
  41.         start_idx = new_start_idx
  42.         steps += n_gifts + 1
  43.         if n_gifts == 0:
  44.             break
  45.     print(f"Number of steps until no gifts are left: {steps - 1}")
  46.  
  47.     with open("./input.txt") as f:
  48.         data = list(map(lambda x: int(x) * 2025, f.read().split()))
  49.     data = np.array(data)
  50.  
  51.     steps = 0
  52.     new_start_idx = 0
  53.     while True:
  54.         new_start_idx, n_gifts = gift_distribution_np(data, start_idx=new_start_idx)
  55.         start_idx = new_start_idx
  56.         steps += n_gifts + 1
  57.         if n_gifts == 0:
  58.             break
  59.     print(f"Number of steps until no gifts are left: {steps - 1}")
  60.  
Advertisement
Add Comment
Please, Sign In to add comment