musifter

AoC 2025 day 8 part 1 (dc)

Dec 14th, 2025 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.15 KB | Source Code | 0 0
  1. # Command: tr ',' ' ' <input | dc -fheap.dc -fdc-p1.dc
  2.  
  3. A000 sA
  4.  
  5. # Load input into x,y,z arrays
  6. 1 ?               # z y x i
  7. [
  8.     4R d3R r:z
  9.        d3R r:y
  10.        d3R r:x
  11.  
  12.     d Fd^  r:n
  13.  
  14.     1+ ? z1<L
  15. ] dsLx                      # i left on stack
  16.  
  17. [A0*] sE
  18.  
  19. 1-                          # num pts = i-1
  20. d A r A00=E se              # e = number of edges to merge
  21. d sn                        # n = num pts
  22.  
  23. # Calculate distance pairs and push on heap
  24. # i = num
  25. [
  26.     dsi
  27.     d1-                     # j=i-1 i
  28.     [
  29.         dsj
  30.         li;x lj;x- l2^
  31.         li;y lj;y- l2^ +
  32.         li;z lj;z- l2^ +    # dist j i
  33.  
  34.         lA* +               # append j to dist
  35.         lA* +               # append i to (dist|j)
  36.         l<x                 # push on heap
  37.  
  38.         lilj l1- dl0<J
  39.     ] dsJx +
  40.  
  41.     l1-p dl0<I
  42. ] dsIx l0*
  43.  
  44. # Initialize circuit array
  45. ln                  # i=numPoints
  46. [
  47.     d d:c
  48.     1- d0<L
  49. ] dsLx +
  50.  
  51. # Set circ[j] = a; b j a i
  52. [
  53.     r3R             # a j b i
  54.     d3R d3R         # a j j a b i
  55.     r:c             # circ[j] = a; j a b i
  56.     3R              # restore stack: b j a i
  57. ] sS
  58.  
  59. # Merge shortest edges into circuits
  60. le                  # i=edges
  61. [
  62.     l>x             # get next edge from heap
  63.     10 8^%          # strip distance
  64.     A000 ~          # aIdx bIdx i
  65.  
  66.     ;c r;c          # b=circ[bIdx] a=circ[aIdx] i
  67.  
  68.     ln              # j=numPoints b a i
  69.     [
  70.         d;c         # circ[j] j b a i
  71.         3R d3R      # cir[j] b b j a i
  72.         =S          # set if circ[j] == b; b j a i
  73.         r 1- d0<J
  74.     ] dsJx ++s.     # junk(j=0 b a);  i
  75.  
  76.     1- d0<I
  77. ] dsIx +
  78.  
  79. # Calculate sizes of subgraphs
  80. ln                  # i = numPoints+0
  81. [
  82.     d;c
  83.     d;s 1+ r:s      # sizes[circ[i]]++
  84.     1- d0<I
  85. ] dsIx +
  86.  
  87. # [r] sr            # available from heap.dc
  88.  
  89. # Get size of three largest subgraphs
  90. dd ln               # i = num points max1 max2 max3 (copying zero from start)
  91. [
  92.     d;s             # size[i] i max1 max2 max3
  93.     r _5R
  94.     [
  95.         d3R d3R >r _4R
  96.     ] dsLx lLx lLx
  97.     s.              # junk min of four; max1 max2 max3 i
  98.  
  99.     4R1- d0<I
  100. ] dsIx +
  101.  
  102. [Part 1: ]n**p
Advertisement
Add Comment
Please, Sign In to add comment