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>
- | ortho |
- LineSet class >> from: pt1 to: pt2 [
- ^(super new) start: pt1 end: pt2
- ]
- start: s end: e [
- | pos end step |
- pos := 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.
- self add: pos.
- [pos = end] whileFalse: [
- pos := pos + step.
- self add: pos.
- ].
- ortho := (e x = s x) or: [e y = s y].
- ^self
- ]
- ortho [ ^ortho ]
- ]
- "
- | Mainline
- "
- ObjectMemory spaceGrowRate: 500.0.
- " Make lines and partition them into orthogonal (part 1) and diagonal. "
- parts := stdin lines contents inject: {Set new. Set new} into: [ :acc :str |
- | line |
- nums := (str subStrings: ', ->') apply: #asNumber.
- line := LineSet from: (nums first @ nums second)
- to: (nums third @ nums fourth).
- (line ortho) ifTrue: [ acc first add: line ]
- ifFalse: [ acc second add: line ].
- acc
- ].
- count := 0.
- list := Array new: 1000000 withAll: 0.
- #(1 2) do: [ :part |
- (parts at: part) do: [ :line |
- line do: [ :x | ((list incAt: x) = 2) ifTrue: [count := count + 1] ].
- ].
- ('Part %1: %2' % {part. count}) displayNl.
- ]
- 62,2 Bo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement