Advertisement
musifter

AoC 2024, day 3 (smalltalk)

Dec 3rd, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 0.85 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. " Helper extensions: "
  4. Symbol     extend [ value: arg  [^arg perform: self] ]
  5. Collection extend [ sum [^self inject: 0 into: [:a :b | a + b]] ]
  6.  
  7. RegexResults extend [
  8.     first   [ ^self at: 1 ]
  9.     second  [ ^self at: 2 ]
  10. ]
  11.  
  12. " Extending String to sum mul() instructions: "
  13. String extend [
  14.     sumMul [
  15.         | res |
  16.         res := 0.
  17.         self allOccurrencesOfRegex: 'mul\((\d{1,3}),(\d{1,3})\)' do: [:mul |
  18.             res := mul first asNumber * mul second asNumber + res
  19.         ].
  20.         ^res
  21.     ]
  22. ]
  23.  
  24. "
  25. | Mainline
  26. "
  27. input := String join: stdin lines contents separatedBy: ' '.
  28.  
  29. " Get all do() blocks and run sumMul on them: "
  30. part2 := ((input allOccurrencesOfRegex: '(^|do\(\)).*?(don''t\(\)|$)') collect: #sumMul) sum.
  31.  
  32. ('Part 1: %1' % {input sumMul}) displayNl.
  33. ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement