musifter

AoC day 3, part 2 (smalltalk)

Dec 3rd, 2021
800
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. " Some conventient covering extensions: "
  4. String extend [
  5.     asRadix: rad [ ^Number readFrom: (ReadStream on: self) radix: rad ]
  6. ]
  7.  
  8. " Extending Set with a method to do the work for this script "
  9. Set extend [
  10.     filter: wantMin [
  11.         | i |
  12.  
  13.         i := 1.
  14.         [self size > 1] whileTrue: [
  15.             | sets sort |
  16.  
  17.             sets := Dictionary from: { $0 -> Set new. $1 -> Set new }.
  18.             self do: [ :num | (sets at: (num at: i)) add: num ].
  19.  
  20.             sort := sets associations sorted: [:a :b |
  21.                         ((a value size > b value size) | (a key > b key)) xor: wantMin
  22.                     ].
  23.  
  24.             self removeAll: (sort first value).
  25.  
  26.             i := i + 1.
  27.         ].
  28.  
  29.         ^self anyOne
  30.     ]
  31. ]
  32.  
  33.  
  34. "
  35. | Mainline
  36. "
  37. input := stdin lines contents.
  38.  
  39. oxyCo2 := #(true false) collect: [ :mode | (input asSet filter: mode) asRadix: 2 ].
  40.  
  41. ('Part 2: %1' % {(oxyCo2 at: 1) * (oxyCo2 at: 2)}) displayNl.
RAW Paste Data