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] ]
- String extend [
- reindeerHash [ ^self inject: 0 into: [:a :b | ((a + b asciiValue) * 17) \\ 256] ]
- ]
- "
- | Mainline
- "
- input := stdin nextLine tokenize: ','.
- part1 := 0.
- boxes := (1 to: 256) collect: [:c | OrderedCollection new].
- input do: [ :str |
- step := (str =~ '(\w+)([=-])(\d?)').
- label := (step at: 1) asSymbol.
- oper := (step at: 2) first.
- part1 := part1 + str reindeerHash.
- box := label reindeerHash + 1.
- found := (boxes at: box) detect: [:l | l first = label] ifNone: [nil].
- (oper = $-) ifTrue: [
- found ifNotNil: [ (boxes at: box) remove: found ].
- ] ifFalse: [ " oper '=' "
- focus := (step at: 3) asInteger.
- found ifNil: [ (boxes at: box) add: {label. focus} ]
- ifNotNil: [ found at: 2 put: focus ].
- ]
- ].
- part2 := 0.
- boxes keysAndValuesDo: [:i :box |
- box keysAndValuesDo: [:j :lens |
- part2 := part2 + (i * j * lens second).
- ].
- ].
- ('Part 1: %1' % {part1}) displayNl.
- ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement