Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Collection extend [
- decAt: idx [ ^self at: idx put: ((self at: idx) - 1) ]
- ]
- "
- | Mainline
- "
- coordDirs := {(-1 @ -1). ( 0 @ -1). ( 1 @ -1).
- (-1 @ 0). ( 1 @ 0).
- (-1 @ 1). ( 0 @ 1). ( 1 @ 1)}.
- " Load input "
- input := stdin lines contents.
- width := input first size + 1.
- dirs := coordDirs collect: [:c | c y * width + c x].
- grid := Array new: ((input size + 2) * width + 1).
- input keysAndValuesDo: [:y :line |
- line keysAndValuesDo: [:x :char |
- (char == $@) ifTrue: [ grid at: (y * width + x + 1) put: 0 ].
- ].
- ].
- " Calculate number of neighbours "
- rolls := (grid keys select: [:r | (grid at: r) notNil]) asSet.
- rolls do: [:roll |
- neigh := dirs collect: [:d | roll + d].
- grid at: roll put: (neigh count: [:n | (grid at: n) notNil]).
- ].
- " Get queue of removable rolls "
- removable := (rolls select: [:roll | (grid at: roll) < 4]) asOrderedCollection.
- ('Part 1: %1' % {removable size}) displayNl.
- " Remove rolls one at a time, adding rolls when they become removable "
- part2 := 0.
- [ removable notEmpty ] whileTrue: [
- next := removable removeFirst.
- part2 := part2 + 1.
- neigh := (dirs collect: [:d | next + d]).
- rolls remove: next.
- (neigh select: [:n | rolls includes: n]) do: [:n |
- ((grid decAt: n) == 3) ifTrue: [
- removable addLast: n.
- ]
- ]
- ].
- ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment