22
33import aminetti .adventofcode2023 .day17 .Day17 ;
44import org .apache .commons .lang3 .tuple .ImmutablePair ;
5+ import org .apache .commons .lang3 .tuple .ImmutableTriple ;
56import org .apache .commons .lang3 .tuple .Pair ;
7+ import org .apache .commons .lang3 .tuple .Triple ;
68import org .slf4j .Logger ;
79import org .slf4j .LoggerFactory ;
810
@@ -17,6 +19,7 @@ public class Day06 {
1719 private List <String > input ;
1820 private int ROWS ;
1921 private int COLS ;
22+ private Pair <Integer , Integer > start ;
2023
2124 public Day06 () {
2225 }
@@ -25,10 +28,15 @@ public void parseInput(List<String> input) {
2528 this .input = input ;
2629 ROWS = input .size ();
2730 COLS = input .getFirst ().length ();
31+ start = findStart ();
2832 }
2933
3034 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 () {
3240 Set <Pair <Integer , Integer >> steps = new HashSet <>();
3341
3442 Pair <Integer , Integer > current = start ;
@@ -39,17 +47,16 @@ public long solvePart1() {
3947
4048 Pair <Integer , Integer > next = new ImmutablePair <>(current .getLeft () + d .x , current .getRight () + d .y );
4149
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 );
4452 d = Direction .values ()[(d .ordinal () + 1 ) % 4 ];
4553 next = new ImmutablePair <>(current .getLeft () + d .x , current .getRight () + d .y );
4654 }
4755
4856 current = next ;
49- LOGGER .info ("Now position is {}" , current );
57+ LOGGER .debug ("Now position is {}" , current );
5058 }
51-
52- return steps .size ();
59+ return steps ;
5360 }
5461
5562 public enum Direction {
@@ -80,7 +87,51 @@ private Pair<Integer, Integer> findStart() {
8087 }
8188
8289 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+ }
83134
84- return 0 ;
135+ return false ;
85136 }
86137}
0 commit comments