r/javahelp • Out of Coffee error - System halted • Dec 02 '15

[META] Daily coding challenges for the Advent

For those who haven't yet seen it on other subreddits or on Facebook:

Advent of Code

Has daily coding challenges during the Advent season up to the 25th of December.

Each daily challenge is split in two parts of which the second one is only unlocked after successful completion of the first part.

The challenges are relatively small and fun to make.

Have fun coding!


EDIT

In order to prevent spoilers (that would reduce the fun for the others), please do not post code directly here in the thread, post the code on pastebin, gist, github, bitbucket, or the code hoster of your choice and link here only.


Edit 2: Post stickied for the duration of the Advent of Code to make it easier to find.

22 Upvotes

137 comments sorted by

View all comments

2

u/Philboyd_Studge Dec 23 '15 edited Dec 23 '15

Took a break from finishing Day 22 to solve Day 23. This one was pretty fun. Enums combined with lambdas are so powerful. edit: accidentally named the class Advent22 because I was still using Advent21 for the day 22 challenge. Also, for part 2 I just did a quick hack of adding a line to the input file.

I want to note here that most of my solutions use practices I normally would try to avoid, most namely using static everything, using many inner classes and not using private member variables and getters/setters. This is done only in the interest of speed (development speed, not program execution) and overall program size. But if you are a beginner programmer and looking at my code, bear that in mind. Each class should generally be in a separate file and member variables should not be exposed, and the main class should be instanced, and the main() method should contain as little as possible, and static methods and variables should be used as little as possible.

1

u/desrtfx Out of Coffee error - System halted Dec 23 '15

Enums combined with lambdas are so powerful.

I am stunned and in awe :) Didn't even know this was possible.

I knew that Enums are extremely powerful in Java, but this takes it to a whole new level.

(Now you have a new topic for your /r/javaexamples ;) - extended information about Enums)

2

u/Philboyd_Studge Dec 23 '15

Thanks! I used a similar approach for day 7. A neat trick is to name the enum the exact same as the input string, that way you can use valueOf to initialize them without any type of switch structure, which is about Java's only equivalent to the old procedural language runtime macro substitution (FoxPro 2 anybody?).

The only thing that bothered me is there seems to be no way to polymorphize the functional expressions? I. E. I had to use BiFunction even for the commands that only needed one parameter.