Advertisement
musifter

AoC 2021 day 7 (dc)

Dec 7th, 2021
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. z sn # num of crab subs
  2.  
  3. [ d2*1+2%* ] S| # abs
  4. [ d sm ] S= # set maximum: leave top on stack
  5.  
  6. [
  7. d ls+ss # top -> (sum += top) top
  8. d lm<= # top -> (top > max, set new max) top
  9. d;c 1+ r:c # c(top) = c(top) + 1
  10.  
  11. z 0<L
  12. ] SL
  13.  
  14. 0 ss # sum = 0
  15. 0 sm # max init to 0
  16. lLx # load sub data
  17.  
  18. [
  19. 1+ # i -> i+1
  20. d;c lc r- d sc # i -> (count = count-crab(i)) new-count i
  21. 0<M # (loop if new-count > 0) i
  22. ] SM
  23. ln 2/sc _1 lMx
  24.  
  25. sv # v = median
  26.  
  27. [ lv- l|x ] sW # weight function: top -> abs(top - v)
  28.  
  29. [
  30. d d;c r # i -> i c(i) i
  31. lWx * # -> weight(i)*c(i) i
  32. l!+ s! # add result to part 1
  33. 1- d _1<S # i--, (loop until i = -1)
  34. ] SS
  35. 0 s! lm
  36. lSx s.
  37.  
  38. l!p
  39.  
  40. [ d 1+* 2/ ] ST # top -> triangle(top)
  41. [ lv- l|x lTx ] sW # weight function: top -> abs(top - v)
  42. [ l@ ] SU
  43.  
  44. ls ln/d1+ # (mean+1) mean
  45.  
  46. sv 0 s! lm lSx s. # run S with v = mean+1
  47. l! s@ # store result in @
  48. sv 0 s! lm lSx # run S with v = mean
  49. l!d l@r >U p # S(m+1) < S(m), U will push S(m+1), else S(m) on top
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement