Advertisement
musifter

AoC 2021 day 11 (dc)

Dec 11th, 2021
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #!/usr/bin/dc -finput
  2.  
  3. [q] sX
  4. [3Q] sQ
  5. [1+] s+ # inc
  6.  
  7. [
  8. 10~ # top -> top%10 top/10
  9. li1+ d si :g # (g(i++) = top%10) top/10
  10. d 0!=I # (top/10 != 0, loop) top/10
  11. ] sI
  12.  
  13. [
  14. lIx s. # process number into g(), junk remnants
  15. li1+ si # i++ (for side borders)
  16. z 0<L # loop until stack empty
  17. ] sL
  18. 0 si lLx
  19.  
  20. ###############################################################################
  21.  
  22. [li] sP # push i on stack to mark flashed
  23.  
  24. # Charge grid by 1 (set i=1 and leave copy on stack, leaves i remnant)
  25. [
  26. d d;g 1+ r:g ;g # i -> (g(i)=g(i)+1) i -> new-g(i)
  27. 10=P # flash on 10, P pushes i back on stack
  28. li1+ d11% 0=+ dsi # i++, i++ again if 0 mod 11
  29. d 110>C # i -> (i < 110, loop) i
  30. ] sC
  31.  
  32. ################################################################################
  33. # Flash
  34. #
  35. [
  36. la r
  37. ] sB
  38.  
  39. # Validator
  40. [
  41. d 1>Q d 109<Q d11% 0=Q # idx, if bad exit back to F (idx still on stack)
  42. ] sV
  43.  
  44. [
  45. lVx # validate
  46. dsa # store idx in a
  47. d d;g 1+ r:g ;g # i -> (g(i)=g(i)+1) i -> new-g(i)
  48. 10=B # flash on 10, B pushed a back and rotates
  49. 0 # put remnant on stack to junk
  50. ] sA
  51.  
  52. [
  53. d 1 r:f # top -> (f(top) = 1) top
  54. d 12- lAx s. # top -> top-12 top, call A, toss remnant
  55. d 11- lAx s. # ditto
  56. d 10- lAx s.
  57. d 1- lAx s.
  58. d 1+ lAx s.
  59. d 10+ lAx s.
  60. d 11+ lAx s.
  61. d 12+ lAx s. s.
  62. ] sF
  63.  
  64. ################################################################################
  65.  
  66. # Zero stack and count flashes
  67. [
  68. d 0 r:g # i -> (g(i) = 0) i
  69. ln1+ sn # num flashes ++
  70. ] sN
  71.  
  72. [
  73. d d;f 1=N # i -> (f(i) = 1, count flash) i i
  74. 0 r:f # i i -> (f(i) = 0) i
  75. 1- d 0<Z # i--, loop if > 0
  76. ] sZ
  77.  
  78. ################################################################################
  79.  
  80. [ [Part 1: ]n lfp ] s1
  81.  
  82. # Process flashers from stack
  83. [
  84. z 0=X # stop if stack empty
  85. d;f 0=F # top -> (f(top) = 0 flash) top
  86. lSx # loop
  87. ] sS
  88.  
  89. # Main loop
  90. [
  91. 1 d si lCx s. # charge grid, stack now flashers
  92. lSx # process stack
  93.  
  94. 0sn 109 lZx s. # zero flash array, counting flashes
  95. ln lf+ sf # add number to flash total
  96.  
  97. ln 100=X # exit if 100 flashed this turn
  98. lt1+ d st 100=1 # t++, print part 1 on t = 100
  99. lTx # loop
  100. ] sT
  101.  
  102. 0 sf # flashes = 0
  103. 1 st lTx # time = 1, start loop
  104.  
  105. [Part 2: ]n ltp
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement