Last active
August 29, 2015 13:57
-
-
Save raphw/9662636 to your computer and use it in GitHub Desktop.
Solution to Reaktor's pyramid challenge: http://reaktor.fi/careers/fast_track/
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
import org.junit.Test; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import java.util.stream.Collectors; | |
public class Pyramid { | |
private static class Node { | |
private final int index; | |
private final int weight; | |
private final Optional<Node> child; | |
private Node(int index, int weight) { | |
this.index = index; | |
this.weight = weight; | |
child = Optional.empty(); | |
} | |
private Node(int index, int weight, Node child) { | |
this.index = index; | |
this.weight = weight; | |
this.child = Optional.of(child); | |
} | |
@Override | |
public String toString() { | |
StringBuilder stringBuilder = new StringBuilder("Path: "); | |
Optional<Node> current = Optional.of(this); | |
int line = 1; | |
while (current.isPresent()) { | |
Node node = current.get(); | |
stringBuilder.append(node.index) | |
.append(" [line=") | |
.append(line++) | |
.append(",weight=") | |
.append(node.weight) | |
.append("]") | |
.append(" -> "); | |
current = node.child; | |
} | |
stringBuilder.append("{}"); | |
return stringBuilder.toString(); | |
} | |
} | |
public static Node findPath(List<String> lines) throws Exception { | |
AtomicInteger index = new AtomicInteger(0); | |
List<Node> nodes = Arrays.stream(lines.get(lines.size() - 1).split(" ")) | |
.map(Integer::parseInt) | |
.map(weight -> new Node(index.getAndIncrement(), weight)) | |
.collect(Collectors.toList()); | |
for (int line = lines.size() - 2; line >= 0; line--) { | |
AtomicInteger parentIndex = new AtomicInteger(0); | |
List<Node> lineBefore = nodes; | |
nodes = Arrays.stream(lines.get(line).split(" ")).mapToInt(Integer::parseInt).mapToObj(weight -> { | |
Node left = lineBefore.get(parentIndex.get()), right = lineBefore.get(parentIndex.get() + 1); | |
Node best = left.weight > right.weight ? left : right; | |
return new Node(parentIndex.getAndIncrement(), best.weight + weight, best); | |
}).collect(Collectors.toList()); | |
} | |
/*Node node = nodes.get(0); | |
for (String line : lines) { | |
Node currentNode = node; | |
AtomicInteger lineIndex = new AtomicInteger(); | |
System.out.println(Arrays.stream(line.split(" ")) | |
.map(s -> String.format("%4s", lineIndex.getAndIncrement() == currentNode.index ? "[" + s + "]" : s)) | |
.collect(Collectors.joining(", "))); | |
node = node.child.orElseGet(() -> null); | |
}*/ | |
return nodes.get(0); | |
} | |
@Test | |
public void testAlgorithm() throws Exception { | |
List<String> lines = Files.readAllLines(Paths.get(getClass().getResource("/pyramid.txt").toURI()), | |
StandardCharsets.UTF_8); | |
lines.remove(0); // Remove line containing seed | |
System.out.println(findPath(lines)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment