musifter

AoC day 3, part 1 (Smalltalk)

Dec 3rd, 2021 (edited)
405
0
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. Collection extend [
  9.     apply: method  [ ^self collect: [:x | x perform: method] ]
  10. ]
  11.  
  12. Array class extend [
  13.     new: size withAllNew: class [ ^(1 to: size) collect: [:x| class new] ]
  14. ]
  15.  
  16.  
  17. "
  18. | Mainline
  19. "
  20. input := stdin lines contents.
  21.  
  22. " Collect bit-characters in an Array (index by position) of Bags "
  23. counts := input inject: (Array new: 12 withAllNew: Bag) into: [:acc :line |
  24.               acc with: line do: [ :bag :digit |
  25.                   bag add: digit
  26.               ]
  27.           ].
  28.  
  29. " Get the most common ones for each position, read as a binary digit string "
  30. gamma := (counts collect: [ :bag |
  31.              bag sortedByCount first value
  32.          ]) asString asRadix: 2.
  33.  
  34. " Epsilon is just gamma with bits XOR'd "
  35. ('Part 1: %1' % {gamma * (gamma bitXor: 16rFFF)}) displayNl.
Add Comment
Please, Sign In to add comment