Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Character extend [
- " Priority score for letters, a-z (1-26), A-Z (27-52). XXX: No safety. "
- priority [
- ^self isLowercase ifTrue: [ self asInteger - 96 ]
- ifFalse: [ self asInteger - 38 ] " - 64 + 26 "
- ]
- ]
- Set extend [
- " Return element if there's only one, else return nil. "
- onlyOne [ ^(self size = 1) ifTrue: [self anyOne] ifFalse: [nil] ]
- ]
- "
- | Mainline
- "
- part1 := 0.
- part2 := 0.
- stdin contents lines keysAndValuesDo: [ :num :sack |
- " Part 1 "
- comp1 := Set from: (sack first: sack size / 2).
- comp2 := Set from: (sack last: sack size / 2).
- part1 := part1 + (comp1 & comp2) onlyOne priority.
- " Part 2 state machine "
- state := num \\ 3.
- (state = 1) ifTrue: [
- " start new group "
- badge := sack asSet.
- ] ifFalse: [
- " add to group, score badge every third "
- badge := badge & sack asSet.
- (state = 0) ifTrue: [
- part2 := part2 + badge onlyOne priority
- ]
- ]
- ].
- ('Part 1: %1' % {part1}) displayNl.
- ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement