• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

[SPOILER ALERT] Advent of Code 2022: Day 1 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 1 of Advent of Code 2022.






- This space intentionally left blank -






SPOILER ALERT This thread contains solution code for Day 1 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 Kotlin solution. My Main program already takes care of reading the input file and I have scaffolding that allows me to just call the solution, passing in a List<String> that it can then process according to the problem requirements.

From https://github.com/jlacar/adventofcode2022-kotlin/blob/main/untitled/src/main/kotlin/Day1.kt

Maybe there's a more idiomatic way to write this in Kotlin? Would love to see other solutions in other languages.

I should note that I "cheated" with the data files. I added a blank line at the end to trigger the total logic for the last group. Otherwise, I'd be off-by-one group or I'd have to add another statement like line 12 when the loop exits. Now that I think about it, I should probably do that anyway, for correctness.
Content minimized. Click to view
 
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
@Piet I was looking at your Day 1 solution on GitHub.

Regarding this:

Do you need .boxed() on line 49? Doesn't mapToInt() already give you an IntStream?
 
Marshal
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
Solutions for day01 are essentially the same. Probably that's idiomatic thinking of us developers than idiomatic language approach
 
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
Liu, this is the Racket solution, right? A little inscrutable for these Java/Kotlin eyes but it certainly is interesting, if only for its conciseness. Is cons on Line 5 like the Scala cons?
 
Liutauras Vilda
Marshal
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

Junilu Lacar wrote:Liu, this is the Racket solution, right? A little inscrutable for these Java/Kotlin eyes but it certainly is interesting, if only for its conciseness. Is cons on Line 5 like the Scala cons?


Yes, it is Racket.  Indeed, it is significantly different. But you would find it fairly easy to grasp if you'd have touched Clojure to some good extent in the past. The latter is JVM language and undoubtedly much better known in industry. Those languages are similar in a way like Java and Kotlin are.

I saw on some social media (it was Twitter actually) where Uncle Bob (Robert C. Martin) in some past AoC years was solving those in Clojure.

I don't recall myself using cons in Scala in the past, and can't find documentation in intuitive way at the moment (no one can beat javadocs here), so don't want to mislead, but Racket's cons really makes a pair, or in that line 5 case, adds a number to an existing list, which in racket is a pair.

So it may sound weird that list '(1 2 3 4) is a pair, which you can access/slice it in any way you want. i.e.:
(car '(1 2 3 4)) => 1
(cdr '(1 2 3 4)) => '(2 3 4)

or

(first '(1 2 3 4)) => 1
(rest '(1 2 3 4)) => '(2 3 4)

more:
(cadr '(1 2 3 4)) => 2
(caddr '(1 2 3 4)) => 3
(cddr '(1 2 3 4)) => '(3 4)
(cdddr '(1 2 3 4)) => '(4)

And many other ways to achieve above, i.e. using take, drop.

No wonder why Lisp stands for List processing
 
Saloon Keeper
Posts: 5583
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:@Piet I was looking at your Day 1 solution on GitHub.

Regarding this:

Do you need .boxed() on line 49? Doesn't mapToInt() already give you an IntStream?


An IntStream only has the sorted-method, without a Comparator. But now I wonder if when using just 'map' instead of 'mapToInt' will give me a List<Integer> straightaway. Gonna give it a try!

Just tried it:

works fine, even though getSumOfCalories() returns an int. Wonderful, that autoboxing!
 
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:I don't recall myself using cons in Scala in the past, and can't find documentation in intuitive way at the moment (no one can beat javadocs here), so don't want to mislead, but Racket's cons really makes a pair, or in that line 5 case, adds a number to an existing list, which in racket is a pair.

So it may sound weird that list '(1 2 3 4) is a pair, which you can access/slice it in any way you want. i.e.:


Yeah, it's like the Scala cons - it's used for list processing.
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic