Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- " Read input file "
- inStream := stdin lines contents readStream.
- now := inStream next asNumber.
- table := (inStream next subStrings: ',')
- collect: [ :a | (a ~= 'x') ifTrue: [ a asNumber ]
- ifFalse: [ a ] ].
- " Part 1 "
- " Build arrival table of associations (bus id -> arrival time), take first. "
- buses := table select: [ :a | a ~= 'x' ].
- arrive := buses collect: [ :b | (b -> (b - (now \\ b))) ].
- next := (arrive sorted: [ :a :b | (a value < b value) ]) first.
- stdout nextPutAll: 'Part 1: ', (next key * next value) asString; nl.
- " Part 2 "
- " Get system of modular equations, as associations (mod -> targ) "
- equat := SortedCollection sortBlock: [ :a :b | a key > b key ].
- table doWithIndex: [ :b : i |
- (b ~= 'x') ifTrue: [ equat add: (b -> ((b - i + 1) \\ b)) ].
- ].
- " Find solution to system of equations with sieve: "
- part2 := equat fold: [ :a :b |
- [ (a value \\ b key) ~= b value ] whileTrue: [
- a value: (a value + a key).
- ].
- a key: (a key * b key).
- ].
- stdout nextPutAll: 'Part 2: ', part2 value asString; nl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement