Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Array subclass: SnafuNumber [
- <shape: #pointer>
- value := Dictionary from: { $= -> -2. $- -> -1. $0 -> 0. $1 -> 1. $2 -> 2 }.
- char := Dictionary from: { -2 -> $=. -1 -> $-. 0 -> $0. 1 -> $1. 2 -> $2 }.
- SnafuNumber class >> fromString: str [ ^(super new: 32 withAll: 0) initString: str ]
- initString: str [
- str reverse keysAndValuesDo: [:i :chr | self at: i put: (value at: chr)].
- ^self
- ]
- + other [
- | res val |
- res := SnafuNumber new: 32 withAll: 0.
- (1 to: self size - 1) do: [ :i |
- val := (self at: i) + (other at: i) + (res at: i).
- " do appropriate carry, if needed "
- (val abs > 2) ifTrue: [
- res at: i+1 put: val sign.
- val := val - (5 * val sign).
- ].
- res at: i put: val.
- ].
- ^res
- ]
- printOn: aStream [
- | printing |
- printing := false. " to ignore leading zeroes"
- self reverse do: [ :digit |
- (printing or: [digit ~= 0]) ifTrue: [
- aStream nextPut: (char at: digit).
- printing := true.
- ]
- ].
- (printing not) ifTrue: [ aStream nextPut: $0 ].
- ]
- ]
- "
- | Mainline
- "
- input := stdin lines contents collect: [:line | SnafuNumber fromString: line].
- ('Part 1: %1' % {input fold: [:a :b | a + b]}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement