• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

[SPOILER ALERT] Advent of Code 2022: Day 3 solutions

 
Sheriff
Posts: 17734
302
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 3 of Advent of Code 2022.






- This space intentionally left blank -







SPOILER ALERT This thread contains solution code for Day 3 of Advent of Code 2022.
 
Junilu Lacar
Sheriff
Posts: 17734
302
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
Here's my Day 3 solution in Kotlin: https://github.com/jlacar/adventofcode2022-kotlin/blob/main/untitled/src/main/kotlin/Day3.kt

I just remembered that Kotlin had a way to chunk streams. Will try that as an alternate solution.

Edit: Yup, significantly reduced lines of code by using chunked(3) - very nice, if I say so myself.
 
Junilu Lacar
Sheriff
Posts: 17734
302
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
Every refactoring leads to another. This goes back to what I was telling someone just the other day: if you refactor for clarity, you begin to see deeper patterns in the code that give you opportunities to refactor further.

I see that my most recent refactoring has revealed a pattern that I can use to tighten the code up to generalize the algorithm.
 
Sheriff
Posts: 8988
652
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
My solutions probably aren't readable for non racketeers, but I'm not sure racket is readable in general.

I shall try moving forward to decompose solution by giving lambdas some meaningful names, so the solution could be told in declarative sentence.
 
Junilu Lacar
Sheriff
Posts: 17734
302
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

Liutauras Vilda wrote:My solutions probably aren't readable for non racketeers, but I'm not sure racket is readable in general.


Well, if we put them side by side...

Racket

Kotlin
Java
I'm just wondering how you came up with 96 and 38. I could imagine defining these constants:

But what names would 96 and 38 get to make them more readable?
 
Junilu Lacar
Sheriff
Posts: 17734
302
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

Junilu Lacar wrote:I could imagine defining these constants:


With those constants, I'd refactor to this:

Edit: I guess not. Apparently, I have to keep it like this:
 
Did you just should on me? You should read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic