Skip to content

Commit f90dc78

Browse files
day04 for AoC 2023 (part 2)
Took 17 minutes
1 parent 309a499 commit f90dc78

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/main/java/aminetti/adventofcode2024/DaySetup.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static String getInput(int year, int day) throws IOException, InterruptedExcepti
7373

7474
HttpRequest req = HttpRequest.newBuilder()
7575
.uri(uri)
76-
.header("User-Agent", "github.com/albertominetti/adventofcode2024")
76+
.header("User-Agent", "github.com/albertominetti/adventofcode2024-java")
7777
.GET().build();
7878

7979
try (HttpClient client = HttpClient.newBuilder()
@@ -87,9 +87,9 @@ static String getInput(int year, int day) throws IOException, InterruptedExcepti
8787

8888
static void prepareCookie() throws IOException {
8989
CookieManager cookieManager = new CookieManager();
90-
String textCookie = IOUtils.resourceToString("/cookie.txt", UTF_8);
90+
String textCookie = IOUtils.resourceToString("/cookies.txt", UTF_8);
9191
if (textCookie == null) {
92-
throw new IllegalArgumentException("Missing cookie file, please check your cookie.txt file in the resource directory.");
92+
throw new IllegalArgumentException("Missing cookie file, please check your cookies.txt file in the resource directory.");
9393
}
9494
Pattern patternForCookie = Pattern.compile("session=([a-f0-9]+)");
9595
Matcher matcher = patternForCookie.matcher(textCookie);
@@ -100,7 +100,7 @@ static void prepareCookie() throws IOException {
100100
cookieManager.getCookieStore()
101101
.add(URI.create("https://adventofcode.com"), sessionCookie);
102102
} else {
103-
throw new IllegalArgumentException("Invalid cookie format, please check your cookie.txt file.");
103+
throw new IllegalArgumentException("Invalid cookie format, please check your cookies.txt file.");
104104
}
105105

106106
CookieHandler.setDefault(cookieManager);

src/main/java/aminetti/adventofcode2024/day04/Day04.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55

6+
import java.util.HashSet;
67
import java.util.List;
8+
import java.util.Set;
79

810
public class Day04 {
911
private static final Logger LOGGER = LoggerFactory.getLogger(Day04.class);
@@ -46,9 +48,9 @@ private long countXmas(int i, int j) {
4648

4749
// N, S, W, O
4850
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);
51+
count += lookingFor("MAS", i + 1, j, +1, 0);
52+
count += lookingFor("MAS", i, j + 1, 0, +1);
53+
count += lookingFor("MAS", i, j - 1, 0, -1);
5254

5355
return count;
5456
}
@@ -69,7 +71,30 @@ private long lookingFor(String search, int i, int j, int dirI, int dirJ) {
6971
}
7072

7173
public long solvePart2() {
74+
long sum = 0;
75+
for (int i = 1; i < input.size() - 1; i++) {
76+
String l = input.get(i);
77+
for (int j = 1; j < l.length() - 1; j++) {
78+
if (checkCrossMas(i, j)) {
79+
sum++;
80+
}
81+
}
82+
}
83+
return sum;
84+
}
7285

73-
return 0;
86+
87+
private boolean checkCrossMas(int i, int j) {
88+
return input.get(i).charAt(j) == 'A'
89+
&& checkMandS(i - 1, j - 1, i + 1, j + 1)
90+
&& checkMandS(i + 1, j - 1, i - 1, j + 1);
7491
}
92+
93+
private boolean checkMandS(int i, int j, int a, int b) {
94+
Set<Character> foundChars = new HashSet<>();
95+
foundChars.add(input.get(i).charAt(j));
96+
foundChars.add(input.get(a).charAt(b));
97+
return foundChars.containsAll(Set.of('M', 'S'));
98+
}
99+
75100
}

0 commit comments

Comments
 (0)