Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- String extend [
- asRadix: rad [
- ^Number readFrom: (ReadStream on: self) radix: rad.
- ]
- ]
- Object subclass: MemoryTable [
- | mem or_mask fl_mask |
- MemoryTable class >> new [
- ^(super new) init
- ]
- init [
- mem := Dictionary new.
- or_mask := 0.
- fl_mask := 0.
- ^self
- ]
- setMask: mask [
- or_mask := (mask copyReplaceAll: 'X' with: '0') asRadix: 2.
- fl_mask := (mask collect: [:c | (c = $X) ifTrue: [$1] ifFalse: [$0]]) asRadix: 2.
- ]
- at: addr put: val float: mask [
- (mask = 0) ifTrue: [
- mem at: addr put: val.
- ] ifFalse: [
- | bit next |
- bit := mask - (mask bitAnd: (mask - 1)). " get lowest set bit "
- next := mask bitXor: bit. " toggle bit off "
- self at: (addr bitAnd: bit bitInvert) put: val float: next.
- self at: (addr bitOr: bit) put: val float: next.
- ]
- ]
- at: addr put: val [
- self at: (addr bitOr: or_mask) put: val float: fl_mask.
- ]
- checksum [
- ^mem fold: [ :a :b | a + b ].
- ]
- ]
- "
- | Mainline
- "
- memory := MemoryTable new.
- ((stdin contents tokenize: 'mask = ') reject: [:a | a size = 0]) do: [ :block |
- cmds := (block tokenize: '\n') asOrderedCollection.
- mask := cmds removeFirst.
- stdout nextPutAll: ('Mask: ', mask); cr; flush.
- memory setMask: mask.
- cmds do: [ :set |
- pair := set subStrings: 'mem[] ='.
- memory at: (pair first) asNumber put: (pair second) asNumber.
- ].
- ].
- stdout nl.
- ('Part 2: ', memory checksum asString) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement