Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Collection extend [ sum [^self inject: 0 into: [:a :b | a + b]] ]
- Object subclass: TowelPatternMatcher [
- | towels memo |
- TowelPatternMatcher class >> new: array [^super new init: array]
- init: array [
- towels := array.
- memo := Dictionary from: {'' -> 1}. " String perfectly matched "
- ^self
- ]
- countPatterns: str [
- | ret |
- ^memo at: str ifAbsentPut: [
- ret := 0.
- towels do: [:patt |
- (str startsWith: patt) ifTrue: [
- ret := ret + (self countPatterns: (str allButFirst: patt size)).
- ]
- ].
- ret
- ]
- ]
- ]
- "
- | Mainline
- "
- sections := stdin contents tokenize: '\n\n'.
- matcher := TowelPatternMatcher new: (sections first substrings: ', ').
- " Adding output to show its working because this is slow: "
- counts := (sections second substrings) collect: [:patt | (matcher countPatterns: patt) displayNl].
- ('Part 1: %1' % {counts count: [:i | i ~= 0]}) displayNl.
- ('Part 2: %1' % {counts sum}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement