Advertisement
musifter

AoC 2021 day 3, part 1 (dc)

Dec 3rd, 2021 (edited)
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. [q] SX
  2.  
  3. [
  4. [
  5. li;c 1+ li:c # (c(i) = c(i)++) i
  6. ] SC
  7.  
  8. [
  9. li 12=X # (exit if i = 12) top
  10. 2~ # top -> top%2 top/2
  11. 1=C # (top%2 = 1) top/2
  12. li 1+ si # (i--) top/2
  13.  
  14. lIx # loop, next top = top/2
  15. ] SI
  16.  
  17. z 0 =X # stop when stack empty
  18.  
  19. 0si lIx # i = 0, run loop
  20. s. # pop remains of top
  21.  
  22. lLx # loop
  23. ] SL
  24.  
  25. #
  26. # Mainline start
  27. #
  28.  
  29. 1010i # back to decimal
  30. z 2/ sn # n = half size of input
  31.  
  32. # Initialize counts
  33. [
  34. 1- # i--
  35. d 0 r:c # 0 i i -> (c(i) = 0) i
  36. d 0=X # (exit if i = 0)
  37. lIx # loop
  38. ] SI
  39. 12 lIx s. # push i, run loop, pop i
  40.  
  41. lLx # start loop
  42.  
  43. # Calculate gamma and epsilon (XOR via subtraction)
  44. 0 sg # init gamma, epsilon
  45.  
  46. [ 1+ ] SS
  47.  
  48. [
  49. 0 lb;c ln >S # (n > c(b)) 0, S changes 0 to 1
  50. 2 lb ^ * lg+ sg # (0|1) -> (g = g + 2^b * (0|1))
  51. lb 1+ d sb 12=X
  52.  
  53. lBx # loop
  54. ] SB
  55. 0 sb lBx
  56.  
  57. [Part 1: ]n lg 4095 lg-* p
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement