Advertisement
musifter

AoC day 22 (pt1), Smalltalk

Dec 22nd, 2020
1,981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. " Read decks "
  4. section := stdin contents tokenize: '\n\n'.
  5.  
  6. deck1 := ((section first)  tokenize: '\n') asOrderedCollection removeFirst; yourself.
  7. deck1 := deck1 collect: [ :v | v asNumber ].
  8.  
  9. deck2 := ((section second) tokenize: '\n') asOrderedCollection removeFirst; yourself.
  10. deck2 := deck2 collect: [ :v | v asNumber ].
  11.  
  12. " Play game "
  13. [(deck1 size > 0) and: [deck2 size > 0]] whileTrue: [
  14.     card1 := deck1 removeFirst.
  15.     card2 := deck2 removeFirst.
  16.  
  17.     (card1 > card2) ifTrue: [
  18.         deck1 addLast: card1; addLast: card2.
  19.     ] ifFalse: [
  20.         deck2 addLast: card2; addLast: card1.
  21.     ].
  22. ].
  23.  
  24. " Tally score "
  25. part1 := 0.
  26. win_deck := (deck1 size > 0) ifTrue: [ deck1 ] ifFalse: [ deck2 ].
  27. win_deck reverse keysAndValuesDo: [ :i :v | part1 := part1 + (i * v) ].
  28.  
  29. stdout nextPutAll: 'Part 1: ', part1 asString; nl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement