Skip to content

Commit 309a499

Browse files
day04 for AoC 2023 (part 1)
Took 22 minutes
1 parent 25de1c7 commit 309a499

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package aminetti.adventofcode2024.day04;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.List;
7+
8+
public class Day04 {
9+
private static final Logger LOGGER = LoggerFactory.getLogger(Day04.class);
10+
private List<String> input;
11+
private int ROWS;
12+
private int COLS;
13+
14+
public Day04() {
15+
}
16+
17+
public void parseInput(List<String> input) {
18+
this.input = input;
19+
20+
ROWS = input.size();
21+
COLS = input.getFirst().length();
22+
}
23+
24+
public long solvePart1() {
25+
long sum = 0;
26+
for (int i = 0; i < input.size(); i++) {
27+
String l = input.get(i);
28+
for (int j = 0; j < l.length(); j++) {
29+
char c = l.charAt(j);
30+
if (c == 'X') {
31+
sum += countXmas(i, j);
32+
}
33+
}
34+
}
35+
return sum;
36+
}
37+
38+
private long countXmas(int i, int j) {
39+
long count = 0;
40+
41+
// NW, SW, SE, NE
42+
count += lookingFor("MAS", i - 1, j - 1, -1, -1);
43+
count += lookingFor("MAS", i + 1, j + 1, +1, +1);
44+
count += lookingFor("MAS", i - 1, j + 1, -1, +1);
45+
count += lookingFor("MAS", i + 1, j - 1, +1, -1);
46+
47+
// N, S, W, O
48+
count += lookingFor("MAS", i - 1, j, -1, 0);
49+
count += lookingFor("MAS", i + 1, j , +1, 0);
50+
count += lookingFor("MAS", i , j + 1, 0, +1);
51+
count += lookingFor("MAS", i , j - 1, 0, -1);
52+
53+
return count;
54+
}
55+
56+
private long lookingFor(String search, int i, int j, int dirI, int dirJ) {
57+
if (search.isEmpty()) {
58+
return 1;
59+
}
60+
if (i < 0 || i >= ROWS || j < 0 || j >= COLS) {
61+
return 0;
62+
}
63+
LOGGER.info("Index {} and {}", i, j);
64+
if (search.charAt(0) == input.get(i).charAt(j)) {
65+
return lookingFor(search.substring(1), i + dirI, j + dirJ, dirI, dirJ);
66+
}
67+
68+
return 0;
69+
}
70+
71+
public long solvePart2() {
72+
73+
return 0;
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package aminetti.adventofcode2024.day04;
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 Day04Test {
15+
16+
@Test
17+
void actualInputPart1() throws IOException {
18+
// given
19+
List<String> input = readLines(resourceToString("/day04/day04_input.txt", UTF_8));
20+
21+
// when
22+
Day04 solver = new Day04();
23+
solver.parseInput(input);
24+
long l = solver.solvePart1();
25+
26+
// then
27+
assertThat(l, is(2599L));
28+
}
29+
30+
@Test
31+
void testInputPart1() throws IOException {
32+
// given
33+
List<String> input = readLines(resourceToString("/day04/day04_input_test.txt", UTF_8));
34+
35+
// when
36+
Day04 solver = new Day04();
37+
solver.parseInput(input);
38+
long l = solver.solvePart1();
39+
40+
// then
41+
assertThat(l, is(18L));
42+
}
43+
44+
@Test
45+
void actualInputPart2() throws IOException {
46+
// given
47+
List<String> input = readLines(resourceToString("/day04/day04_input.txt", UTF_8));
48+
49+
// when
50+
Day04 solver = new Day04();
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("/day04/day04_input_test.txt", UTF_8));
62+
63+
// when
64+
Day04 solver = new Day04();
65+
solver.parseInput(input);
66+
long l = solver.solvePart2();
67+
68+
// then
69+
assertThat(l, is(0L));
70+
}
71+
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
MMMSXXMASM
2+
MSAMXMSMSA
3+
AMXSXMAAMM
4+
MSAMASMSMX
5+
XMASAMXAMM
6+
XXAMMXXAMA
7+
SMSMSASXSS
8+
SAXAMASAAA
9+
MAMMMXMMMM
10+
MXMXAXMASX

0 commit comments

Comments
 (0)