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/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:

private static int sumNumbers(String input) {
	return DIGITS.splitAsStream(input)
			.filter(s -> !s.isEmpty())
			.mapToInt(s -> (int)Double.parseDouble(s))
			.sum();
}

Required change of DIGITS matcher pattern to:

static final Pattern DIGITS = Pattern.compile("[\\[\\{\\]\\}:\\\"a-zA-Z,\\s]");

which is basically the reverse pattern of digits because it acts as a delimiter pattern instead of a matcher pattern.

1

u/TheHorribleTruth Kind of meh Dec 13 '15

Now you even have test methods! You're almost there young padawan, just slap some @Test annotations around it and put in an assert :)


Regarding File I/O: I've never shown mine, because I think it's trivial (and actually very similar to /u/Philboyd_Studge's, there's only that many ways to read a couple of lines from a file, right?). One thing I did though is loading all input from the same (relative) directory, because I'm lazy efficient :)
That way I don't have to deal with a file-name constant, just put it in the directory, follow the naming convention and I'm done. Here's the core of that:

private static final String BASE_PATH = "./input/";

protected List<String> getInputLines() {
	return getInputLines(getResourceNameFromClass());
}

protected List<String> getInputLines(String resource) {
	List<String> list = new ArrayList<>();
	try (BufferedReader reader = new BufferedReader(new InputStreamReader(AdventOfCode.class.getResourceAsStream(resource)))) {
		String line;
		while ((line = reader.readLine()) != null) {
			list.add(line);
		}
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
	return list;
}

private String getResourceNameFromClass() {
	String suffix= this.getClass().getSimpleName().toLowerCase();
	return getResourceName(suffix);
}

private String getResourceName(String suffix) {
	String resourceName = BASE_PATH + suffix;
	return resourceName;
}

If I want I can still add an additional file and call getInputLines(String resource) directly. In my IDE this looks like this.

1

u/Philboyd_Studge Dec 13 '15

I like it, using the class name is pretty nifty.