musifter

AoC 2021 day 4 (dc)

Dec 4th, 2021
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #!/usr/bin/dc
  2.  
  3. [q] SX
  4.  
  5. ###################################################################################
  6.  
  7. # Initial loading of data into stack and queue
  8.  
  9. 100 sz # z = number of boards (hardcoded)
  10.  
  11. # Push card data onto - register stack
  12. [
  13. r # i top -> top i next
  14. S- # (push top on - stack) i next
  15. 1- d 0=X # ((i-1) == 0 exit) (i-1) next
  16. lIx
  17. ] sI
  18. lz 25* lIx s. # push counter of items, call loop, pop the counter
  19.  
  20. # Load remaining items into the caller list (q)
  21. [
  22. z 0 =X # exit when stack empty
  23. li:q # top -> (q(i) = top)
  24. li 1- si # i++
  25. lIx
  26. ] sI
  27. z1- si lIx
  28.  
  29. ###################################################################################
  30.  
  31. # Initialize the number hash to -1 for all values 0-top
  32. [
  33. 1- # i--
  34. d _1 r:n # 0 i i -> (n(i) = _1) i
  35. d 0=X # (exit if i = 0)
  36. lNx
  37. ] sN
  38.  
  39. # load card into number hash
  40. [
  41. li 1- d si # (i = i-1) (i-1)
  42. _1=X # ((i-1) == -1 exit)
  43. L- # top = pop item from card input
  44. d ls+ ss # top -> (s = s + top) top
  45. li r:n # (n(top) = i)
  46. lBx
  47. ] sB
  48.  
  49. # clear line count array
  50. [
  51. 1- dd # j-- -> j j j
  52. 0 r:l # (l(j) = 0) j j
  53. 0=X # (j = 0 exit) j
  54. lJx
  55. ] sJ
  56.  
  57. # Macro to call to initialize card
  58. [
  59. 10 lJx s. # initialize line counts
  60. 100 lNx s. # initialize number hash
  61. 0 ss # init s (sum of numbers on card)
  62. 25si lBx # load card into hash
  63. ] sC
  64.  
  65. ###################################################################################
  66.  
  67. # Output result from winning card
  68. [
  69. [Calls: ]n li1+n
  70. [ Result: ]n lc ls*p
  71. c
  72. 3Q
  73. ] sP
  74.  
  75. # Macro to mark card
  76. [
  77. lc ls r- ss # pos -> (s = s - call) pos
  78. 5~5+ # pos -> yline xline
  79.  
  80. d;l 1+ # l(yline)+1 yline xline
  81. d 5=P # (l(yline)+1 == 5 print) l(yline)+1 yline xline
  82. r:l # (l(yline) = l(yline)+1) xline
  83.  
  84. d;l 1+ #
  85. d 5=P # ditto above (only now working on xline)
  86. r:l #
  87.  
  88. 0 # place number on top of stack for return
  89. ] sM
  90.  
  91. #
  92. # Main loop
  93. #
  94. [
  95. li;q # next number from q(i)
  96. d sc # (c = num) num
  97. ;n d _1!=M # num -> (n(num) != _1 mark card) n(num)
  98. s. # clear 0 or -1 depending if M ran
  99. li 1+ si # i++
  100. lIx # loop
  101. ] sI
  102.  
  103. [
  104. lz 1- d sz _1=X # z--, exit if z == -1
  105.  
  106. lCx # initialize card data
  107. 0si lIx # start calling loop
  108.  
  109. lZx
  110. ] SZ
  111. lZx
  112.  
Add Comment
Please, Sign In to add comment