From 9e46be0f8278abdc3e8dc66a0fda3f242fb2878d Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sun, 24 Dec 2017 12:27:57 +0100 Subject: [PATCH] Implement day 24, in java. --- 2017/README.md | 2 +- 2017/day-24/.gitignore | 1 + 2017/day-24/Makefile | 8 +++ 2017/day-24/solution.java | 105 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 2017/day-24/.gitignore create mode 100644 2017/day-24/Makefile create mode 100644 2017/day-24/solution.java diff --git a/2017/README.md b/2017/README.md index b4a939a..ec2e206 100644 --- a/2017/README.md +++ b/2017/README.md @@ -16,7 +16,7 @@ The current plan, in no particular order: - [x] Go - [Day 13](./day-13/solution.go) - [x] Groovy - [Day 16](./day-16/solution.groovy) - [x] Haskell - [Day 15](./day-15/solution.hs) -- [ ] Java +- [x] Java - [Day 24](./day-24/solution.java) - [x] Julia - [Day 22](./day-22/solution.jl) - [x] Kotlin - [Day 10](./day-10/solution.kt) - [x] Lex - [Day 09](./day-09/solution.l) diff --git a/2017/day-24/.gitignore b/2017/day-24/.gitignore new file mode 100644 index 0000000..6b468b6 --- /dev/null +++ b/2017/day-24/.gitignore @@ -0,0 +1 @@ +*.class diff --git a/2017/day-24/Makefile b/2017/day-24/Makefile new file mode 100644 index 0000000..bf6ebd7 --- /dev/null +++ b/2017/day-24/Makefile @@ -0,0 +1,8 @@ +all: solution.class + java solution + +clean: + $(RM) *.class + +solution.class: solution.java + javac $< diff --git a/2017/day-24/solution.java b/2017/day-24/solution.java new file mode 100644 index 0000000..0e88e54 --- /dev/null +++ b/2017/day-24/solution.java @@ -0,0 +1,105 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class solution { + + private final Map> connections; + + private solution(Map> connections) { + this.connections = connections; + } + + private int part1() { + return getMaxScore(0, new HashSet<>()); + } + + private int getMaxScore(int startingPoint, HashSet used) { + int best = 0; + + for (Connection c : connections.get(startingPoint)) { + if (used.contains(c)) { + continue; + } + + used.add(c); + best = Math.max(best, getMaxScore(c.getOther(startingPoint), used) + c.score()); + used.remove(c); + } + + return best; + } + + private int part2() { + return getMaxLongest(0, new HashSet<>()).getKey(); + } + + private Map.Entry getMaxLongest(int startingPoint, HashSet used) { + int strongest = 0; + int longest = 0; + + for (Connection c : connections.get(startingPoint)) { + if (used.contains(c)) { + continue; + } + + used.add(c); + Map.Entry res = getMaxLongest(c.getOther(startingPoint), used); + used.remove(c); + + if (longest < res.getValue() + 1) { + longest = res.getValue() + 1; + strongest = res.getKey() + c.score(); + } else if (longest == res.getValue() + 1) { + strongest = Math.max(res.getKey() + c.score(), strongest); + } + } + + return new AbstractMap.SimpleEntry<>(strongest, longest); + } + + public static void main(String[] args) throws IOException { + solution s = new solution(readInput()); + + System.out.println(s.part1()); + System.out.println(s.part2()); + } + + private static Map> readInput() throws IOException { + final Map> connections = new HashMap<>(); + + try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) { + in.lines() + .map(s -> s.trim().split("/")) + .map(Connection::new) + .forEach(c -> { + connections.computeIfAbsent(c.getValue(), a -> new ArrayList<>()) + .add(c); + connections.computeIfAbsent(c.getKey(), a -> new ArrayList<>()) + .add(c); + }); + } + + return Collections.unmodifiableMap(connections); + } + + private static class Connection extends AbstractMap.SimpleImmutableEntry { + + Connection(String[] parts) { + this(Integer.valueOf(parts[0]), Integer.valueOf(parts[1])); + } + + Connection(Integer integer, Integer integer2) { + super(Math.min(integer, integer2), Math.max(integer, integer2)); + } + + int getOther(int first) { + return getKey() == first ? getValue() : getKey(); + } + + int score() { + return getKey() + getValue(); + } + } +}