Advertisement
musifter

AoC 2024, day 5, part 2 (dc)

Dec 5th, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.40 KB | Source Code | 0 0
  1. # Command: tr -cs '[0-9\n]' ' ' <input | dc -fdc-p1.dc
  2.  
  3. [ lc+sc 0 ] sC                # add rule lookup to c, replace with 0
  4. [ rd;l lp+sp r ] sS           # score list(second)
  5.  
  6. [
  7.     lnd2/r                    # stack: i=n n/2
  8.     [
  9.         d3Rd 3R;c=S           # if count(i) == n/2 then score
  10.         r1- d0<I
  11.     ] dsIx +s.
  12. ] sP
  13.  
  14. # Read in rules section
  15. ?
  16. [
  17.     rA0*+ 1 r:r
  18.     ? z0<L
  19. ] dsLx
  20.  
  21. # Process
  22. ?
  23. [
  24.     # Load list
  25.     z dsn                   # n = size of list, stack: i=0 ln ln-1 ... l1
  26.     [
  27.         d_3R :l             # set list(i)
  28.         1- d0<I
  29.     ] dsIx                  # end: 0 on stack
  30.  
  31.     # Check list
  32.     ln                      # i = n, stack: i total=0
  33.     [
  34.         0sc
  35.         0r ln               # j=n i sum=0 total
  36.         [
  37.             dd 4Rdd         # i i i j j j sum total
  38.             ;l 4R;l         # list(j) list(i) i j sum total
  39.             A0*+ ;r         # rule(j|i) i j sum total
  40.             r4R >C          # keep rule checks with j>i out of sum for now
  41.             4R + _3R        # add rule to sum
  42.             r1- d0<J        # j--
  43.         ] dsJx +            # end: stack: i+0 sum total
  44.  
  45.         d3Rd lc+ 3R:c       # counts(i) = sum+c; stack: sum i total
  46.         3R+r                # total += sum
  47.  
  48.         1- d0<I             # i--
  49.     ] dsIx +                # end: stack: total+0
  50.  
  51.     ln d1-*2/ !=P
  52.     ? z0<L
  53. ] dsLx
  54.  
  55. [Part 2: ]n lpp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement