Advertisement
musifter

AoC day 14 (pt1), Smalltalk

Dec 14th, 2020
2,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. String extend [
  4.     asRadix: rad [
  5.         ^Number readFrom: (ReadStream on: self) radix: rad.
  6.     ]
  7. ]
  8.  
  9. memory := LookupTable new.
  10.  
  11. or_mask  := 0.
  12. and_mask := 0.
  13.  
  14. ((stdin contents tokenize: 'mask = ') reject: [:a | a size = 0]) do: [ :block |
  15.     cmds := (block tokenize: '\n') asOrderedCollection.
  16.  
  17.     mask := cmds removeFirst.
  18.     or_mask  := (mask copyReplaceAll: 'X' with: '0') asRadix: 2.
  19.     and_mask := (mask copyReplaceAll: 'X' with: '1') asRadix: 2.
  20.  
  21.     cmds do: [ :set |
  22.         pair := set subStrings: 'mem[] ='.
  23.         val := ((pair second) asNumber bitOr: or_mask) bitAnd: and_mask.
  24.         memory at: (pair first) put: val.
  25.     ].
  26. ].
  27.  
  28. ('Part 1: ', (memory fold: [ :a :b | a + b ]) asString) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement