Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Collection extend [
- apply: method [ ^self collect: [:x | x perform: method] ]
- incAt: idx [ ^self at: idx put: ((self at: idx) + 1) ]
- ]
- Set subclass: LineSet [
- <shape: #pointer>
- LineSet class >> from: pt1 to: pt2 [
- ^(super new) start: pt1 end: pt2
- ]
- start: s end: e [
- | start end step pos |
- start := s y * 1000 + s x + 1.
- end := e y * 1000 + e x + 1.
- step := (e y - s y) sign * 1000 + (e x - s x) sign.
- pos := start.
- self add: pos.
- [pos = end] whileFalse: [
- pos := pos + step.
- self add: pos.
- ].
- ^self
- ]
- ]
- " Recursive divide and conquer to apply set arithmetic "
- div_and_conq := [ :lines |
- | half res1 res2 union intersect |
- (lines size = 1) ifTrue: [
- { Set from: lines first. Set new }
- ] ifFalse: [
- half := lines size // 2.
- res1 := div_and_conq value: (lines atAll: (1 to: half)).
- res2 := div_and_conq value: (lines atAll: (half + 1 to: lines size)).
- union := res1 first + res2 first.
- intersect := res1 second + res2 second + (res1 first & res2 first).
- { union. intersect }
- ]
- ].
- "
- | Mainline
- "
- ObjectMemory spaceGrowRate: 500.0.
- lines := stdin lines contents collect: [ :str |
- nums := (str subStrings: ', ->') apply: #asNumber.
- LineSet from: (nums first @ nums second) to: (nums third @ nums fourth).
- ].
- res := div_and_conq value: lines.
- ('Part 2: %1' % {res second size}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement