Advertisement
musifter

AoC day 23 (pt2), Smalltalk

Dec 23rd, 2020
1,846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. Character extend [
  4.     asDigit [
  5.         ^self value - $0 value
  6.     ]
  7. ]
  8.  
  9. input := OrderedCollection new.
  10. stdin nextLine do: [ :c | input add: c asDigit ].
  11.  
  12. " Build ring out of input "
  13. cup_ring := Array new: 1_000_000.
  14. input fold: [ :a :b | cup_ring at: a put: b ].
  15. cup_ring at: input last put: 10.
  16.  
  17. (10 to: 999_999) do: [ :i | cup_ring at: i put: (i + 1) ].
  18. cup_ring at: 1_000_000 put: input first.
  19.  
  20. " Play 10,000,000 turns "
  21. curr := input first.
  22. (1 to: 10_000_000) do: [ :turn |
  23.     (turn \\ 10000 = 0) ifTrue: [ stdout nextPutAll: 'Turn: ', turn asString; cr; flush ].
  24.     ptr := curr.
  25.  
  26.     " Move over next three, marking them as grabbed "
  27.     " Zero counts as grabbed so we skip it while calculating dest "
  28.     grab := Set with: 0.
  29.     (1 to: 3) do: [ :i | ptr := cup_ring at: ptr.  grab add: ptr ].
  30.  
  31.     " Calculate destination for block "
  32.     dest := curr - 1.
  33.     [ grab includes: dest ] whileTrue: [ dest := (dest - 1) \\ 1_000_001 ].
  34.  
  35.     " Relink ring with block after dest "
  36.     old_curr_ptr := cup_ring at: curr.
  37.     cup_ring at: curr put: (cup_ring at: ptr).   " curr points to after block "
  38.     cup_ring at: ptr  put: (cup_ring at: dest).  " end block to old after dest "
  39.     cup_ring at: dest put: old_curr_ptr.         " dest to start of block "
  40.  
  41.     curr := cup_ring at: curr.
  42. ].
  43.  
  44. 'Part 2: ' display.
  45. ((cup_ring at: 1) * (cup_ring at: (cup_ring at: 1))) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement