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

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/desrtfx Out of Coffee error - System halted Dec 02 '15

Probably some form of Java 8 lambda in conjunction with Streams?

4

u/Philboyd_Studge Dec 02 '15 edited Dec 03 '15

1

u/SikhGamer Dec 02 '15

Not a huge fan of the streams stuff in Java 8, mostly because I find it unreadable. But you can remove some of the brackets:

int floor = input.chars().map(x -> x == '(' ? 1 : -1).sum();

To make it even smaller.

1

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

Nice. I did it with a simple for loop but that one is definitely cleaner.

1

u/[deleted] Dec 02 '15 edited Dec 02 '15

With regexes. I tried streams, but they suck for primitives that aren't int, long, or double

public class Advent1 {
    public static void main(String[] args) {
        String input = "()()(()()()(()()((("; // etc.
        String lParens = input.replaceAll("[^(]", "").length();
        String rParens = input.replaceAll("[^)]", "").length();
        System.out.println(lParens - rParens);
    }
}

Edit: nevermind, I didn't know about the chars() method. Damn that's much easier