musifter

AoC day 22 (pt 1) dc

Dec 26th, 2020 (edited)
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. [q] sX
  2.  
  3. z 2/ d se sf # get deck sizes (assuming half of input each), set end ptrs
  4. 0 sp # set the start ptr (next card to be played)
  5.  
  6. # initialize arrays with input (S macro parameter to set array)
  7. [
  8. li 1- dsi lSx
  9. li 0=X
  10. lIx
  11. ] sI
  12.  
  13. # S macros to parameterize the above I macro
  14. [:a]sS le si lIx
  15. [:b]sS lf si lIx
  16.  
  17. # Macro to calculate score (R macro parameter to read array)
  18. [
  19. le 1- d lRx li * r se + # top += read_array( --end_ptr ) * i
  20. le lp =X # check if (end == start)
  21. li 1+ si # inc i
  22. lVx # loop
  23. ] sV
  24.  
  25. # R macros to parameterize the above V macro
  26. [ [;a]sR 1 si 0 lVx q ] sP
  27. [ [;b]sR lf se 1 si 0 lVx q ] sQ # parameterize the end_pointer by setting e = f
  28.  
  29. # Handle case where player 1 wins hand
  30. [
  31. lp d;a le:a
  32. d;b le 1+:a 1+ sp
  33. le 2+ se
  34. s.0 # replace the -1 on the stack with a 0
  35. ] sA
  36.  
  37. # Handle case where player 2 wins hand
  38. [
  39. lp d;b lf:b
  40. d;a lf 1+:b 1+ sp
  41. lf 2+ sf
  42. ] sB
  43.  
  44. [
  45. _1 # push -1 to signal when A macro doesn't run
  46. lp ddd le =Q lf =P # check end of game, leaving two copies of ptr
  47.  
  48. ;b r ;a >A # compare a(p) > b(p)
  49. _1 =B # if -1 left on stack, do else B macro
  50.  
  51. lLx
  52. ] sL
  53. lLx
  54.  
  55. [Part 1: ]np
Add Comment
Please, Sign In to add comment