Day 4: 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

  • ystael@beehaw.org
    link
    fedilink
    arrow-up
    1
    ·
    5 hours ago

    J

    Had to actually render output! What is this “user interface” of which you speak?

    J doesn’t have meaningful identifiers for system interfaces built into the core language because why would you ever do that. It’s all routed through the “foreign conjunction” !:. There are aliases in the library, like fread, but if the documentation gives a list of all of them, I haven’t found it. We’re doing 1980 style system calls by number here. 1 !: 2 is write(), so x (1 !: 2) 2 writes x (which must be a list of characters) to stdout. (6 !: 3) y is sleep for y seconds.

    It’s inefficient to compute, but I looked for low spots in the mean distance between robots to find the pattern for part 2. The magic numbers (11 and 101) were derived by staring at the entire series for a little bit.

    load 'regex'
    data_file_name =: '14.data'
    raw =: cutopen fread data_file_name
    NB. a b sublist y gives elements [a..a+b) of y
    sublist =: ({~(+i.)/)~"1 _
    parse_line =: monad define
       match =: 'p=(-?[[:digit:]]+),(-?[[:digit:]]+) v=(-?[[:digit:]]+),(-?[[:digit:]]+)' rxmatch y
       2 2 $ ". y sublist~ }. match
    )
    initial_state =: parse_line"1 > raw
    'positions velocities' =: ({."2 ; {:"2) initial_state
    steps =: 100
    size =: 101 103
    step =: (size & |) @: +
    travel =: step (steps & *)
    quadrant =: (> & (<. size % 2)) - (< & (<. size % 2))
    final_quadrants =: quadrant"1 @: travel"1
    quadrant_ids =: 4 2 $ 1 1 _1 1 1 _1 _1 _1
    result1 =: */ +/"1 quadrant_ids -:"1/ positions final_quadrants velocities
    
    render =: monad define
       |: 'O' (<"1 y)} size $ '.'
    )
    pair_distances =: monad : 'y (| @: j./ @: -/"1)/ y'
    loop =: dyad define
       positions =. positions step"1 (velocities * x)
       for_i. i. 1000 do.
          time_number =. x + i * y
          mean_distance =. (+/ % #) , pair_distances positions
          if. mean_distance < 50 do.
             (render positions) (1!:2) 2
             (": time_number, mean_distance) (1!:2) 2
             (6!:3) 1
          end.
          if. mean_distance < 35 do. break. end.
          positions =. positions step"1 (velocities * y)
       end.
       time_number
    
    result2 =: 11 loop 101