Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Symbol extend [ value: arg [^arg perform: self] ]
- Collection extend [ sum [^self inject: 0 into: [:a :b | a + b]] ]
- Array extend [
- " sum of abs differences of a count list, where 0s expand by scale "
- diffSum_expand: scale [
- | col seen sum number |
- number := self sum.
- col := seen := sum := 0.
- self do: [ :val |
- (val = 0) ifTrue: [ col := col + scale ]
- ifFalse: [ col := col + 1 ].
- sum := sum + ((2 * seen + val - number) * val * col).
- seen := seen + val.
- ].
- ^sum
- ]
- ]
- "
- | Mainline
- "
- input := stdin lines contents collect: [:line |
- line asArray collect: [:chr | (chr = $#) ifTrue: [1] ifFalse: [0]].
- ].
- dim_sums := {
- input collect: #sum. " row sums "
- (1 to: input size) collect: [:col | " col sums "
- (input collect: [:row | row at: col]) sum
- ].
- }.
- ('Part 1: %1' % {(dim_sums collect: [:d | d diffSum_expand: 2]) sum}) displayNl.
- ('Part 2: %1' % {(dim_sums collect: [:d | d diffSum_expand: 1_000_000]) sum}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement