Skip to content

Commit 3c14610

Browse files
day06 for AoC 2023 (part 1)
Took 23 minutes
1 parent 404a9a6 commit 3c14610

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package aminetti.adventofcode2024.day06;
2+
3+
import aminetti.adventofcode2023.day17.Day17;
4+
import org.apache.commons.lang3.tuple.ImmutablePair;
5+
import org.apache.commons.lang3.tuple.Pair;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import java.util.HashSet;
10+
import java.util.List;
11+
import java.util.Set;
12+
13+
import static aminetti.adventofcode2024.day06.Day06.Direction.*;
14+
15+
public class Day06 {
16+
private static final Logger LOGGER = LoggerFactory.getLogger(Day06.class);
17+
private List<String> input;
18+
private int ROWS;
19+
private int COLS;
20+
21+
public Day06() {
22+
}
23+
24+
public void parseInput(List<String> input) {
25+
this.input = input;
26+
ROWS = input.size();
27+
COLS = input.getFirst().length();
28+
}
29+
30+
public long solvePart1() {
31+
Pair<Integer, Integer> start = findStart();
32+
Set<Pair<Integer, Integer>> steps = new HashSet<>();
33+
34+
Pair<Integer, Integer> current = start;
35+
Direction d = UP;
36+
37+
while (inMap(current)) {
38+
steps.add(current);
39+
40+
Pair<Integer, Integer> next = new ImmutablePair<>(current.getLeft() + d.x, current.getRight() + d.y);
41+
42+
if (inMap(next) && input.get(next.getLeft()).charAt(next.getRight()) == '#') {
43+
LOGGER.info("Can't go {}, so rotating 90°", d);
44+
d = Direction.values()[(d.ordinal() + 1) % 4];
45+
next = new ImmutablePair<>(current.getLeft() + d.x, current.getRight() + d.y);
46+
}
47+
48+
current = next;
49+
LOGGER.info("Now position is {}", current);
50+
}
51+
52+
return steps.size();
53+
}
54+
55+
public enum Direction {
56+
RIGHT(0, 1), DOWN(1, 0), LEFT(0, -1), UP(-1, 0);
57+
58+
public final int x;
59+
public final int y;
60+
61+
Direction(int x, int y) {
62+
this.x = x;
63+
this.y = y;
64+
}
65+
}
66+
67+
private boolean inMap(Pair<Integer, Integer> p) {
68+
return 0 <= p.getLeft() && p.getLeft() < ROWS && 0 <= p.getRight() && p.getRight() < COLS;
69+
}
70+
71+
private Pair<Integer, Integer> findStart() {
72+
for (int i = 0; i < ROWS; i++) {
73+
for (int j = 0; j < COLS; j++) {
74+
if (input.get(i).charAt(j) == '^') {
75+
return new ImmutablePair<>(i, j);
76+
}
77+
}
78+
}
79+
throw new IllegalArgumentException("Can't find start");
80+
}
81+
82+
public long solvePart2() {
83+
84+
return 0;
85+
}
86+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package aminetti.adventofcode2024.day06;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
8+
import static java.nio.charset.StandardCharsets.UTF_8;
9+
import static org.apache.commons.io.IOUtils.readLines;
10+
import static org.apache.commons.io.IOUtils.resourceToString;
11+
import static org.hamcrest.MatcherAssert.assertThat;
12+
import static org.hamcrest.Matchers.is;
13+
14+
class Day06Test {
15+
16+
@Test
17+
void actualInputPart1() throws IOException {
18+
// given
19+
List<String> input = readLines(resourceToString("/day06/day06_input.txt", UTF_8));
20+
21+
// when
22+
Day06 solver = new Day06();
23+
solver.parseInput(input);
24+
long l = solver.solvePart1();
25+
26+
// then
27+
assertThat(l, is(0L));
28+
}
29+
30+
@Test
31+
void testInputPart1() throws IOException {
32+
// given
33+
List<String> input = readLines(resourceToString("/day06/day06_input_test.txt", UTF_8));
34+
35+
// when
36+
Day06 solver = new Day06();
37+
solver.parseInput(input);
38+
long l = solver.solvePart1();
39+
40+
// then
41+
assertThat(l, is(41L));
42+
}
43+
44+
@Test
45+
void actualInputPart2() throws IOException {
46+
// given
47+
List<String> input = readLines(resourceToString("/day06/day06_input.txt", UTF_8));
48+
49+
// when
50+
Day06 solver = new Day06();
51+
solver.parseInput(input);
52+
long l = solver.solvePart2();
53+
54+
// then
55+
assertThat(l, is(0L));
56+
}
57+
58+
@Test
59+
void testInputPart2() throws IOException {
60+
// given
61+
List<String> input = readLines(resourceToString("/day06/day06_input_test.txt", UTF_8));
62+
63+
// when
64+
Day06 solver = new Day06();
65+
solver.parseInput(input);
66+
long l = solver.solvePart2();
67+
68+
// then
69+
assertThat(l, is(0L));
70+
}
71+
72+
73+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
....#.....
2+
.........#
3+
..........
4+
..#.......
5+
.......#..
6+
..........
7+
.#..^.....
8+
........#.
9+
#.........
10+
......#...

0 commit comments

Comments
 (0)