Advertisement
musifter

AoC 2024, day 17, part 2 (dc)

Dec 17th, 2024 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.31 KB | Source Code | 0 0
  1. # Command: perl -00 -pe 's/\D*(\d+),?/$1 /g' <input | dc -fdc-p2.dc
  2.  
  3. # Ignore registers
  4. ??c
  5.  
  6. # Load code
  7. ? zsn
  8. [
  9.     z1-:c
  10.     z0<L
  11. ] dsLx
  12.  
  13. # XOR of top two elements (lowest three bits)
  14. # Uses (A + B*(-1^A)) % 4 for 2-bits and +2% for the third
  15. [
  16.     4~ 3R 4~                # (B-low2) (B-rest) (A-low2) (A-rest)
  17.     3R
  18.     d_1r^3R*+4d3R+r%        # (B^A low2) (B-rest) (A-rest)
  19.     _3R
  20.     +2%                     # (B^A third) (B^A low2)
  21.     4*+
  22. ] s^
  23.  
  24. [ s.laq ] sA    # replace top with A
  25. [ s.lbq ] sB    # replace top with B
  26. [ s.lcq ] sC    # replace top with C
  27.  
  28. # Resolve combo referencing:
  29. [
  30.     d 4=A
  31.     d 5=B
  32.     d 6=C
  33. ] sR
  34.  
  35. [r] sr
  36.  
  37. [ lRx 2r^ lar/ sa ] 0:o     # adv
  38. #[ lRx 2r^ lar/ sb ] 6:o    # bdv - unused
  39. [ lRx 2r^ lar/ sc ] 7:o     # cdv
  40.  
  41. [ lRx 8% sb ]       2:o     # bst
  42. [ lRx 8% q ]        5:o     # out
  43.  
  44. #[ 2- la 0!=r s. ]   3:o    # jnz - unneed for part 2
  45.  
  46. [ lb l^x sb ]       1:o     # bxl
  47. [ s. lblc l^x sb ]  4:o     # bxc
  48.  
  49. # Try a value for register A: stack: val -> result
  50. [
  51.     sa                      # la = arg
  52.     0                       # ip = 1
  53.     [
  54.         dd 1+;c
  55.         r;c ;ox             # [block]x arg ip => ip
  56.         2+ lMx
  57.     ] dsMx
  58.     rs.                     # clear ip
  59. ] sT
  60.  
  61. # push result: push 2nd, stack untouched
  62. [ r dSp r ] sP
  63.  
  64. _1d Sp ln+                  # push _1 on p, stack: i=n-1 (last index in code)
  65. 0r
  66.  
  67. # stack: i (values to process)
  68. [
  69.     # test possibilities (stack: i val rest)
  70.     [
  71.         d;c                     # code(i) i val rest
  72.         3R 7                    # j=7 val targ=code(i) i rest
  73.         [
  74.             d 3Rd8* 3R+ dlTx    # Try(val*8+j) val*8+j val j targ i rest
  75.             5R d3R =P           # push if (result=targ); stack: targ tryval val j
  76.             rs.r 3R             # j val targ
  77.             1- d0!>J            # j--, loop
  78.         ] dsJx ++s.             # stack: i rest
  79.  
  80.         z1<Z
  81.     ] dsZx
  82.  
  83.     # stack: i
  84.     [
  85.         Lp d_1!=L               # fully pop p stack
  86.     ] dsLx
  87.  
  88.     Sp                          # push -1 back on p
  89.     zR                          # move i to top
  90.     1- d0!>I
  91. ] dsIx s.
  92.  
  93. # Get minimum value on stack:
  94. [
  95.     d3R d3R                     # a b b a
  96.     >r s.                       # rotate top if needed, then scrap
  97.     z1<M
  98. ] dsMx
  99.  
  100. [Part 2: ]np
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement