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] ]
- prod [ ^self inject: 1 into: [:a :b | a * b] ]
- lcm [ ^self fold: [:a :b | a lcm: b] ]
- ]
- Object subclass: Monkey [
- | num items op arg test pass checks |
- Monkey class >> new: desc [
- ^super new init: desc
- ]
- init: desc [
- " Monkey 0: (smalltalk arrays start at 1, so +1)"
- num := desc first second asNumber + 1.
- " Starting items: 79, 98 "
- items := (desc second allButFirst: 2) apply: #asNumber.
- " Operation: new = old + 19 (ASSUME 0 isn't used here)"
- " Operation: new = old * old (... so 0 represents this)"
- op := (desc third at: 5) asSymbol.
- arg := desc third last asNumber.
- " Test: divisible by 23 "
- test := desc fourth last asNumber.
- " If true: throw to monkey 2 (+1 for array indexing)"
- " If false: throw to monkey 3 (+1 for array indexing)"
- pass := Dictionary from: {
- true -> ((desc at: 5) last asNumber + 1).
- false -> ((desc at: 6) last asNumber + 1).
- }.
- checks := 0.
- ^self
- ]
- inspect: item [
- checks := checks + 1.
- ^item perform: op with: (arg = 0 ifTrue: [item] ifFalse: [arg]).
- ]
- testPass: worry [
- ^pass at: (worry \\ test = 0).
- ]
- testValue [ ^test ]
- items [ ^items ]
- numInspect [ ^checks ]
- ]
- "
- | Mainline
- "
- monkeys := ((stdin contents tokenize: '\n\n') collect: [ :blk |
- blk lines apply: #substrings " break into lines and words "
- ]) collect: [:txt | Monkey new: txt].
- (1 to: 20) do: [ :round |
- monkeys do: [ :monk |
- [monk items notEmpty] whileTrue: [
- | item |
- item := (monk inspect: monk items removeFirst) // 3.
- (monkeys at: (monk testPass: item)) items addLast: item.
- ]
- ]
- ].
- ('Part 1: %1' % {((monkeys apply: #numInspect) sorted last: 2) prod}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement