Day 14: Restroom Redoubt

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • iAvicenna@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    9 days ago

    Python

    I was very confused when I saw the second part. I was like “how the fuck am I supposed now how that shape will exactly look like?” I looked a couple of initial shapes all of which looked sufficiently random. So I constructed x and y marginal distributions of the robots to impose some non-symmetry conditions.

    My initial attempt was to just require maximum of x marginal should be at the centre but the sneaky bastards apparently framed the tree and tree was shorter than I expected (realised this only later) so that did not return any hits. I played around a bit and settled for: most of the maximums of x marginal should be near the centre and y marginal should be asymmetric. I still had to play around with the thresholds for these a bit because initially there was a couple of shapes (some repeating every 100 cycles or so) that satisfied my requirements (I had a part which actually outputted found shapes to a text file but then removed that in the final code). So it wasn’t %100 free of manual labour but I can say mostly…

    import numpy as np
    from pathlib import Path
    from collections import Counter
    cwd = Path(__file__).parent
    
    
    def parse_input(file_path):
      with file_path.open("r") as fp:
        robot_info = fp.readlines()
    
      _split = lambda x,p: [int(x.split(' ')[p].split(',')[0].split('=')[-1]),
                            int(x.split(' ')[p].split(',')[-1])]
    
      robot_pos = np.array([_split(x, 0) for x in robot_info])
      robot_vel = np.array([_split(x, 1) for x in robot_info])
    
      return robot_pos, robot_vel
    
    
    def solve_problem1(file_name, nrows, ncols, nmoves):
    
      robot_pos, robot_vel = parse_input(Path(cwd, file_name))
    
      final_pos = robot_pos + nmoves*robot_vel
      final_pos = [(x[0]%ncols, x[1]%nrows) for x in list(final_pos)]
    
      pos_counts = Counter(final_pos)
      coords = np.array(list(pos_counts.keys()))[:,::-1] #x is cols, y is rows
      coords = tuple(coords.T)
    
      grid = np.zeros((nrows, ncols), dtype=int)
      grid[coords] += list(pos_counts.values())
    
      counts = [np.sum(grid[:nrows>>1, :ncols>>1]),
                np.sum(grid[:nrows>>1, -(ncols>>1):]),
                np.sum(grid[-(nrows>>1):, :ncols>>1]),
                np.sum(grid[-(nrows>>1):, -(ncols>>1):])]
    
      return int(np.prod(counts))
    
    
    def solve_problem2(file_name, nrows, ncols):
    
      robot_pos, robot_vel = parse_input(Path(cwd, file_name))
    
      grid = np.zeros((nrows, ncols), dtype=object)
    
      # update all positions in a vectorised manner
      final_positions = robot_pos[None, :, :] + np.arange(1,10000)[:,None,None]*robot_vel[None,:,:]
      final_positions[:,:,0] = final_positions[:,:,0]%ncols
      final_positions[:,:,1] = final_positions[:,:,1]%nrows
    
      for s in range(final_positions.shape[0]):
        grid[:,:] = 0
    
        final_pos = map(tuple, tuple(final_positions[s,:,:]))
    
        pos_counts = Counter(final_pos)
        coords = np.array(list(pos_counts.keys()))[:,::-1] #x is cols, y is rows
        coords = tuple(coords.T)
    
        grid[coords] += list(pos_counts.values())
    
        xmarg = np.sum(grid, axis=0)
        tops = set(np.argsort(xmarg)[::-1][:10])
        p_near_center = len(tops.intersection(set(range((ncols>>1)-5, (ncols>>1) + 6))))/10
    
        ymarg = np.sum(grid, axis=1)
        ysym = 1 - abs(ymarg[:nrows>>1].sum() - ymarg[nrows>>1:].sum())/ymarg.sum()
    
        if p_near_center>0.5 and ysym<0.8:
          return s+1