Skip to content

Commit

Permalink
Added 2015 Day 12
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-belellou committed Nov 25, 2024
1 parent 43bcb9a commit b87dc94
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<!-- Dependency versions -->
<commons-codec.version>1.17.1</commons-codec.version>
<gson.version>2.11.0</gson.version>
<commons-lang3.version>3.17.0</commons-lang3.version>
<jgrapht-core.version>1.5.2</jgrapht-core.version>
<spring-context.version>6.2.0</spring-context.version>
Expand Down Expand Up @@ -56,6 +57,11 @@
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/com/belellou/kevin/advent/year2015/Day12.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.belellou.kevin.advent.year2015;

import java.io.BufferedReader;
import java.util.Collection;
import java.util.Map;

import com.google.gson.Gson;
import org.jgrapht.util.ModifiableInteger;

import com.belellou.kevin.advent.generic.AbstractDaySolver;

@SuppressWarnings("unused")
public class Day12 extends AbstractDaySolver<Integer> {

private static final String RED = "red";

public Day12() {
super(Day12.class);
}

private static void readMap(Map<?, ?> map, ModifiableInteger count, boolean stopForRed) {
if (stopForRed && map.containsValue(RED)) {
return;
}

readCollection(map.keySet(), count, stopForRed);
readCollection(map.values(), count, stopForRed);
}

private static void readCollection(Collection<?> collection, ModifiableInteger count, boolean stopForRed) {
for (Object o : collection) {
if (o instanceof Collection<?> objects) {
readCollection(objects, count, stopForRed);
} else if (o instanceof Map<?, ?> map) {
readMap(map, count, stopForRed);
} else {
try {
count.setValue((int) (count.getValue() + Double.parseDouble(o.toString())));
} catch (NumberFormatException e) {
// Do nothing
}
}
}
}

@Override
protected Integer doSolveFirstStar(BufferedReader reader) {
String line = reader.lines().findFirst().orElseThrow();

Map<?, ?> map = new Gson().fromJson(line, Map.class);
ModifiableInteger count = new ModifiableInteger(0);

readMap(map, count, false);

return count.getValue();
}

@Override
public Integer getFirstStarSolution() {
return 111_754;
}

@Override
protected Integer doSolveSecondStar(BufferedReader reader) {
String line = reader.lines().findFirst().orElseThrow();

Map<?, ?> map = new Gson().fromJson(line, Map.class);
ModifiableInteger count = new ModifiableInteger(0);

readMap(map, count, true);

return count.getValue();
}

@Override
public Integer getSecondStarSolution() {
return 65_402;
}
}
Loading

0 comments on commit b87dc94

Please sign in to comment.