Advertisement
musifter

AoC 2022, day 1 (dc)

Dec 1st, 2022 (edited)
210
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | Source Code | 0 0
  1. [ q ] sQ
  2.  
  3. 0 sn # number of elves
  4.  
  5. # Store elf sum
  6. [
  7. s. # remove dup sentinel
  8. ln:e # store in elf(n)
  9. ln 1+ sn # n++
  10. 0 # start new sum
  11. ] sS
  12.  
  13. # Main loop
  14. [
  15. r # rotate sum under input
  16. d _1=S # check if next input is _1 (blank line)
  17. z 1=Q # if only sum left on stack, quit loop
  18. + # sum += top
  19. lLx # loop
  20. ] sL
  21.  
  22. 0 # push elf calorie sum
  23. lLx # do reading loop
  24.  
  25. # New max
  26. [
  27. d sm
  28. ] sN
  29.  
  30. # Max finding loop
  31. [
  32. d;e # get elf(idx)
  33. 3R # rotate max from bottom to top
  34. <N # set new max if greater
  35. lm;e r # put max under idx
  36. 1+ # idx++
  37. d ln>M # loop until idx == number of elves
  38. ] sM
  39.  
  40. # Run max finder
  41. [
  42. 0 sm
  43. 0;e 1 lMx # get new max
  44. ] sR
  45.  
  46. lRx
  47. s. [Part 1: ]np
  48.  
  49. 0 lm:e # zero max
  50. lRx
  51. s.+
  52.  
  53. 0 lm:e # zero max
  54. lRx
  55. s.+
  56. [Part 2: ]np
  57.  
Advertisement
Comments
  • musifter
    2 years
    # text 0.14 KB | 0 0
    1. The command I use with this for the input (which needs some mangling) is:
    2.  
    3. perl -pe 's#^$#_1#; BEGIN {print "_1 "}' input | dc -f- dc-p12.dc
Add Comment
Please, Sign In to add comment
Advertisement