Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Symbol extend [ value: arg [^arg perform: self] ]
- Collection extend [ sum [^self inject: 0 into: [:a :b | a + b]] ]
- " Extending Booleans to do multiplication "
- True extend [ * num [^num] ]
- False extend [ * num [^0] ]
- Integer extend [
- " Concatenation operation for integers "
- || arg [ ^self * (10 raisedToInteger: (arg + 1) log ceiling) + arg ]
- " Test if self can be made from list using +, *, || "
- ? list [ ^(self recurse: list first with: list allButFirst) * self ]
- recurse: acc with: list [
- | next nlist |
- (list isEmpty or: [acc > self]) ifTrue: [^self = acc].
- next := list first.
- nlist := list allButFirst.
- ^(self recurse: acc + next with: nlist)
- or: [(self recurse: acc * next with: nlist)
- or: [self recurse: acc || next with: nlist]]. " remove for part 1 "
- ]
- ]
- "
- | Mainline
- "
- input := stdin lines contents collect: [:line | line subStrings collect: #asNumber].
- part2 := (input collect: [:list | list removeFirst ? list]) sum.
- ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement