Advertisement
musifter

AoC 2024 day 17, part 1 (dc)

Dec 17th, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.09 KB | None | 0 0
  1. # Command: perl -00 -pe 's/\D*(\d+),?/$1 /g' <input | dc -fdc-p1.dc | sed -e's/,$//'
  2.  
  3. # Load registers
  4. ? sc sb sa
  5. ?   # blank line
  6.  
  7. # Load code
  8. ? zsn
  9. [
  10.     z1-:c
  11.     z0<L
  12. ] dsLx
  13.  
  14. # XOR of top two elements (lowest three bits)
  15. # Uses (A + B*(-1^A)) % 4 for 2-bits and +2% for the third
  16. [
  17.     4~ 3R 4~                # (B-low2) (B-rest) (A-low2) (A-rest)
  18.     3R
  19.     d_1r^3R*+4d3R+r%        # (B^A low2) (B-rest) (A-rest)
  20.     _3R
  21.     +2%                     # (B^A third) (B^A low2)
  22.     4*+
  23. ] s^
  24.  
  25. [ s.laq ] sA    # replace top with A
  26. [ s.lbq ] sB    # replace top with B
  27. [ s.lcq ] sC    # replace top with C
  28.  
  29. # Resolve combo referencing:
  30. [
  31.     d 4=A
  32.     d 5=B
  33.     d 6=C
  34. ] sR
  35.  
  36. [r] sr
  37.  
  38. [ lRx 2r^ lar/ sa ] 0:o     # adv
  39. [ lRx 2r^ lar/ sb ] 6:o     # bdv
  40. [ lRx 2r^ lar/ sc ] 7:o     # cdv
  41.  
  42. [ lRx 8% sb ]       2:o     # bst
  43. [ lRx 8% n44an ]    5:o     # out
  44.  
  45. [ 2- la 0!=r s. ]   3:o     # jnz
  46.  
  47. [ lb l^x sb ]       1:o     # bxl
  48. [ s. lblc l^x sb ]  4:o     # bxc
  49.  
  50. # stack frame: ip
  51. 0
  52. [
  53.     dd 1+;c
  54.     r;c ;ox                 # [block]x arg ip => ip
  55.     2+d ln>M
  56. ] dsMx
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement