Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Array extend [
- incAt: key [
- ^self at: key put: (self at: key) + 1
- ]
- ]
- " Read and sort input "
- adapt := SortedCollection with: 1.
- stdin linesDo: [ :line | adapt add: (line asNumber + 1) ].
- " Calculate difference counts "
- counts := Array withAll: #(0 0 1).
- (1 to: adapt size - 1) do: [ :i |
- counts incAt: (adapt at: i+1) - (adapt at: i)
- ].
- stdout nextPutAll: 'Part 1: ', ((counts at: 3) * (counts at: 1)) asString; nl.
- " Part 2 -- Dynamic Programming "
- table := Array new: adapt last.
- table at: 1 put: 1.
- " Progressively build table using previous calculated values "
- adapt do: [ :i |
- ((table at: i) = nil) ifTrue: [
- conx := adapt select: [ :a | a between: (i-3) and: (i-1) ].
- sum := conx inject: 0 into: [ :a :b | a + (table at: b) ].
- table at: i put: sum.
- ]
- ].
- stdout nextPutAll: 'Part 2: ', table last asString; nl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement