Advertisement
musifter

AoC 2024, day 7, part 1 (dc)

Dec 7th, 2024
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.00 KB | Source Code | 0 0
  1. # Command: tr -d ':' <input | dc -fdc-p1.dc
  2.  
  3. [ 0*  ] sZ
  4. [ lt+ ] sT
  5.  
  6. # exit recursion: idx=0 acc -> acc or 0
  7. [
  8.     + dlt!=Z            # zero top of stack it acc != target
  9.     q
  10. ] sX
  11.  
  12. # recursion: stack frame: acc idx; return acc or 0
  13. [
  14.     r 1-d 0=X           # idx--, check base case, stack: idx acc
  15.  
  16.     d3Rd
  17.     3R d;l 3R+          # list(idx)+acc idx acc idx
  18.     lRx                 # res acc idx
  19.     _3R                 # tuck res
  20.  
  21.     r d;l 3R*
  22.     lRx                 # res* res+
  23.     +                   # "or" results with +
  24. ] sR
  25.  
  26. 0
  27. ?
  28. [
  29.     0
  30.     [
  31.         1+
  32.         d3R r:l         # store list (backwards n to 1)
  33.         z3<I
  34.     ] dsIx              # stack: n=size-of-list target sum
  35.  
  36.     rst                 # target = input(0); stack: n sum
  37.     d;l                 # acc = list(n) (first item); stack: acc i=n sum
  38.     lRx                 # res sum
  39.     0d 3R>T             # res = (res > 0) ? targ : 0
  40.     +                   # sum += res
  41.     ? z1<M
  42. ] dsMx
  43.  
  44. [Part 1: ]n p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement