r/javahelp • u/desrtfx 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.
5
u/TheHorribleTruth Kind of meh Dec 02 '15
There's also a dedicated subreddit for it: /r/adventofcode/
Although it'd be nice to discuss Java solutions here.
3
u/desrtfx Out of Coffee error - System halted Dec 10 '15
Back in the game.
Here is my Day 10 solution.
Can surely be optimized, but it worked for me.
2
u/desrtfx Out of Coffee error - System halted Dec 11 '15
Day 11
Again, unoptimized.
I should really learn Unit testing... Anybody got a decent tutorial?
2
u/desrtfx Out of Coffee error - System halted Dec 12 '15 edited Dec 12 '15
Day 12
Basically the same as /u/TheHorribleTruth's solution with /u/Philboyd_Studge's FileIO and Google GSON.
Edit: Gist updated to use Java 8 streams for the summation.
New summation method:
Required change of DIGITS matcher pattern to:
which is basically the reverse pattern of digits because it acts as a delimiter pattern instead of a matcher pattern.
2
u/desrtfx Out of Coffee error - System halted Dec 13 '15
Day 13 was a bummer for me. Optimization problems, or travelling salesman problems are absolutely nothing for me.
Code is not worth putting up because it's too clobbered together. Maybe once I've refactored it.
2
u/desrtfx Out of Coffee error - System halted Dec 14 '15
Day 14
Was not all too complicated but still took longer than it should have...
2
u/desrtfx Out of Coffee error - System halted Dec 16 '15
Day 16
Also nothing fancy but works.
2
u/desrtfx Out of Coffee error - System halted Dec 17 '15
Day17
Brute force with bit-mapping. Nothing fancy.
2
u/desrtfx Out of Coffee error - System halted Dec 18 '15 edited Dec 18 '15
Day18
Simplified Conway's Game of life (the original wraps around on the borders) - easy peasy :)
Edit: Updated my gist with a slightly optimized version with simplification via boolean algebra and short-circuiting of the neighbor count - runs considerably faster.
2
u/king_of_the_universe Dec 02 '15
So, did you try Sink Twice by now or not? >:)
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.3
u/Philboyd_Studge Dec 08 '15
Day 8 an easy one, finally figuring out some regex ;)
1
u/desrtfx Out of Coffee error - System halted Dec 03 '15 edited Dec 03 '15
Puzzle for Day 3
Edit: Set version added, based on /u/Philboyd_Studge's implementation
1
u/desrtfx Out of Coffee error - System halted Dec 15 '15 edited Dec 15 '15
Day 15
Very basic, brute force solution, slightly optimized to ignore invalid combinations. Gives a very short run time (Part 1 completed in 0.297s and part 2 completed in 0.291s on an i7 Laptop).
Proper work stood in my way, hence the late solution.
Edit: Out of fun, I tried to copy the code to Ideone and see the runtimes there (2 passes):
The time differences (of summing up the individual times) to the total time are explained by the overhead time to populate the original ingredient list.
1
u/desrtfx Out of Coffee error - System halted Dec 20 '15
Day 20
This one was really easy.
1
u/desrtfx Out of Coffee error - System halted Dec 21 '15
Day 21 brute force solution.
Still, was fun to program :)
1
u/desrtfx Out of Coffee error - System halted Dec 23 '15
Day 23
Standard solution, no advanced magic like /u/Philboyd_Studge has done.
1
u/desrtfx Out of Coffee error - System halted Dec 25 '15
Day 25 this was one of the easiest.
4
u/Philboyd_Studge Dec 02 '15 edited Dec 03 '15
Day 2, part 1:
http://pastebin.com/8V5grLvY
day 1, part 2
4
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 03 '15 edited Dec 09 '15
Cool! My implementations sofar:
No comments whatsoever. This code isn't intended to be read by others ;)
1
u/TheHorribleTruth Kind of meh Dec 02 '15
Nice :)
Solved the first one using a text editor – software engineering at its finest :D
1
u/Philboyd_Studge Dec 02 '15 edited Dec 03 '15
I'm trying to see if there is a simpler Java solution than this:
http://pastebin.com/Q8sR1Txh
edit: removed code
1
u/Philboyd_Studge Dec 03 '15 edited Dec 03 '15
Day 3, part 2
Only oneifstatement, no switch/case.Edit: No
ifstatements! Bitwise logic for the win!!Edit#2: another version switched to awt.Point and HashSet.
1
u/kanczi Dec 03 '15
Hey there! Could anyone help me out and tell me why this code isn't correct for the day3 part1 challenge? http://pastebin.com/ZqKYSt7Z
1
u/Philboyd_Studge Dec 19 '15 edited Dec 19 '15
Well, Day 19 part 1 wasn't hard. Part two, on the other hand, is a bear.
eh, was overthinking it. Don't over think it!!! Day 19
2
u/Philboyd_Studge Dec 20 '15
Day 20 was really easy. I would have been on the leader-board but I forgot I still have like three unfinished challenges.
3
u/Philboyd_Studge Dec 06 '15
Day 6
3
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 06 '15 edited Dec 08 '15
Day 6
Took a more Java 8 based approach than some other Java solutions I saw.
3
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 07 '15 edited Dec 07 '15
Day 7
This one was actually pretty challenging. My "pretty smart" solution that worked for the sample input didn't work at all for the long input. So the solution was a recursive solver.
Edit: I also moved all my solutions to separate classes and moved everything into a public github repo.
3
u/Philboyd_Studge Dec 08 '15
My late, Java solution. Ran the actual firing process in 4ms.
Day 7
I spent literally about 5 hours on this. Part 2 is a super cheesy hack.
3
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 15 '15
Day 15
Figuring out the algorithm for finding the combinations was the toughest part: it's similar to the one that lets you calculate the different amounts of coins you can have add up to a certain change.
3
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 16 '15 edited Dec 16 '15
Day 16 slightly fancy, doesn't work ;)
Edit: Also the Scala version.
Again I am amazed at the power of Scala for these kinds of challenges. It currently still takes me a lot longer to write than the corresponding Java code because there's a ton of stuff I still need to do but again the scala solution is much shorter (32 lines compared to 83 lines of Java). The regex matching power of scala is incredible.
2
u/Philboyd_Studge Dec 05 '15
Day 5 Both parts is not pretty but it works. I suck at REGEX so I didn't even try, this is basic String manipulation.
2
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 05 '15 edited Dec 07 '15
Day 5
2
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 08 '15 edited Dec 08 '15
Day 8
Edit: Also in Scala
2
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 09 '15 edited Dec 09 '15
Day 9
Nice challenge. Classic travelling salesman problem. Fortunately with a set that can be brute-forced because there's only around 40k permutations.
Edit: And a Scala implementation. These kinds of challenges really show the power of Scala. What's really awesome is that you're getting the algo to create all the permutations for free. It's pretty amazing how much shorter the Scala version is.
2
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 10 '15 edited Dec 10 '15
Day 10
Edit: And Scala
2
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 11 '15
Day 11
2
u/Zeroeh Brewing Expert Dec 11 '15 edited Dec 11 '15
Day 11 Rule 1 Rule 2 Rule 3
I used a Filter Intercept Pattern for the rules to simplify the logic for the rules
Test Case Output:
Please Enter a password to try abcdfges
A valid password = abcdggaa
2)Please Enter a password to try abcdefgh
A valid password = abcdffaa
2
u/TheHorribleTruth Kind of meh Dec 12 '15
Day 12
2
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 12 '15
Day 12
The first one is very doable with just regex replaces / split but the second one isn't so I figured I'd just cave and used Jackson.
2
u/TheHorribleTruth Kind of meh Dec 13 '15
Day 13
Bruteforced because I'm really bad at optimization problems :( But, as I said before: the input data is too nice, so we can get away with it. This bruteforce "algorithm" ran task 1 in ~64ms and task 2 in ~256ms.
2
u/Philboyd_Studge Dec 14 '15
Finally got one quickly Day 14
Still have 4 to catch up on.
2
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 14 '15
Day 13
2
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 14 '15
Day 14
2
u/Philboyd_Studge Dec 16 '15
Day 16 Nothing fancy here, but it works.
2
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 16 '15
Really happy with the improvement to the code that prints my solutions
Now uses an incredibly dirty way of figuring out the 'day' of the solution it should print :D
2
u/Philboyd_Studge Dec 17 '15
Day 17 Recursive solution.
2
u/Philboyd_Studge Dec 18 '15
Day 15 Using random permutations.
2
u/Philboyd_Studge Dec 18 '15 edited Dec 18 '15
Day 18 easy one today.
https://imgflip.com/gif/vz1wf visualization of part one
2
u/Philboyd_Studge Dec 21 '15
Day 21 A brute farce!
1
u/Philboyd_Studge Dec 02 '15
Oh, wow, how did I miss there was a second part to both challenges!!! Cool.
1
u/Philboyd_Studge Dec 04 '15 edited Dec 04 '15
I'm ready.... 2.5 hours away...
edit: ok, HOW THE HELL DID PEOPLE SOLVE THIS IN 2 MINUTES. I've used MD5 before, and I still had to do a little googling to get it working. took me 30 minutes to get both.
here's mine can't believe I forgot about string.startsWith
REFACTORED DAY 4
1
u/nutrecht Lead Software Engineer / EU / 20+ YXP Dec 04 '15 edited Dec 07 '15
And Day 4: https://github.com/nielsutrecht/adventofcode/blob/master/src/main/java/com/nibado/projects/advent/Day04.java
If you have used MD5 before it's not hard at all.
1
u/Philboyd_Studge Dec 05 '15 edited Dec 06 '15
[x-post from /r/adventofcode]
For Java users:
edit: Added another method, moved it to Gist.
Here are some simple static utility methods for quickly adding to your code for various challenges. While we have no idea what the future challenges hold, we can assume there will be a lot more large files of test data. There are four methods here:
Which uses java.NIO to load the entire file into one string, like in challenges Day 1, Day 3.
2.
Which uses BufferedReader to get all the lines from the challenge and return them in an ArrayList. i.e. Day 5
3.
This uses Java 8 Functional Expressions, for occasions where you want to perform an action on each line of the file, and don't need the data again. This takes a Function that has a String parameter and an Integer return value, applies that function to each line in the file and sums the result.
Example: Day 2
4.
This gets all lines of the input file, split into string arrays using the given REGEX delimiter.
Example: Day 6
code here
1
u/Philboyd_Studge Dec 13 '15
Damn, I am so far behind, have been too busy, hopefully catching up tomorrow!!!
1
u/Philboyd_Studge Dec 25 '15
Day 25 Pretty easy, but now I have to go back and finish all the unfinished ones.