Advertisement
musifter

AoC 2021 day 9 (dc)

Dec 9th, 2021
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #!/usr/bin/dc -finput
  2.  
  3. z sn # lines of input
  4. 0 sw # width of lines
  5.  
  6. [1+] S1 # inc
  7. [ d2*1+2%* ] s| # abs
  8. [ d l|x d 0=1 / ] sS # sign
  9.  
  10. # load array, find maximum width line of input
  11. [ dZ sw ] sW
  12.  
  13. [
  14. dZ lw<W # num -> (max width < len(num), set new width) num
  15. z :g # store line in grid
  16. z 0<L # loop until stack empty
  17. ] sL
  18. lLx
  19.  
  20. # add top and bottom borders
  21. lw2+ 10r^1- d 0:g ln1+:g
  22.  
  23. # add borders to left and right
  24. [
  25. dd # i -> i i i
  26. ;g lw 10r^ 9*+ 10*9+ # g(i) i i -> ((g(i) + 10^x * 9) * 10 + 9) i i
  27. r:g # (g(i) = number with 9 borders) i
  28. 1- d 0<L # i--, loop while > 0
  29. ] sL
  30. ln lLx
  31.  
  32. 0 ss
  33.  
  34. [s. _1] sZ # replace top of stack with -1 (so it adds zero)
  35. [ ;gr 10r^/ 10% ] s= # y x -> digit x of g(y)
  36.  
  37. [
  38. lx ly l=x dddd 0r # g(y,x) five times, insert sum of signs pos 2
  39. lx1+ ly l=x - lSx+ r # take sign of diff for dir, add to sum of signs
  40. lx1- ly l=x - lSx+ r # ditto
  41. lx ly1+ l=x - lSx+ r # ditto
  42. lx ly1- l=x - lSx+ # ditto
  43. _4!=Z 1+ ls+ ss # if sum of signs isn't -4, make it add zero to score
  44.  
  45. lx1- dsx 0<X
  46. ] sX
  47.  
  48. [
  49. lw sx lXx
  50. ly1- dsy 0<Y
  51. ] sY
  52. ln sy lYx
  53.  
  54. [Part 1: ]n lsp
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement