Advertisement
musifter

AoC 2021 day1 (dc)

Dec 1st, 2021
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #!/usr/bin/dc -finput
  2.  
  3. [q] SX # exit loop macro
  4.  
  5. [ lx 1+ sx ] S1 # inc counter for part 1
  6. [ ly 1+ sy ] S2 # inc counter for part 2
  7.  
  8. # Main loop:
  9. [
  10. z 0 =X # stop when stack empty
  11.  
  12. d li;p >1 # top -> (p(i) > top) top -> top
  13.  
  14. d li 1+ 3% d si # top -> (i+1)%3 top top, next i = (i+1)%3
  15. ;p >2 # -> (p(next i) > top) top -> top
  16.  
  17. li:p # p(next i) = pop
  18.  
  19. lLx # loop
  20. ] SL
  21.  
  22. # Mainline:
  23. 0d sx sy # init counters for results
  24.  
  25. # Initialize previous array
  26. # Note that input is pushed, so we're going backwards (first is actually last)
  27. 0:p d 0;p >1 # compare top two, store first
  28. 1:p d 1;p >1 # compare next two, store second
  29. 2:p # store third
  30.  
  31. 2 si # init index into previous array
  32. lLx # start loop
  33.  
  34. [Part 1: ]n lxp
  35. [Part 2: ]n lyp
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement