Skip to content

Commit 370224e

Browse files
committed
brute force dead end
1 parent 0e7c1f6 commit 370224e

File tree

1 file changed

+25
-14
lines changed
  • src/main/java/com/sbaars/adventofcode/year22/days

1 file changed

+25
-14
lines changed

src/main/java/com/sbaars/adventofcode/year22/days/Day16.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ public class Day16 extends Day2022 {
1414

1515
public Day16() {
1616
super(16);
17+
this.example = 1;
18+
this.in = dayStream().map(s -> {
19+
try {
20+
return readString(s, "Valve %s has flow rate=%n; tunnels lead to valves %s", Valve.class);
21+
} catch (IllegalStateException e) {
22+
return readString(s, "Valve %s has flow rate=%n; tunnel leads to valve %s", Valve.class);
23+
}
24+
}).toList();
25+
this.indices = IntStream.range(0, in.size()).boxed().collect(Collectors.toMap(i -> this.in.get(i).name, i -> i));
1726
}
1827

1928
public static void main(String[] args) {
@@ -26,24 +35,24 @@ public static void main(String[] args) {
2635
// d.submitPart2();
2736
}
2837

38+
private final List<Valve> in;
39+
private final Map<String, Integer> indices;
40+
2941
public record Valve(String name, long flow, String others) {}
30-
public record State(Map<String, Long> open, List<String> path, int index) {}
42+
public record State(Map<String, Long> open, int mins, int index) {}
3143

3244
@Override
3345
public Object part1() {
34-
List<Valve> in = dayStream().map(s -> {
35-
try {
36-
return readString(s, "Valve %s has flow rate=%n; tunnels lead to valves %s", Valve.class);
37-
} catch (IllegalStateException e) {
38-
return readString(s, "Valve %s has flow rate=%n; tunnel leads to valve %s", Valve.class);
39-
}
40-
}).toList();
41-
Map<String, Integer> indices = IntStream.range(0, in.size()).boxed().collect(Collectors.toMap(i -> this.in.get(i).name, i -> i));
42-
List<State> states = new ArrayList<>();
43-
return walk(0, 0, new HashMap<>(), new HashSet<>());
46+
return walk(0, 0, new HashMap<>(), new HashMap<>());
4447
}
4548

46-
private long walk(int mins, int index, Map<String, Long> open, Set<String> visited) {
49+
Map<State, Long> visited = new HashMap<>();
50+
51+
private long walk(int mins, int index, Map<String, Long> open, Map<State, Long> visited) {
52+
State st = new State(open, mins, index);
53+
if(visited.containsKey(st)){
54+
return visited.get(st);
55+
}
4756
long flow = open.values().stream().mapToLong(e -> e).sum();
4857
if(mins == 30){
4958
// System.out.println(Arrays.toString(open.keySet().toArray()));
@@ -54,9 +63,11 @@ private long walk(int mins, int index, Map<String, Long> open, Set<String> visit
5463
List<Long> options = new ArrayList<>();
5564
if(v.flow > 0 && !open.containsKey(v.name)) {
5665
open.put(v.name, v.flow);
57-
options.add(walk(mins + 1, index, new HashMap<>(open), new HashSet<>(visited)));
66+
options.add(walk(mins + 1, index, new HashMap<>(open), new HashMap<>(visited)));
5867
}
59-
return flow + LongStream.concat(options.stream().mapToLong(e -> e), Arrays.stream(v.others.split(", ")).mapToLong(s -> walk(mins + 1, indices.get(s), new HashMap<>(open), new HashSet<>(visited)))).max().orElse(0);
68+
long res = flow + LongStream.concat(options.stream().mapToLong(e -> e), Arrays.stream(v.others.split(", ")).mapToLong(s -> walk(mins + 1, indices.get(s), new HashMap<>(open), new HashMap<>(visited)))).max().orElse(0);
69+
visited.put(st, res);
70+
return res;
6071
}
6172

6273
@Override

0 commit comments

Comments
 (0)