Advertisement
musifter

AoC 2024, day 5, part 1 (dc)

Dec 5th, 2024 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.00 KB | Source Code | 0 0
  1. # Command: tr -cs '[0-9\n]' ' ' <input | dc -fdc-p1.dc
  2.  
  3. [ lp ln2/1+;l+ sp ] sP
  4.  
  5. # Read in rules section
  6. ?
  7. [
  8.     rA0*+ 1 r:r
  9.     ? z0<L
  10. ] dsLx
  11.  
  12. # Process
  13. ?
  14. [
  15.     # Load list
  16.     z dsn                   # n = size of list, stack: i=0 ln ln-1 ... l1
  17.     [
  18.         d_3R :l             # set list(i)
  19.         1- d0<I
  20.     ] dsIx                  # end: 0 on stack
  21.  
  22.     # Check list
  23.     ln                      # i = n, stack: i sum=0
  24.     [
  25.         d1-                 # j = n-1, stack: j i sum
  26.         [
  27.             d3Rd            # i i j j sum
  28.             ;l3R;l          # list(j) list(i) i j sum
  29.             A0*+;r          # rule(j|i) i j sum
  30.             4R+_3R          # add rule to sum
  31.             r1- d0<J        # j--
  32.         ] dsJx +            # end: stack: i+0 sum
  33.  
  34.         1- d1<I             # i--
  35.     ] dsIx *                # end: stack: sum*1
  36.  
  37.     ln d1-*2/               # triangle( n-1 )
  38.     =P                      # P if triangle == sum
  39.  
  40.     ? z0<L
  41. ] dsLx
  42.  
  43. [Part 1: ]n lpp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement