Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Character extend [
- asDigit [
- ^self value - $0 value
- ]
- ]
- input := OrderedCollection new.
- stdin nextLine do: [ :c | input add: c asDigit ].
- " Build ring out of input "
- cup_ring := Array new: 1_000_000.
- input fold: [ :a :b | cup_ring at: a put: b ].
- cup_ring at: input last put: 10.
- (10 to: 999_999) do: [ :i | cup_ring at: i put: (i + 1) ].
- cup_ring at: 1_000_000 put: input first.
- " Play 10,000,000 turns "
- curr := input first.
- (1 to: 10_000_000) do: [ :turn |
- (turn \\ 10000 = 0) ifTrue: [ stdout nextPutAll: 'Turn: ', turn asString; cr; flush ].
- ptr := curr.
- " Move over next three, marking them as grabbed "
- " Zero counts as grabbed so we skip it while calculating dest "
- grab := Set with: 0.
- (1 to: 3) do: [ :i | ptr := cup_ring at: ptr. grab add: ptr ].
- " Calculate destination for block "
- dest := curr - 1.
- [ grab includes: dest ] whileTrue: [ dest := (dest - 1) \\ 1_000_001 ].
- " Relink ring with block after dest "
- old_curr_ptr := cup_ring at: curr.
- cup_ring at: curr put: (cup_ring at: ptr). " curr points to after block "
- cup_ring at: ptr put: (cup_ring at: dest). " end block to old after dest "
- cup_ring at: dest put: old_curr_ptr. " dest to start of block "
- curr := cup_ring at: curr.
- ].
- 'Part 2: ' display.
- ((cup_ring at: 1) * (cup_ring at: (cup_ring at: 1))) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement