Advertisement
musifter

AoC day 15, Smalltalk

Dec 15th, 2020
2,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. init := ((stdin nextLine) subStrings: ',') collect: [:n | n asInteger].
  4.  
  5. table := LookupTable new.
  6.  
  7. (1 to: (init size - 1)) do: [ :t | table at: (init at: t) put: t ].
  8.  
  9. next := init last.
  10. curr := 0.
  11.  
  12. (init size to: 2020) do: [ :t |
  13.     curr := next.
  14.     next := t - (table at: curr ifAbsent: [t]).
  15.     table at: curr put: t.
  16. ].
  17.  
  18. stdout nextPutAll: ('Part 1: ', curr asString); nl.
  19.  
  20. (2021 to: 30000000) do: [ :t |
  21.     (t \\ 10000 == 0) ifTrue: [ stdout nextPutAll: ('Time: ', t asString); cr; flush ].
  22.     curr := next.
  23.     next := t - (table at: curr ifAbsent: [t]).
  24.     table at: curr put: t.
  25. ].
  26.  
  27. stdout nl; nextPutAll: ('Part 2: ', curr asString); nl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement