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]] ]
- String extend [
- " Interpret (\d+)-(\d+) string as an Interval "
- asInterval [
- | range |
- range := (self subStrings: '-') collect: [:n | n asInteger].
- ^Interval from: range first to: range second.
- ]
- ]
- Interval extend [
- " Get intersection of self and another Interval "
- & other [ ^(self first max: other first) to: (self last min: other last) ]
- " Get union of self and another Interval (ASSUMES: intersecting)"
- + other [ ^(self first min: other first) to: (self last max: other last) ]
- ]
- "
- | Mainline
- "
- sections := (stdin contents tokenize: '\n\n') collect: #lines.
- ranges := sections first collect: #asInterval.
- food := sections second collect: #asNumber.
- " Part 1: "
- part1 := food count: [:id | ranges contains: [:r | id between: r first and: r last]].
- ('Part 1: %1' % {part1}) displayNl.
- " Part 2: "
- merged := OrderedCollection new.
- ranges do: [:r |
- curr := r.
- next := OrderedCollection new.
- merged do: [:o |
- ((r & o) size > 0) ifTrue: [ curr := curr + o ]
- ifFalse: [ next add: o ].
- ].
- merged := next, {curr}.
- ].
- ('Part 2: %1' % {(merged collect: #size) sum}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment