Advertisement
musifter

AoC 2024, day 1, part 1 (dc)

Dec 1st, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.94 KB | Source Code | 0 0
  1. # Command: dc -fdc-p1.dc <input
  2.  
  3. [q] sQ
  4.  
  5. # Read Input
  6. ?
  7. [
  8.     # stack: item2 item1
  9.     d;b 1+ r:b          # b(item2)++
  10.     d;a 1+ r:a          # a(item1)++
  11.     ? z0<I
  12. ] dsIx
  13.  
  14. # sorting loop, paramertized (lGx: get from reg array, lPx: push to reg stack)
  15. [
  16.     99999
  17.     [
  18.         dlGx            # j=get(i) i
  19.         [
  20.             d 0=Q       # j i, quit if j == 0
  21.             rd lPx r    # Push copy of i on register stack
  22.             1- lJx      # j--
  23.         ] dsJx +        # end: junk j=0 with addition
  24.         1- d0<I         # i--, loop while i > 0
  25.     ] dsIx
  26. ] sS
  27.  
  28. 0 Sc                    # add sentinel to bottom of c
  29. [;a]sG [Sc]sP lSx       # convert array a into sorted stack c
  30. [;b]sG [Sd]sP lSx       # convert array b into sorted stack d
  31.  
  32. # Process
  33. 0                       # sum
  34. [
  35.     Lc d0=Q             # pop item, quit if == 0
  36.     Ld - d*v +          # sum += abs(item1 - item2)
  37.     lLx
  38. ] dsLx
  39.  
  40. [Part 1: ]nrp
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement