Skip to content

Commit

Permalink
Added 2015 Day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-belellou committed Nov 25, 2024
1 parent b87dc94 commit 7642518
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/belellou/kevin/advent/year0/Day0.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected Integer doSolveFirstStar(BufferedReader reader) {
@DisableTest
@Override
public Integer getFirstStarSolution() {
return 0;
return 1;
}

@Override
Expand All @@ -31,6 +31,6 @@ protected Integer doSolveSecondStar(BufferedReader reader) {
@DisableTest
@Override
public Integer getSecondStarSolution() {
return 0;
return 1;
}
}
141 changes: 141 additions & 0 deletions src/main/java/com/belellou/kevin/advent/year2015/Day13.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package com.belellou.kevin.advent.year2015;

import java.io.BufferedReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jgrapht.Graph;
import org.jgrapht.GraphPath;
import org.jgrapht.alg.tour.HeldKarpTSP;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;
import org.jgrapht.graph.SimpleWeightedGraph;

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

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

private static final String GROUP_FIRST_GUEST = "firstGuest";
private static final String GROUP_ACTION = "action";
private static final String GROUP_AMOUNT = "amount";
private static final String GROUP_SECOND_GUEST = "secondGuest";

private static final Pattern PATTERN = Pattern.compile("^" +
"(?<" + GROUP_FIRST_GUEST + ">\\w+)" +
" would " +
"(?<" + GROUP_ACTION + ">\\w+)" +
" (?<" + GROUP_AMOUNT + ">\\d+)" +
" happiness units by sitting next to " +
"(?<" + GROUP_SECOND_GUEST + ">\\w+)." +
"$");

private static final String ACTION_GAIN = "gain";
private static final String ME = "Me";

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

private static void fillGraph(String line, Graph<String, DefaultWeightedEdge> graph) {
Matcher matcher = PATTERN.matcher(line);

if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid line: " + line);
}

String firstGuest = matcher.group(GROUP_FIRST_GUEST);
boolean gainAction = matcher.group(GROUP_ACTION).equals(ACTION_GAIN);
int amount = Integer.parseInt(matcher.group(GROUP_AMOUNT));
String secondGuest = matcher.group(GROUP_SECOND_GUEST);

graph.addVertex(firstGuest);
graph.addVertex(secondGuest);
DefaultWeightedEdge edge = graph.addEdge(firstGuest, secondGuest);
graph.setEdgeWeight(edge, gainAction ? amount : -amount);
}

private static SimpleWeightedGraph<String, DefaultWeightedEdge> transformGraph(
SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> graph) {
SimpleWeightedGraph<String, DefaultWeightedEdge> undirectedGraph = new SimpleWeightedGraph<>(
DefaultWeightedEdge.class);

graph.vertexSet().forEach(undirectedGraph::addVertex);

graph.edgeSet().forEach(edge -> {
double edgeWeight = graph.getEdgeWeight(edge);

String edgeSource = graph.getEdgeSource(edge);
String edgeTarget = graph.getEdgeTarget(edge);

if (undirectedGraph.containsEdge(edgeSource, edgeTarget)) {
return;
}

DefaultWeightedEdge returnEdge = graph.getEdge(edgeTarget, edgeSource);
double returnEdgeWeight = graph.getEdgeWeight(returnEdge);

DefaultWeightedEdge undirectedEdge = undirectedGraph.addEdge(edgeSource, edgeTarget);
undirectedGraph.setEdgeWeight(undirectedEdge, -(edgeWeight + returnEdgeWeight));
});

return undirectedGraph;
}

private static int findOptimalHamiltonianCycleDistance(
SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> directedGraph) {
SimpleWeightedGraph<String, DefaultWeightedEdge> undirectedGraph = transformGraph(directedGraph);

GraphPath<String, DefaultWeightedEdge> tour = new HeldKarpTSP<String, DefaultWeightedEdge>().getTour(
undirectedGraph);

return (int) -tour.getWeight();
}

private static SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> createGraphFromReader(
BufferedReader reader) {
SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> directedGraph = new SimpleDirectedWeightedGraph<>(
DefaultWeightedEdge.class);

reader.lines().forEach(line -> fillGraph(line, directedGraph));
return directedGraph;
}

@Override
protected Integer doSolveFirstStar(BufferedReader reader) {
SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> directedGraph = createGraphFromReader(reader);

return findOptimalHamiltonianCycleDistance(directedGraph);
}

@Override
public Integer getFirstStarSolution() {
return 664;
}

@Override
protected Integer doSolveSecondStar(BufferedReader reader) {
SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> directedGraph = createGraphFromReader(reader);

directedGraph.addVertex(ME);

for (String vertex : directedGraph.vertexSet()) {
if (vertex.equals(ME)) {
continue;
}

DefaultWeightedEdge edge = directedGraph.addEdge(ME, vertex);
directedGraph.setEdgeWeight(edge, 0);

edge = directedGraph.addEdge(vertex, ME);
directedGraph.setEdgeWeight(edge, 0);
}

return findOptimalHamiltonianCycleDistance(directedGraph);
}

@Override
public Integer getSecondStarSolution() {
return 640;
}
}
56 changes: 56 additions & 0 deletions src/main/resources/2015/13/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Alice would lose 2 happiness units by sitting next to Bob.
Alice would lose 62 happiness units by sitting next to Carol.
Alice would gain 65 happiness units by sitting next to David.
Alice would gain 21 happiness units by sitting next to Eric.
Alice would lose 81 happiness units by sitting next to Frank.
Alice would lose 4 happiness units by sitting next to George.
Alice would lose 80 happiness units by sitting next to Mallory.
Bob would gain 93 happiness units by sitting next to Alice.
Bob would gain 19 happiness units by sitting next to Carol.
Bob would gain 5 happiness units by sitting next to David.
Bob would gain 49 happiness units by sitting next to Eric.
Bob would gain 68 happiness units by sitting next to Frank.
Bob would gain 23 happiness units by sitting next to George.
Bob would gain 29 happiness units by sitting next to Mallory.
Carol would lose 54 happiness units by sitting next to Alice.
Carol would lose 70 happiness units by sitting next to Bob.
Carol would lose 37 happiness units by sitting next to David.
Carol would lose 46 happiness units by sitting next to Eric.
Carol would gain 33 happiness units by sitting next to Frank.
Carol would lose 35 happiness units by sitting next to George.
Carol would gain 10 happiness units by sitting next to Mallory.
David would gain 43 happiness units by sitting next to Alice.
David would lose 96 happiness units by sitting next to Bob.
David would lose 53 happiness units by sitting next to Carol.
David would lose 30 happiness units by sitting next to Eric.
David would lose 12 happiness units by sitting next to Frank.
David would gain 75 happiness units by sitting next to George.
David would lose 20 happiness units by sitting next to Mallory.
Eric would gain 8 happiness units by sitting next to Alice.
Eric would lose 89 happiness units by sitting next to Bob.
Eric would lose 69 happiness units by sitting next to Carol.
Eric would lose 34 happiness units by sitting next to David.
Eric would gain 95 happiness units by sitting next to Frank.
Eric would gain 34 happiness units by sitting next to George.
Eric would lose 99 happiness units by sitting next to Mallory.
Frank would lose 97 happiness units by sitting next to Alice.
Frank would gain 6 happiness units by sitting next to Bob.
Frank would lose 9 happiness units by sitting next to Carol.
Frank would gain 56 happiness units by sitting next to David.
Frank would lose 17 happiness units by sitting next to Eric.
Frank would gain 18 happiness units by sitting next to George.
Frank would lose 56 happiness units by sitting next to Mallory.
George would gain 45 happiness units by sitting next to Alice.
George would gain 76 happiness units by sitting next to Bob.
George would gain 63 happiness units by sitting next to Carol.
George would gain 54 happiness units by sitting next to David.
George would gain 54 happiness units by sitting next to Eric.
George would gain 30 happiness units by sitting next to Frank.
George would gain 7 happiness units by sitting next to Mallory.
Mallory would gain 31 happiness units by sitting next to Alice.
Mallory would lose 32 happiness units by sitting next to Bob.
Mallory would gain 95 happiness units by sitting next to Carol.
Mallory would gain 91 happiness units by sitting next to David.
Mallory would lose 66 happiness units by sitting next to Eric.
Mallory would lose 75 happiness units by sitting next to Frank.
Mallory would lose 99 happiness units by sitting next to George.

0 comments on commit 7642518

Please sign in to comment.