Day 10: Hoof It

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
    2
    ·
    edit-2
    2 days ago

    J

    Who needs recursion or search algorithms? Over here in line noise array hell, we have built-in sparse matrices! :)

    data_file_name =: '10.data'
    grid =: "."0 ,. > cutopen fread data_file_name
    data =: , grid
    'rsize csize' =: $ grid
    inbounds =: monad : '(*/ y >: 0 0) * (*/ y < rsize, csize)'
    coords =: ($ grid) & #:
    uncoords =: ($ grid) & #.
    NB. if n is the linear index of a point, neighbors n lists the linear indices
    NB. of its orthogonally adjacent points
    neighbors =: monad : 'uncoords (#~ inbounds"1) (coords y) +"1 (4 2 $ 1 0 0 1 _1 0 0 _1)'
    uphill1 =: dyad : '1 = (y { data) - (x { data)'
    uphill_neighbors =: monad : 'y ,. (#~ (y & uphill1)) neighbors y'
    adjacency_of =: monad define
       edges =. ; (< @: uphill_neighbors"0) i.#y
       NB. must explicitly specify fill of integer 0, default is float
       1 edges} 1 $. ((#y), #y); (0 1); 0
    )
    adjacency =: adjacency_of data
    NB. maximum path length is 9 so take 9th power of adjacency matrix
    leads_to_matrix =: adjacency (+/ . *)^:8 adjacency
    leads_to =: dyad : '({ & leads_to_matrix) @: < x, y'
    trailheads =: I. data = 0
    summits =: I. data = 9
    scores =: trailheads leads_to"0/ summits
    result1 =: +/, 0 < scores
    result2 =: +/, scores
    
      • ystael@beehaw.org
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        2 days ago

        Yes. I don’t know whether this is a beehaw specific issue (that being my home instance) or a lemmy issue in general, but < and & are HTML escaped in all code blocks I see. Of course, this is substantially more painful for J code than many other languages.