--- Begin Message ---
Thanks Ben,
I think you almost give me the answer but I have to do some testing to see how it really works,
Roelof
Op 30-12-2018 om 02:14 schreef Ben Coman:
On Sun, 30 Dec 2018 at 00:29, Roelof Wobben <r.wob...@home.nl> 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
) oraabcdefgaa
(aa
), but not likeaaa
(aa
, but it overlaps).To exclude overlaps, one approach could be to subtract each candidate pair from the stringand then check the remainder-string for matches.I'm not sure, but suspect that as the candidate pair progresses through the string, only thetrailing remainder-string needs matching since the preceding remainder-string has already been checked.
So a Pharo 7 Spotter search for 'pairs' finds 28 implementorswhere 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 - 1do: [: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 - 1do: [: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 evenaaa
.You might adapt "overlappingPairsCollect: aBlock"into "overlappingPairsSkip: n collect: aBlock"but changing "size -1" and "i + 1"into "size - n" and "i + n"HTHcheers -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:
--- End Message ---
Re: [Pharo-users] how to use indexes to copy a substring out of a string
Roelof Wobben via Pharo-users Sun, 30 Dec 2018 01:19:36 -0800
- [Pharo-users] how to use indexes to... Roelof Wobben
- Re: [Pharo-users] how to use i... Hernán Morales Durand via Pharo-users
- Re: [Pharo-users] how to u... Roelof Wobben
- Re: [Pharo-users] how to use i... Ben Coman
- Re: [Pharo-users] how to u... Roelof Wobben via Pharo-users
- Re: [Pharo-users] how to u... Roelof Wobben via Pharo-users
- Re: [Pharo-users] how ... Ben Coman
- Re: [Pharo-users] how to use i... Richard O'Keefe via Pharo-users