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 bf7f92d

Browse files
d13p1
1 parent ec6728b commit bf7f92d

File tree

1 file changed

+92
-17
lines changed
  • src/main/java/com/sbaars/adventofcode/year15/days

1 file changed

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

33
import com.sbaars.adventofcode.year15.Day2015;
4+
import java.util.*;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
import java.util.stream.Collectors;
48

59
public class Day13 extends Day2015 {
6-
public Day13() {
7-
super(13);
8-
}
9-
10-
public static void main(String[] args) {
11-
new Day13().printParts();
12-
}
13-
14-
@Override
15-
public Object part1() {
16-
return "";
17-
}
18-
19-
@Override
20-
public Object part2() {
21-
return "";
22-
}
10+
11+
private final Map<String, Map<String, Integer>> happinessMap = new HashMap<>();
12+
private final Set<String> people = new HashSet<>();
13+
14+
public Day13() {
15+
super(13);
16+
parseInput();
17+
}
18+
19+
public static void main(String[] args) {
20+
Day13 day = new Day13();
21+
day.printParts();
22+
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 13, 1);
23+
}
24+
25+
private void parseInput() {
26+
Pattern pattern = Pattern.compile("(\\w+) would (gain|lose) (\\d+) happiness units by sitting next to (\\w+)\\.");
27+
Arrays.stream(day().split("\n")).forEach(line -> {
28+
Matcher matcher = pattern.matcher(line);
29+
if (matcher.find()) {
30+
String person1 = matcher.group(1);
31+
String person2 = matcher.group(4);
32+
int happiness = Integer.parseInt(matcher.group(3));
33+
if (matcher.group(2).equals("lose")) {
34+
happiness = -happiness;
35+
}
36+
37+
happinessMap.computeIfAbsent(person1, k -> new HashMap<>()).put(person2, happiness);
38+
people.add(person1);
39+
people.add(person2);
40+
}
41+
});
42+
}
43+
44+
@Override
45+
public Object part1() {
46+
List<String> peopleList = new ArrayList<>(people);
47+
return findMaxHappiness(peopleList);
48+
}
49+
50+
@Override
51+
public Object part2() {
52+
return 0; // Implement in next part
53+
}
54+
55+
private int findMaxHappiness(List<String> peopleList) {
56+
return generatePermutations(peopleList).stream()
57+
.mapToInt(this::calculateHappiness)
58+
.max()
59+
.orElse(0);
60+
}
61+
62+
private Set<List<String>> generatePermutations(List<String> original) {
63+
if (original.isEmpty()) {
64+
Set<List<String>> result = new HashSet<>();
65+
result.add(new ArrayList<>());
66+
return result;
67+
}
68+
69+
String firstElement = original.get(0);
70+
List<String> rest = original.subList(1, original.size());
71+
Set<List<String>> permutations = generatePermutations(rest);
72+
Set<List<String>> newPermutations = new HashSet<>();
73+
74+
for (List<String> permutation : permutations) {
75+
for (int i = 0; i <= permutation.size(); i++) {
76+
List<String> newPermutation = new ArrayList<>(permutation);
77+
newPermutation.add(i, firstElement);
78+
newPermutations.add(newPermutation);
79+
}
80+
}
81+
return newPermutations;
82+
}
83+
84+
private int calculateHappiness(List<String> arrangement) {
85+
int totalHappiness = 0;
86+
int size = arrangement.size();
87+
88+
for (int i = 0; i < size; i++) {
89+
String current = arrangement.get(i);
90+
String next = arrangement.get((i + 1) % size);
91+
92+
totalHappiness += happinessMap.get(current).get(next);
93+
totalHappiness += happinessMap.get(next).get(current);
94+
}
95+
96+
return totalHappiness;
97+
}
2398
}

0 commit comments

Comments
(0)

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