Advertisement
musifter

AoC day 23 (pt1), Smalltalk

Dec 23rd, 2020
1,707
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: input size.
  14. input fold: [ :a :b | cup_ring at: a put: b ].
  15. cup_ring at: input last put: input first.
  16.  
  17. " Play 100 turns "
  18. curr := input first.
  19. (1 to: 100) do: [ :turn |
  20.     ptr := curr.
  21.  
  22.     " Move over next three, marking them as grabbed "
  23.     " Zero counts as grabbed so we skip it while calculating dest "
  24.     grab := Set with: 0.
  25.     (1 to: 3) do: [ :i | ptr := cup_ring at: ptr.  grab add: ptr ].
  26.  
  27.     " Calculate destination for block "
  28.     dest := curr - 1.
  29.     [ grab includes: dest ] whileTrue: [ dest := (dest - 1) \\ 10 ].
  30.  
  31.     " Relink ring with block after dest "
  32.     old_curr_ptr := cup_ring at: curr.
  33.     cup_ring at: curr put: (cup_ring at: ptr).   " curr points to after block "
  34.     cup_ring at: ptr  put: (cup_ring at: dest).  " end block to old after dest "
  35.     cup_ring at: dest put: old_curr_ptr.         " dest to start of block "
  36.  
  37.     curr := cup_ring at: curr.
  38. ].
  39.  
  40. 'Part 1: ' display.
  41. curr := 1. [ (curr := cup_ring at: curr) ~= 1 ] whileTrue: [ curr display ]. stdout nl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement