Advertisement
musifter

AoC 2022, day 3, part 1 (dc)

Jan 16th, 2023
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | Source Code | 0 0
  1. [q] sQ
  2.  
  3. [ d l1+ s1 q] sP
  4.  
  5. # Find and score error (stack: count)
  6. [
  7. d;f lh r- 1+ # half - f(count) + 1
  8. r d;l lh - # l(count) - half
  9. 3R* 0<P # call P (score) if (h - f(c) + 1) * (l(c) - h) is +'ve
  10. 1- d 0<E # count--, loop if > 0
  11. ] sE
  12.  
  13. # Set f(pri) to index, stack: pri rest
  14. [
  15. zr # push index of item, rotate pri up
  16. d_3R # dup pri, tuck under index
  17. :l # l(pri) = index
  18. ] sS
  19.  
  20. # Loop (stack: items)
  21. [
  22. 32~ r 3- 26*- # convert item to priority
  23. d;l 0=S # check l(pri) == 0, set; stack: pri rest
  24. z r:f # push index, f(pri) = index
  25. z 0<L
  26. ] sL
  27.  
  28. # Main loop
  29. [
  30. ? # get line
  31. z d2/ sh # h = half length
  32. 0=Q # quit if 0
  33. lLx # parse to build f() and l() arrays
  34.  
  35. 52 # counter
  36. lEx s. # find error item and score, clear count
  37.  
  38. 0Sf 0Sl # clear f and l
  39. lMx
  40. ] sM
  41. lMx
  42.  
  43. [Part 1: ]n l1p
  44.  
  45. [<END>]ps.
  46. [<stack>]ps. f [<bottom>]ps.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement