Advertisement
musifter

AoC 2024, day 11 (smalltalk)

Dec 11th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 0.96 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend  [ value: arg [^arg perform: self] ]
  4.  
  5. " Extending Booleans to coerce to numbers: "
  6. Boolean extend  [ generality [^1] ]
  7. True  extend    [ truncated  [^1] ]
  8. False extend    [ truncated  [^0] ]
  9.  
  10. "
  11. | Mainline
  12. "
  13. stones := Bag from: (stdin nextLine substrings collect: #asNumber).
  14.  
  15. (1 to: 75) do: [ :time |
  16.     next := Bag new.
  17.  
  18.     stones contents keysAndValuesDo: [ :value :num |
  19.         len := (value floorLog: 10) + 1.    "Note: floorLog returns 0 for value == 0 "
  20.         (len \\ 2 == 0) ifTrue: [
  21.             factor := 10 raisedToInteger: len // 2.
  22.             next add: value // factor  withOccurrences: num.
  23.             next add: value \\ factor  withOccurrences: num.
  24.         ] ifFalse: [
  25.             next add: value * 2024 + (value == 0)  withOccurrences: num.
  26.         ]
  27.     ].
  28.  
  29.     stones := next.
  30.     (time == 25) ifTrue: [('Part 1: %1' % {stones size}) displayNl].
  31. ].
  32.  
  33. ('Part 2: %1' % {stones size}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement