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

Show parent comment

1

u/Philboyd_Studge Dec 15 '15

I'm still trying to figure out how you did that! Too late to finish tonight.

1

u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 15 '15 edited Dec 15 '15

I'm still trying to figure out how you did that!

It's a recursive function that creates all combinations, not unlike the solutions to Day 9 and Day 13. Basically what it does is start at the front of the list, in a for-loop iterate from 0 to 100 for that index and then pass that newly created list into the same function but one step deeper. And if we're at the 'end' of the list (so we changes the value at the last index) and the sum adds up to the target we add that list to the possible combinations.

This is a brute force approach: it will generate lists like 100,100,100 and then later find out it can't add to the list. It can be made quite a bit faster by not going down a branch if the total sum of the numbers so far is higher than 100.

Currently it finds 176851 combinations in 8.40 seconds. I'll go and see how much of a difference such a cut off will make.

Edit: Small change brought the runtime down from 8.4 to 5.9 seconds. Neat :D It shows how algorithmic complexity matters: by doing a little more work (calcating the sum in a loop) you can trim a large part of the search space.