musifter

AoC 2025 day 8 part 2 (dc)

Dec 8th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.46 KB | Source Code | 0 0
  1. # Command: tr ',' ' ' </tmp/input | dc -fdc-p2.dc
  2.  
  3. # Load input into x,y,z arrays
  4. 1 ?               # z y x i
  5. [
  6.     4R d3R r:z
  7.        d3R r:y
  8.        d3R r:x
  9.  
  10.     d Fd^  r:n
  11.  
  12.     1+ ? z1<L
  13. ] dsLx
  14.  
  15. 1- dsn           # num pts
  16.  
  17. # update nearest: j dist i
  18. [
  19.     d3R d3R         # j dist dist j i
  20.     :n              # nearest(j) = dist; dist j i
  21.     r 3R d3R d3R    # i j j i dist
  22.     r:e             # end(j) = i; j i dist
  23.     3Rr             # restore stack
  24. ] sN
  25.  
  26. # i = num
  27. [
  28.     dsi
  29.     d1-          # j=i-1 i
  30.     [
  31.         dsj
  32.         li;x lj;x- 2^
  33.         li;y lj;y- 2^ +
  34.         li;z lj;z- 2^ +     # dist j i
  35.  
  36.         d3R d3R             # dist j j dist i
  37.         r;n                 # nearest(j) dist j dist i
  38.         >N                  # update nearest(j); j dist i
  39.  
  40.         _3R d3R d3R         # dist i i dist j
  41.         r;n
  42.         >N                  # update nearest(i); i dist j
  43.  
  44.         rs.r                # j i
  45.         1- d0<J
  46.     ] dsJx +
  47.  
  48.     1- d0<I
  49. ] dsIx 0*                   # XXX: Why does this exit with -1?
  50.  
  51. # stack: 0
  52.  
  53. # change max idx: max i -> max=i i
  54. [ s.d ] sM
  55.  
  56. ln                          # i=n max=0
  57. [
  58.     d;n                     # nearest(i) i max
  59.     3R d;n 3R               # nearest(i) nearest(max) max i
  60.     >M                      # update max; max i
  61.  
  62.     r 1- d0<L
  63. ] dsLx +
  64.  
  65. # stack: max
  66.  
  67. d;e                         # end max
  68. ;x r;x                      # max->x end->x
  69. *
  70.  
  71. [Part 2: ]np
Advertisement
Add Comment
Please, Sign In to add comment