Advertisement
icarussiano

day14 Julia/Python

Dec 14th, 2024 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. #part1 Julia
  2. wide, tall=101,103
  3. parse_coords(coord_str) = parse.(Int, split(split(coord_str, "=")[2], ","))
  4. robots = split.(readlines("input"))
  5. positions = [mod.(pos .+ vel .* 100, [wide, tall]) for robot in robots]
  6. quadrants = [
  7.     count(el -> el[1] > wide ÷ 2 && el[2] > tall ÷ 2, positions),
  8.     count(el -> el[1] > wide ÷ 2 && el[2] < tall ÷ 2, positions),
  9.     count(el -> el[1] < wide ÷ 2 && el[2] > tall ÷ 2, positions),
  10.     count(el -> el[1] < wide ÷ 2 && el[2] < tall ÷ 2, positions)
  11. ]
  12. println(prod(quadrants))
  13.  
  14. #part2 Python
  15. import numpy as np
  16. from numpy.fft import fft2
  17. wide,tall=101,103
  18. def next_pos(pos,vel): return (pos[0]+vel[0])%wide,(pos[1]+vel[1])%tall
  19. robots=[x.split() for x in open('input').read().splitlines()]
  20. pos = [list(map(int, x[0].split("=")[1].split(","))) for x in robots]
  21. vel = [list(map(int, x[1].split("=")[1].split(","))) for x in robots]
  22. values=[]
  23. for i in range(10000):
  24.     pos=[next_pos(p,v) for p,v in zip(pos,vel)]
  25.     values.append(np.min(np.real(fft2(np.array(pos)))))
  26.  
  27. print(np.argmin(values)+1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement