Advertisement
musifter

AoC 2023 day 7 part 2 (dc)

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