Advertisement
musifter

AoC 2023 day 7 part 1 (dc)

Dec 7th, 2023 (edited)
819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.52 KB | Source Code | 0 0
  1. 16i                         # Working in base 16
  2.  
  3. # Load input onto reg i stack
  4. 0Sl                         # Push sentinel 0 to mark bottom
  5. ?
  6. [
  7.     rd                      # bid hand hand
  8.     0 Sc                    # Clear cards and group arrays with push
  9.     [
  10.         10~                 # remaining card
  11.         d;c 1+ r:c          # count(c)++
  12.         d0<C                # loop while remaining>0
  13.     ] dsCx
  14.  
  15.     E                       # bid hand groups=0 i=E
  16.     [
  17.         d;c                 # g i count(i)
  18.         10r^                # g i 10^c(i)
  19.         3R+                 # i g+10^c(i)
  20.         r1- d1<I            # g i--
  21.     ] dsIx
  22.  
  23.     *                       # clear i (*1)
  24.     10 5^*+ 1000*+          # bid hand groups -> combined groups-hand-bid
  25.  
  26.     Sl                      # store hand in register stack for sorting
  27.     ?z 0<L
  28. ] dsLx
  29.  
  30. # Sort hands
  31. [rq] sQ
  32. Ll Ll                       # next (ASSUME: at least 2 hands to sort)
  33. [
  34.     [
  35.         d3R d3R             # next top -> next top top next
  36.         !>Q                 # if top >= next, rotate next on top and quit J loop
  37.  
  38.         Sl                  # push top back on input stack
  39.         z 1<J               # quit if out next left on stack
  40.     ] dsJx                  # next rest
  41.  
  42.     Ll d 0<S
  43. ] dsSx
  44.  
  45. # stack: sorted-hands 0
  46. 1                           # hands lowest-hand sum=0 i=1
  47. [
  48.     d4R                     # sum i i low
  49.     1000%*                  # sum i i*bid
  50.     3R+                     # i sum
  51.     r1+ z2<M
  52. ] dsMx
  53.  
  54. rp
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement