Skip to content

Commit 30b68b9

Browse files
day06 for AoC 2023 (part 2)
Took 30 minutes
1 parent 3c14610 commit 30b68b9

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/main/java/aminetti/adventofcode2024/day06/Day06.java

+58-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import aminetti.adventofcode2023.day17.Day17;
44
import org.apache.commons.lang3.tuple.ImmutablePair;
5+
import org.apache.commons.lang3.tuple.ImmutableTriple;
56
import org.apache.commons.lang3.tuple.Pair;
7+
import org.apache.commons.lang3.tuple.Triple;
68
import org.slf4j.Logger;
79
import 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
}

src/test/java/aminetti/adventofcode2024/day06/Day06Test.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.apache.commons.io.IOUtils.readLines;
1010
import static org.apache.commons.io.IOUtils.resourceToString;
1111
import static org.hamcrest.MatcherAssert.assertThat;
12+
import static org.hamcrest.Matchers.greaterThan;
1213
import static org.hamcrest.Matchers.is;
1314

1415
class Day06Test {
@@ -24,7 +25,7 @@ void actualInputPart1() throws IOException {
2425
long l = solver.solvePart1();
2526

2627
// then
27-
assertThat(l, is(0L));
28+
assertThat(l, is(4977L));
2829
}
2930

3031
@Test
@@ -52,7 +53,9 @@ void actualInputPart2() throws IOException {
5253
long l = solver.solvePart2();
5354

5455
// then
55-
assertThat(l, is(0L));
56+
assertThat(l, greaterThan(1650L));
57+
assertThat(l, greaterThan(1651L));
58+
assertThat(l, is(1729L));
5659
}
5760

5861
@Test
@@ -66,7 +69,7 @@ void testInputPart2() throws IOException {
6669
long l = solver.solvePart2();
6770

6871
// then
69-
assertThat(l, is(0L));
72+
assertThat(l, is(6L));
7073
}
7174

7275

0 commit comments

Comments
 (0)