Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eb17d1e

Browse files
Solved Day 9 (2015) - All in a Single Night
1 parent ed14b0d commit eb17d1e

File tree

1 file changed

+49
-3
lines changed
  • src/main/java/com/sbaars/adventofcode/year15/days

1 file changed

+49
-3
lines changed
Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,69 @@
11
package com.sbaars.adventofcode.year15.days;
22

33
import com.sbaars.adventofcode.year15.Day2015;
4+
import java.util.*;
5+
import java.util.stream.Collectors;
46

57
public class Day9 extends Day2015 {
8+
private final Map<String, Map<String, Integer>> distances = new HashMap<>();
9+
private final Set<String> cities = new HashSet<>();
10+
611
public Day9() {
712
super(9);
13+
parseInput();
814
}
915

1016
public static void main(String[] args) {
11-
new Day9().printParts();
17+
Day9 day = new Day9();
18+
day.printParts();
19+
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 9, 1);
20+
new com.sbaars.adventofcode.network.Submit().submit(day.part2(), 2015, 9, 2);
21+
}
22+
23+
private void parseInput() {
24+
day().lines().forEach(line -> {
25+
String[] parts = line.split(" = ");
26+
String[] cities = parts[0].split(" to ");
27+
int distance = Integer.parseInt(parts[1]);
28+
addDistance(cities[0], cities[1], distance);
29+
addDistance(cities[1], cities[0], distance);
30+
this.cities.add(cities[0]);
31+
this.cities.add(cities[1]);
32+
});
33+
}
34+
35+
private void addDistance(String from, String to, int distance) {
36+
distances.computeIfAbsent(from, k -> new HashMap<>()).put(to, distance);
1237
}
1338

1439
@Override
1540
public Object part1() {
16-
return "";
41+
return findPath(true);
1742
}
1843

1944
@Override
2045
public Object part2() {
21-
return "";
46+
return findPath(false);
47+
}
48+
49+
private int findPath(boolean shortest) {
50+
return cities.stream()
51+
.mapToInt(start -> findPathFromCity(start, new HashSet<>(List.of(start)), shortest))
52+
.reduce(shortest ? Integer.MAX_VALUE : Integer.MIN_VALUE, shortest ? Math::min : Math::max);
53+
}
54+
55+
private int findPathFromCity(String current, Set<String> visited, boolean shortest) {
56+
if (visited.size() == cities.size()) {
57+
return 0;
58+
}
59+
60+
return distances.get(current).entrySet().stream()
61+
.filter(e -> !visited.contains(e.getKey()))
62+
.mapToInt(e -> {
63+
Set<String> newVisited = new HashSet<>(visited);
64+
newVisited.add(e.getKey());
65+
return e.getValue() + findPathFromCity(e.getKey(), newVisited, shortest);
66+
})
67+
.reduce(shortest ? Integer.MAX_VALUE : Integer.MIN_VALUE, shortest ? Math::min : Math::max);
2268
}
2369
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /