On Sun, 30 Dec 2018 at 00:29, Roelof Wobben <[email protected]> wrote:
> Hello,
>
> Still working on AdventOfCode
>
> I'm struggling to see how this can be solved.
>
> Now, a nice string is one with all of the following properties:
>
> - It contains a pair of any two letters that appears at least twice in
> the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but
> not like aaa (aa, but it overlaps).
>
> To exclude overlaps, one approach could be to subtract each candidate pair
from the string
and then check the remainder-string for matches.
I'm not sure, but suspect that as the candidate pair progresses through the
string, only the
trailing remainder-string needs matching since the preceding
remainder-string has already been checked.
So a Pharo 7 Spotter search for 'pairs' finds 28 implementors
where SequenceableCollection>>overlappingPairsCollect:
looks quite close to what is required...
'abcdefg' asArray overlappingPairsCollect: [ :a :b | { a.b } ]. "#(
#($a $b)
#($b $c)
#($c $d)
#($d $e)
#($e $f)
#($f $g))"
Reviewing its implementation...
SequenceableCollection>>overlappingPairsCollect: aBlock
| retval |
retval := self species ofSize: self size - 1.
1 to: self size - 1
do: [:i | retval at: i put: (aBlock value: (self at: i) value: (self at: i
+ 1)) ].
^retval
it can be adapted to provide also a remainder...
SequenceableCollection>>overlappingPairsRemainderCollect: aBlock
| retval |
retval := self species ofSize: self size - 1.
1 to: self size - 1
do: [:i | retval at: i put: (aBlock value: (self at: i) value: (self at: i
+ 1) value: (self allButFirst: i + 1)) ].
^retval
for use like this...
'abcdefg' asArray overlappingPairsRemainderCollect: [ :a :b :rem | {
a.b.rem } ] "#(
#($a $b #($c $d $e $f $g))
#($b $c #($d $e $f $g))
#($c $d #($e $f $g))
#($d $e #($f $g))
#($e $f #($g))
#($f $g #()))"
> - It contains at least one letter which repeats with exactly one
> letter between them, like xyx, abcdefeghi (efe), or even aaa.
>
> You might adapt "overlappingPairsCollect: aBlock"
into "overlappingPairsSkip: n collect: aBlock"
but changing "size -1" and "i + 1"
into "size - n" and "i + n"
HTH
cheers -ben
P.S. If #overlappingPairsSkip:collect: seemed generically useful to push
into Pharo,
then the following would be redefined to reuse it...
SequenceableCollection>>overlappingPairsCollect: aBlock
^self overlappingPairsSkip: 1 collect: aBlock
The extra layer of function call doesn't add much overhead.
On the other hand #overlappingPairsRemainderCollect:
does lots of extra copying through #allButFirst:
so would not be so good for reuse by #overlappingPairsCollect: