musifter

AoC 2025, day 6 (Smalltalk)

Dec 6th, 2025 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.12 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend [
  4.     value: arg               [ ^arg  perform: self ]
  5.     value: arg1 value: arg2  [ ^arg1 perform: self with: arg2 ]
  6. ]
  7.  
  8. Collection extend [ sum  [ ^self inject: 0 into: [:a :b | a + b] ] ]
  9.  
  10. SequenceableCollection extend [
  11.     transpose [
  12.         ^(1 to: self first size) collect: [:col | (self collect: [:row | row at: col])].
  13.     ]
  14. ]
  15.  
  16. "
  17. | Mainline
  18. "
  19. input := stdin lines contents asOrderedCollection.
  20. ops   := input removeLast subStrings collect: #asSymbol.
  21.  
  22. " Part 1 numbers (read horizontal and transpose by numbers in table): "
  23. horz  := (input collect: [:row | row subStrings collect: #asNumber]) transpose.
  24.  
  25. " Part 2 numbers (transpose by col and then split on delimiting spaces (0)): "                      
  26. colNums := input transpose collect: [:charArray |
  27.               (charArray select: #isDigit) asString asNumber
  28.            ].
  29.  
  30. vert  := (ReadStream on: colNums) splitAt: 0.
  31.  
  32. " Calculate values and output: "
  33. #(1 2) with: {horz. vert} do: [:part :table |
  34.     ('Part %1: %2' % {
  35.         part.
  36.         (table with: ops collect: #fold:) sum
  37.     }) displayNl.
  38. ]
  39.  
Advertisement
Add Comment
Please, Sign In to add comment