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] ]
- min [ ^self fold: [ :a :b | a min: b ] ]
- max [ ^self fold: [ :a :b | a max: b ] ]
- ]
- Array extend [
- + arr [ ^self with: arr collect: [:a :b | a + b] ]
- ]
- "
- | Mainline
- "
- dirs := #( (1 0 0) (-1 0 0) (0 1 0) (0 -1 0) (0 0 1) (0 0 -1) ).
- droplet := Set new.
- surface := 0.
- minimum := SmallInteger largest.
- maximum := SmallInteger smallest.
- stdin lines contents do: [:line |
- cell := (line substrings: $,) asArray apply: #asNumber.
- droplet add: cell.
- surface := surface + 6 - (2 * ((dirs collect: [:d | cell + d])
- count: [:n | droplet includes: n])).
- minimum := minimum min: cell min.
- maximum := maximum max: cell max.
- ].
- ('Part 1: %1' % {surface}) displayNl.
- " Expand bounding cube: "
- minimum := minimum - 1.
- maximum := maximum + 1.
- " BFS the encasing cube "
- encase := Set new.
- outer := 0.
- queue := OrderedCollection with: {minimum. minimum. minimum}.
- [ queue notEmpty ] whileTrue: [
- cell := queue removeFirst.
- (outer \\ 100 = 0) ifTrue: [
- stderr nextPutAll: ('Outer: %1' % {outer}); cr; flush
- ].
- (encase includes: cell) ifFalse: [
- encase add: cell.
- (dirs collect: [:d | cell + d]) do: [:neigh |
- (droplet includes: neigh) ifTrue: [
- outer := outer + 1. " ran into droplet, count this face "
- ] ifFalse: [
- (neigh conform: [:n | n between: minimum and: maximum]) ifTrue: [
- queue add: neigh
- ]
- ]
- ]
- ]
- ].
- ('Part 2: %1' % {outer}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement