Advertisement
musifter

AoC 2021 day 21 part 1 (smalltalk)

Dec 21st, 2021 (edited)
1,972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. Integer extend [
  4.     %% modulus [ ^self - 1 \\ modulus + 1 ]
  5. ]
  6.  
  7. Collection extend [
  8.     at: idx modInc: val [ ^self at: idx put: ((self at: idx) + val %% 10) ]
  9.     at: idx inc: val    [ ^self at: idx put: ((self at: idx) + val) ]
  10.  
  11.     sum [ ^self inject: 0 into: [:a : b | a + b]    ]
  12. ]
  13.  
  14. Object subclass: Die100 [
  15.     | rolls |
  16.     Die100 class >> new  [ ^super new init ]
  17.     init                 [ rolls := 0.  ^self ]
  18.  
  19.     stream  [ ^Generator on: [:gen | [gen yield: (rolls := rolls + 1) %% 100] repeat] ]
  20.     rolls   [ ^rolls ]
  21. ]
  22.  
  23. "
  24. | Mainline
  25. "
  26. pawns  := stdin lines contents collect: [:line | (line tokenize: ': ') second asNumber].
  27. score  := Array new: 2 withAll: 0.
  28. player := 2.
  29.  
  30. die  := (Die100 new).
  31. roll := die stream.
  32. [
  33.     " switch player "
  34.     player := 3 - player.
  35.  
  36.     " move token around track the sum of three dice rolls "
  37.     pawns at: player modInc: (roll next: 3) sum.
  38.  
  39.     " score where token lands, check for win "
  40.     (score at: player inc: (pawns at: player)) >= 1000
  41. ] whileFalse.
  42.  
  43. ('Part 1: %1' % {(score at: 3 - player) * die rolls}) displayNl.
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement