2
2
3
3
import aminetti .adventofcode2023 .day17 .Day17 ;
4
4
import org .apache .commons .lang3 .tuple .ImmutablePair ;
5
+ import org .apache .commons .lang3 .tuple .ImmutableTriple ;
5
6
import org .apache .commons .lang3 .tuple .Pair ;
7
+ import org .apache .commons .lang3 .tuple .Triple ;
6
8
import org .slf4j .Logger ;
7
9
import org .slf4j .LoggerFactory ;
8
10
@@ -17,6 +19,7 @@ public class Day06 {
17
19
private List <String > input ;
18
20
private int ROWS ;
19
21
private int COLS ;
22
+ private Pair <Integer , Integer > start ;
20
23
21
24
public Day06 () {
22
25
}
@@ -25,10 +28,15 @@ public void parseInput(List<String> input) {
25
28
this .input = input ;
26
29
ROWS = input .size ();
27
30
COLS = input .getFirst ().length ();
31
+ start = findStart ();
28
32
}
29
33
30
34
public long solvePart1 () {
31
- Pair <Integer , Integer > start = findStart ();
35
+ Set <Pair <Integer , Integer >> steps = findSteps ();
36
+ return steps .size ();
37
+ }
38
+
39
+ private Set <Pair <Integer , Integer >> findSteps () {
32
40
Set <Pair <Integer , Integer >> steps = new HashSet <>();
33
41
34
42
Pair <Integer , Integer > current = start ;
@@ -39,17 +47,16 @@ public long solvePart1() {
39
47
40
48
Pair <Integer , Integer > next = new ImmutablePair <>(current .getLeft () + d .x , current .getRight () + d .y );
41
49
42
- if (inMap (next ) && input .get (next .getLeft ()).charAt (next .getRight ()) == '#' ) {
43
- LOGGER .info ("Can't go {}, so rotating 90°" , d );
50
+ while (inMap (next ) && input .get (next .getLeft ()).charAt (next .getRight ()) == '#' ) {
51
+ LOGGER .debug ("Can't go {}, so rotating 90°" , d );
44
52
d = Direction .values ()[(d .ordinal () + 1 ) % 4 ];
45
53
next = new ImmutablePair <>(current .getLeft () + d .x , current .getRight () + d .y );
46
54
}
47
55
48
56
current = next ;
49
- LOGGER .info ("Now position is {}" , current );
57
+ LOGGER .debug ("Now position is {}" , current );
50
58
}
51
-
52
- return steps .size ();
59
+ return steps ;
53
60
}
54
61
55
62
public enum Direction {
@@ -80,7 +87,51 @@ private Pair<Integer, Integer> findStart() {
80
87
}
81
88
82
89
public long solvePart2 () {
90
+ Set <Pair <Integer , Integer >> obstructionsForLoops = new HashSet <>();
91
+ for (Pair <Integer , Integer > step : findSteps ()) {
92
+ Pair <Integer , Integer > o = step ;
93
+
94
+ if (o .equals (start )) {
95
+ continue ;
96
+ }
97
+ LOGGER .info ("Obstruction at {}" , o );
98
+
99
+ if (isLoop (start , o )) {
100
+ obstructionsForLoops .add (o );
101
+ }
102
+
103
+ }
104
+
105
+ return obstructionsForLoops .size ();
106
+ }
107
+
108
+ private boolean isLoop (Pair <Integer , Integer > start , Pair <Integer , Integer > o ) {
109
+ Set <Triple <Integer , Integer , Direction >> steps = new HashSet <>();
110
+
111
+ Pair <Integer , Integer > current = start ;
112
+ Direction d = UP ;
113
+
114
+ while (inMap (current )) {
115
+ ImmutableTriple <Integer , Integer , Direction > posAndDir = new ImmutableTriple <>(current .getLeft (), current .getRight (), d );
116
+ if (steps .contains (posAndDir )) {
117
+ return true ;
118
+ }
119
+ steps .add (posAndDir );
120
+
121
+ Pair <Integer , Integer > next = new ImmutablePair <>(current .getLeft () + d .x , current .getRight () + d .y );
122
+
123
+ while (inMap (next ) && (
124
+ input .get (next .getLeft ()).charAt (next .getRight ()) == '#' || next .equals (o )
125
+ )) {
126
+ LOGGER .debug ("Can't go {}, so rotating 90°" , d );
127
+ d = Direction .values ()[(d .ordinal () + 1 ) % 4 ];
128
+ next = new ImmutablePair <>(current .getLeft () + d .x , current .getRight () + d .y );
129
+ }
130
+
131
+ current = next ;
132
+ LOGGER .debug ("Now position is {}" , current );
133
+ }
83
134
84
- return 0 ;
135
+ return false ;
85
136
}
86
137
}
0 commit comments