-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43bcb9a
commit b87dc94
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/belellou/kevin/advent/year2015/Day12.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.