This week's book giveaway is in the Open Source Projects forum.
We're giving away four copies of Eclipse Collections Categorically: Level up your programming game and have Donald Raab on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Riding the wayback time sleigh to Advent of Code 2015 - Day 14 solution (SPOILER ALERT!)

 
Sheriff
Posts: 17735
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
Hi folks,

Just thought I'd share. I decided to start prepping for Advent of Code early this year and have gone back to the very first AoC to get some practice. As recent years, I'm doing my solutions in Kotlin.

I'm going to skip ahead to Day 14 here. I was pleasantly surprised to solve Part 1 fairly quickly. After parsing the input and coming up with a formula to calculate the distance traveled by each reindeer, it was just a matter of getting the maximum of all those distances traveled. A one-liner, basically. Ironically, I found out in Part 2 that the formula I had used for the distance calculation was not quite right, even though it had somehow earned me a gold star for a correct answer.

Part 2, where we had to award points to reindeer for every second during the race that they had the lead, was a little trickier. I got the correct answer to my puzzle input with a very imperative-style solution. I guess I haven't quite made the transition to an FP mindset yet. I did have a sense that the imperative-style implementation could be refactored to a more functional style though. After a long, good run at refactoring,

I started with this (the code that earned my 28th gold star) and slowly refactored it to this:



I have to say, I'm quite happy with how it went. The story for Part 2 is composed and layered nicely from high-level abstract, down to more and more detail. Also, I like that the extracted functions are singular in purpose and are easy to understand. At least, that's what I think when I imagine seeing the code for the first time. I'm curious about what others think though. Appreciate any thoughts/reactions.

I also created a branch to keep a history of my refactoring steps: https://github.com/jlacar/aoc2015/blob/refactor-day14-pt2-to-functional/src/main/kotlin/lacar/junilu/Day14.kt
 
Junilu Lacar
Sheriff
Posts: 17735
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
If you know me at all, you'll probably expect that I did a bit of test-driving as I worked through my solutions. Here's the test I used for Day 14:

 
Bartender
Posts: 5669
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't know about AOC in 2015, but here's my day 14: (java, so pretty verbose as always)

Oops: just noticed that the plural of reindeer is also reindeer. Blame the English language!
 
Rancher
Posts: 5195
84
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Oops: just noticed that the plural of reindeer is also reindeer. Blame the English language!


Hah!  I had the same issue, and also considered "reindeers" for a list of Reindeer.  I chose "herd" instead.

My own Kotlin code was pretty similar to what Junilu showed, with slightly fewer extracted methods with helpful names.  And no automated tests    
 
Piet Souris
Bartender
Posts: 5669
214
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A new shorter and easier version in java:
 
Always look on the bright side of life. At least this ad is really tiny:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic