Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- " Some conventient covering extensions: "
- String extend [
- asRadix: rad [ ^Number readFrom: (ReadStream on: self) radix: rad ]
- ]
- Collection extend [
- apply: method [ ^self collect: [:x | x perform: method] ]
- ]
- Array class extend [
- new: size withAllNew: class [ ^(1 to: size) collect: [:x| class new] ]
- ]
- "
- | Mainline
- "
- input := stdin lines contents.
- " Collect bit-characters in an Array (index by position) of Bags "
- counts := input inject: (Array new: 12 withAllNew: Bag) into: [:acc :line |
- acc with: line do: [ :bag :digit |
- bag add: digit
- ]
- ].
- " Get the most common ones for each position, read as a binary digit string "
- gamma := (counts collect: [ :bag |
- bag sortedByCount first value
- ]) asString asRadix: 2.
- " Epsilon is just gamma with bits XOR'd "
- ('Part 1: %1' % {gamma * (gamma bitXor: 16rFFF)}) displayNl.
Add Comment
Please, Sign In to add comment