Advertisement
musifter

AoC day 9, dc

Dec 9th, 2020
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. 0k
  2.  
  3. z sn # n = input size
  4. 25 sm # m = window size
  5.  
  6. [ LRx q ] sX # Return macro, call cleanup and quit
  7. [ q ] sQ # Exit loop without cleanup
  8.  
  9. # Loop: Read in input file
  10. [ Lis. LLs. ] SR # cleanup for loop
  11. [
  12. z 0 =X # process stack
  13. li 1- d si :a # pop into a @ --i
  14. lLx
  15. ] SL
  16. ln Si lLx
  17.  
  18. # Loop: Initialize window
  19. [ LLs. s. ] SR # cleanup for loop
  20. [
  21.  
  22. dd ;a r :w # w @ index = a @ index, index on top
  23. 1- d _1 =X # index--, break if -1
  24. lLx
  25. ] SL
  26. lm 1- lLx
  27.  
  28. # Part 1
  29. [
  30. # Locals:
  31. [
  32. LCs. LSs. LLs. LAs.
  33. Lps. Lqs.
  34. ] SR
  35.  
  36. 0 Sp # ptr for Shift window
  37. lm Sq # next_ptr for Shift window
  38.  
  39. # Shift window
  40. [
  41. lq ;a lp :w # w @ ptr = a @ next_ptr
  42. lp 1+ lm% sp # ptr++ mod window size
  43. lq 1+ sq # next_ptr++
  44. ] SS
  45.  
  46. # Check window
  47. [
  48. # Locals:
  49. [
  50. LTs. LIs. LJs.
  51. Lis. Ljs.
  52. ] SR
  53.  
  54. [ 1 LRx 4Q ] ST
  55.  
  56. 0 Si 1 Sj
  57.  
  58. # inner loop
  59. [
  60. li;w lj;w + lq;a =T # found sum, return True
  61.  
  62. lj 1+ d sj lm =Q # j++, break if j == window size
  63. lJx
  64. ] SJ
  65.  
  66. # outer loop
  67. [
  68. li 1+ sj # j = i + 1
  69. lJx # call inner j loop
  70.  
  71. li 1+ d si lm 1- =Q # i++, break if i == window size - 1
  72. lIx
  73. ] SI
  74. lIx
  75.  
  76. 0 LRx # return False
  77. ] SC
  78.  
  79. # Print Answer
  80. [
  81. [Part 1: ]n lq;a p # Print answer
  82. LRx q
  83. ] SA
  84.  
  85. # Foreach Window
  86. [
  87. lCx 0 =A # Print and exit when check fails
  88.  
  89. lSx lq ln 1+ =X # shift window, break at end
  90. lLx # loop
  91. ] SL
  92. lLx
  93. ] S1
  94. l1x
  95.  
  96. # Part 2 (passing in part 1)
  97. [
  98. # Locals:
  99. [
  100. LWs. L+s. L-s.
  101. Lts. Lss. # target value, and running sum
  102. Lis. Ljs. # shifing window, i to j
  103. LKs. LLs. LHs.
  104. Lks. Lls. Lhs. # variables for finding min/low, max/high
  105. ] SR
  106.  
  107. St # t = passed in top of stack
  108. 0 Si 0 Sj 0;a Ss # i, j = 0, s = a @ 0
  109.  
  110. [
  111. lj 1+ d sj ;a ls + ss # sum += a @ (++j)
  112. ] S+
  113.  
  114. [
  115. li d ;a ls r- ss 1+ si # sum -= a @ (i++)
  116. ] S-
  117.  
  118. # Sliding window loop
  119. [
  120. ls lt =Q # break if sum == target
  121. ls lt >+ # too low, add next element
  122. ls lt <- # too high, remove first element
  123. lWx
  124. ] SW
  125. lWx
  126.  
  127. [ d sl ] SL # update min/low
  128. [ d sh ] SH # update max/high
  129.  
  130. # Loop for finding min/low and max/high
  131. [
  132. lk ;a
  133. d ll >L
  134. d lh <H
  135.  
  136. lk 1+ d sk lj =Q
  137. lKx
  138. ] SK
  139.  
  140. # init: low = target, high = 0, k = i
  141. lt Sl
  142. 0 Sh
  143. li Sk
  144. lKx
  145.  
  146. [Part 2: ]n ll lh + ps.
  147. LRx
  148. ] S2
  149. l2x
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement