Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Symbol extend [ value: arg [^arg perform: self] ]
- Collection extend [ min [^self fold: [:a :b | a min: b]] ]
- Interval extend [
- " Get intersection of self and another Interval "
- & other [ ^(self first max: other first) to: (self last min: other last) ]
- ]
- Interval subclass: Mapping [
- | dest |
- Mapping class >> new: str [
- | nums |
- nums := str subStrings collect: #asNumber.
- ^(super from: nums second to: nums second + nums third - 1) init: nums first.
- ]
- init: d [ dest := d. ^self ]
- dest [ ^dest ]
- ]
- "
- | Mainline
- "
- sections := (stdin contents tokenize: '\n\n') collect: #lines.
- seeds := sections first first subStrings allButFirst collect: #asNumber.
- " Read in mappings. ASSUME: sections are in order "
- map := OrderedCollection new.
- sections allButFirst do: [ :lines |
- typeMap := lines allButFirst asOrderedCollection collect: [:str | Mapping new: str].
- typeMap addLast: (Mapping new: '0 0 4294967296'). " add identity map as last default"
- map add: typeMap.
- ].
- locations := seeds collect: [:seed |
- map inject: seed into: [:loc :rules |
- | trans |
- trans := rules detect: [:r | loc between: r first and: r last].
- trans dest + (loc - trans first).
- ]
- ].
- ('Part 1: %1' % {locations min}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement