• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

[SPOILER ALERT] Advent of Code: Day 6 solutions

 
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SPOILER ALERT This thread contains solution code for Day 6 of Advent of Code 2022.







 -- This space intentionally left blank --








SPOILER ALERT This thread contains solution code for Day 6 of Advent of Code 2022.
 
Junilu Lacar
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finished a couple of hours ago but just woke up and realized there was a more OO way to organize the (Kotlin) code:
 
Master Rancher
Posts: 5060
81
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did something pretty similar:  [re-edited to improve names]
 
Junilu Lacar
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key idea for me was s.toSet().size == s.length. Once I saw that, it was just a matter of iterating through the string to get the right substrings.
 
Marshal
Posts: 8962
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Racket doesn't have sliding window function, but I achieved a similar effect with this code:
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The key idea for me was s.toSet().size == s.length


Yup!  I had other ideas about more performant solutions that are O(input.length) rather than O(input.length * markerLength), because I reflexively dislike the latter.  Knowing it's going over the same data repeatedly is annoying to me (Sets of mostly the same data each time) - I want a single pass.  But it's just too easy to code the way we did.

Still, in discussing it now, I felt compelled to code my alternate solution anyway.
 
Junilu Lacar
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just gave me an idea. What do you think about using a mutable set? You remove the last letter of the sequence and then add the new letter that is going to come in, then check if the set size changed. This way, you're not creating a new set every time.

Then again, this could be just an exercise in premature optimization. And the code will probably be less terse.
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I considered a mutable set.  But, how do you know when it's safe to remove an element?  You may know that one "x" has just left your region of interest... but there may be another 'x' in there, in between the two endpoints.  Or more than one.  So, you could use a mutable Map to keep the count of each element.  But I thought using a mutable Map to just keep track of the last position seen would be easier, or at least, no worse.  Either way, you need to keep track of something that tells you whether there's another 'x' within the region of interest, or whether the last one is too far back, or never been seen.
 
Junilu Lacar
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where there's a will, there's a kludge:

It ain't pretty. For me, the optimization isn't worth the price of elegance.
 
Junilu Lacar
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just checked and the kludgey solution I had using mutableSet didn't work for part 2 when I changed the size from 4 to 14.  Turns out it was just by chance that it seemed to work with the data I used to test with a packetSize of 4.
 
Junilu Lacar
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:more performant solutions that are O(input.length) rather than O(input.length * markerLength)


If I remember my Big O correctly, since markerLength is a constant, doesn't that make O(input.length * markerLength) the same as O(input.length) anyway?
 
Junilu Lacar
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems I program best when I'm on the can. I figured out my mistake

It still ain't pretty, but at least this time it works.
 
Saloon Keeper
Posts: 15727
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did it with this one-liner:

Of course, the value of PATTERN is left as an exercise to the reader.
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice!  I imagine you used something like

Though the one for part 2 is... longer.
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:If I remember my Big O correctly, since markerLength is a constant, doesn't that make O(input.length * markerLength) the same as O(input.length) anyway?


Yes, if you treat markerLength as constant.  But they do change the markerLength between part 1 and 2.   The new value isn't that big, really - but in some of their problems, they make part 2 a much more performance-intensive version of part 1, so you never know if one of the values you thought was constant actually turns out to matter quite a bit.  This is a more general issue with big-O notation - people don't always realize (or agree) which parameters might become significant later.  

Note that for Stephan's regex solution, the length of the regex you need is O(markerLength ^ 2).  Still manageable for markerLength=14, but it's growing.

Though I may be missing a way to simplify.
 
Bartender
Posts: 5558
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't use regex much, and if I do I always have to read a tutorial first. Can you explain what that regex means?
 
Piet Souris
Bartender
Posts: 5558
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't bother, found it out
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, too late.  Well, here's a commented version anyway, for posterity:
 
Stephan van Hulst
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the exact regex I used Mike, nice work!

Aye, it's not elegant, but after part 1 I was committed to it.
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic