Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- " Helper extensions: "
- Symbol extend [ value: arg [^arg perform: self] ]
- Collection extend [ sum [^self inject: 0 into: [:a :b | a + b]] ]
- RegexResults extend [
- first [ ^self at: 1 ]
- second [ ^self at: 2 ]
- ]
- " Extending String to sum mul() instructions: "
- String extend [
- sumMul [
- | res |
- res := 0.
- self allOccurrencesOfRegex: 'mul\((\d{1,3}),(\d{1,3})\)' do: [:mul |
- res := mul first asNumber * mul second asNumber + res
- ].
- ^res
- ]
- ]
- "
- | Mainline
- "
- input := String join: stdin lines contents separatedBy: ' '.
- " Get all do() blocks and run sumMul on them: "
- part2 := ((input allOccurrencesOfRegex: '(^|do\(\)).*?(don''t\(\)|$)') collect: #sumMul) sum.
- ('Part 1: %1' % {input sumMul}) displayNl.
- ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement