Just like last year I'm working through Advent of Code challenges. Jus
like last year I'm probably going to fall out in the next day or two :)
Nevertheless, I'm still holding up, and have a question.

The 4th day part 2 quest has a test, semi-quoting: two adjacent matching
digits that are not part of a larger group of matching digits. So 112 is
fine (two 1's) but 111 is not. Also 11122 is fine, we're just looking
for any occurrence of two adjacent matching digits.

I write some clojure in my day job and there's this function
partition-by[1]. With a word like that the check could simply be

    digits [ ] partition-by [ length 2 = ] any?

where digits explodes the number into a vector of its digits.

Alas, I couldn't find such a word and after a lot of looking around
nothing similar. Grouping and clumping and such didn't lead me to any
simple solution, so I just rolled something dirty:

    : -reset ( cnt curr next -- one next ) [ 2drop 1 ] dip ;
    : -inc ( cnt curr next -- cnt+1 curr ) drop [ 1 + ] dip ;
    : -init ( seq -- cnt curr seq ) [ 1 ] dip unclip-slice swap ;
    : has-2? ( seq -- ? ) -init [ 2dup = not [ pick 2 = [ ] [ -reset f ]
if ] [ -inc f ] if ] find [ 2drop 2 = ] dip or ;

I was thinking about writing partition-by myself but I don't like using
locals and I just wasn't able to come up with a simple solution on the
stack.

Thoughts/pointers welcome!

P.S.: if anyone wants to see/compare/review my solutions so far I can
post it somewhere, just let me know.


[1] https://clojuredocs.org/clojure.core/partition-by

-- 
------------
  Peter Nagy
------------


_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to