Advertisement
musifter

AoC 2023 day 9, part 1 (dc)

Dec 9th, 2023
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.02 KB | Source Code | 0 0
  1. # Command: tr '-' '_' <input | dc -fpart1.dc
  2.  
  3. 0                           # sum = 0
  4. ?
  5. [
  6.     zsn                     # n = length
  7.     [
  8.         z:a                 # array(z) = top
  9.         z1<L
  10.     ] dsLx
  11.  
  12.     ln                      # stack: sum i=len
  13.     [
  14.         dd;a                # sum i j=i a(j)
  15.         d5R+                # i j a(j) sum+=a(j)
  16.  
  17.         _4Rr                # sum i a(j) j=i
  18.         [
  19.             1-              # j--
  20.  
  21.             d;a             # sum a(j+1) j a(j)
  22.             d 4Rr-          # diff = a(j+1) - a(j)
  23.  
  24.             3Rd             # sum a(j) diff j j
  25.             _3R:a           # a(j)=diff; stack: sum old-a(j) j
  26.  
  27.             d 0<J           # actually need only to 2<J, but this makes clearing junk easy
  28.         ] dsJx
  29.  
  30.         # stack: sum i junk 0
  31.         *+                  # clear junk
  32.  
  33.         1- d1<I             # only need 2<J again, but saves a stroke cleaning up
  34.     ] dsIx
  35.  
  36.     *                       # junk i (*1)
  37.  
  38.     ? z1<M
  39. ] dsMx
  40.  
  41. [Part 1: ]np
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement