Advertisement
musifter

AoC day 9, part 1, dc

Dec 9th, 2020
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 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+ sj lj 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+ si li 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 ps. # 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement