Advertisement
musifter

AoC 2023 day 11 (Smalltalk)

Dec 11th, 2023 (edited)
1,694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.21 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend     [ value: arg [^arg perform: self]                    ]
  4. Collection extend [ sum        [^self inject: 0 into: [:a :b | a + b]] ]
  5.  
  6. Array extend [
  7.     " sum of abs differences of a count list, where 0s expand by scale "
  8.     diffSum_expand: scale [
  9.         | col seen sum number |
  10.  
  11.         number := self sum.
  12.         col := seen := sum := 0.
  13.         self do: [ :val |
  14.             (val = 0) ifTrue:  [ col := col + scale ]
  15.                       ifFalse: [ col := col + 1     ].
  16.  
  17.             sum  := sum + ((2 * seen + val - number) * val * col).
  18.             seen := seen + val.
  19.         ].
  20.         ^sum
  21.     ]
  22. ]
  23.  
  24. "
  25. | Mainline
  26. "
  27. input := stdin lines contents collect: [:line |
  28.              line asArray collect: [:chr | (chr = $#) ifTrue: [1] ifFalse: [0]].
  29.          ].
  30.  
  31. dim_sums := {
  32.     input collect: #sum.                                " row sums "
  33.     (1 to: input size) collect: [:col |                 " col sums "
  34.         (input collect: [:row | row at: col]) sum
  35.     ].
  36. }.
  37.  
  38. ('Part 1: %1' % {(dim_sums collect: [:d | d diffSum_expand:         2]) sum}) displayNl.
  39. ('Part 2: %1' % {(dim_sums collect: [:d | d diffSum_expand: 1_000_000]) sum}) displayNl.
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement