Advertisement
musifter

AoC 2024, day 11 (dc)

Dec 11th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.70 KB | Source Code | 0 0
  1. # Command: echo 75 | dc -finput -fdc-p12.dc
  2.  
  3. ?st
  4.  
  5. [ 1+        ] s+    # increment
  6. [ *1+ q     ] sZ    # return(1): clear frame (depth==0) * value + 1
  7. [ _4R++s. q ] sM    # return(memo), clear frame (three items)
  8.  
  9. [ +s. A0 0  ] sH    # replace hashes with impossible ones (no hash ends in two 0s)
  10.  
  11.  
  12. # Even (stack: value depth-1 hash)
  13. [
  14.     dZ 2/Ar^        # 10^(len(value)/2) value depth-1 hash
  15.     ~               # rhs lhs depth-1 hash
  16.     3Rd             # depth-1 depth-1 rhs lhs hash
  17.     3R r lRx        # recurse(depth-1 rhs) depth-1 lhs hash
  18.     _3R             # tuck result
  19.     lRx             # recurse(depth-1 lhs) result-rhs hash
  20.     +               # result-sum hash
  21.     d 3R:m q        # memo(hash)=result, return result
  22. ] sE
  23.  
  24. # recurse: depth value -> count
  25. [
  26.     d 0=Z           # return (1) if (depth == 0)
  27.  
  28.     d1-3Rd          # value value depth-1 depth
  29.     A0*4R+          # hash=100*value+depth value depth-1
  30.  
  31.     dd 9d^<H        # cancel hash if > max; stack: hash-write hash-read value depth
  32.                     # max hash is actually 2^31 - 1, 9d^ is short and big enough
  33.  
  34.     _4R             # tuck hash-write
  35.     ;m d0<M         # memo check hash-read; return if in memo
  36.  
  37.                     # stack: 0 value depth-1 hash-write
  38.     r dZ2% 3R=E     # if (0==len(value)%2) do Even; stack: value depth-1 hash
  39.  
  40.     2024*           # 2024*value depth-1 hash
  41.     d 0=+           # 2024*value|1 depth-1 hash
  42.     r lRx           # recurse(depth-1 2024*value|1) hash
  43.     d 3R:m          # memo(hash)=rec; stack: rec (return value)
  44. ] sR
  45.  
  46. 0                   # sum=0 input1 input2 ...
  47. [
  48.     r
  49.     lt lRx+          # sum += recurse(25, input1) input2 ...
  50.  
  51.     z1<L
  52. ] dsLx
  53.  
  54. p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement