Skip to content

Commit

Permalink
aoc 2024 day 4 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed Dec 4, 2024
1 parent 1600e5b commit a03312f
Showing 1 changed file with 27 additions and 69 deletions.
96 changes: 27 additions & 69 deletions adventofcode/src/main/java/org/ck/adventofcode/year2024/Day04.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ck.adventofcode.year2024;

import java.util.*;
import java.util.function.IntFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.ck.adventofcode.util.AOCSolution;
Expand All @@ -17,96 +18,53 @@
url = "https://adventofcode.com/2024/day/4#part2",
category = "2024")
public class Day04 extends AOCSolution {
private static final String XMAS = "XMAS";
private static final String SAMX = "SAMX";
private static final String XMAS_OTHERS = "X.{%1$d}M.{%1$d}A.{%1$d}S";
private static final String SAMX_OTHERS = "S.{%1$d}A.{%1$d}M.{%1$d}X";
private static final String X_MAS_PATTERN =
"([MS]).([MS]).{%1$d}A.{%1$d}((?!\\2)[MS]).((?!\\1)[MS])";
"([MS])[^\\n]([MS]).{%1$d}A.{%1$d}((?!\\2)[MS]).((?!\\1)[MS])";

@Override
protected void runPartOne(final Scanner in) {
List<String> grid = new ArrayList<>();

while (in.hasNextLine()) {
grid.add(in.nextLine());
}

long count = 0;

for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid.get(i).length(); ++j) {
if (j < grid.get(i).length() - 3 && "XMAS".equals(grid.get(i).substring(j, j + 4))) {
++count;
}
if (j < grid.get(i).length() - 3 && "SAMX".equals(grid.get(i).substring(j, j + 4))) {
++count;
}
if (i < grid.size() - 3
&& grid.get(i).charAt(j) == 'X'
&& grid.get(i + 1).charAt(j) == 'M'
&& grid.get(i + 2).charAt(j) == 'A'
&& grid.get(i + 3).charAt(j) == 'S') {
++count;
}
if (i < grid.size() - 3
&& grid.get(i).charAt(j) == 'S'
&& grid.get(i + 1).charAt(j) == 'A'
&& grid.get(i + 2).charAt(j) == 'M'
&& grid.get(i + 3).charAt(j) == 'X') {
++count;
}
if (i < grid.size() - 3 && j < grid.get(i).length() - 3) {
if (grid.get(i).charAt(j) == 'X'
&& grid.get(i + 1).charAt(j + 1) == 'M'
&& grid.get(i + 2).charAt(j + 2) == 'A'
&& grid.get(i + 3).charAt(j + 3) == 'S') {
++count;
}
if (grid.get(i).charAt(j) == 'S'
&& grid.get(i + 1).charAt(j + 1) == 'A'
&& grid.get(i + 2).charAt(j + 2) == 'M'
&& grid.get(i + 3).charAt(j + 3) == 'X') {
++count;
}
if (grid.get(i + 3).charAt(j) == 'X'
&& grid.get(i + 2).charAt(j + 1) == 'M'
&& grid.get(i + 1).charAt(j + 2) == 'A'
&& grid.get(i).charAt(j + 3) == 'S') {
++count;
}
if (grid.get(i + 3).charAt(j) == 'S'
&& grid.get(i + 2).charAt(j + 1) == 'A'
&& grid.get(i + 1).charAt(j + 2) == 'M'
&& grid.get(i).charAt(j + 3) == 'X') {
++count;
}
}
}
}

print(count);
run(
in,
l ->
List.of(
Pattern.compile(XMAS),
Pattern.compile(SAMX),
Pattern.compile(XMAS_OTHERS.formatted(l), Pattern.DOTALL),
Pattern.compile(SAMX_OTHERS.formatted(l), Pattern.DOTALL),
Pattern.compile(XMAS_OTHERS.formatted(l + 1), Pattern.DOTALL),
Pattern.compile(SAMX_OTHERS.formatted(l + 1), Pattern.DOTALL),
Pattern.compile(XMAS_OTHERS.formatted(l - 1), Pattern.DOTALL),
Pattern.compile(SAMX_OTHERS.formatted(l - 1), Pattern.DOTALL)));
}

@Override
protected void runPartTwo(final Scanner in) {
run(in);
run(in, l -> List.of(Pattern.compile(X_MAS_PATTERN.formatted(l - 1), Pattern.DOTALL)));
}

private void run(final Scanner in) {
private void run(final Scanner in, IntFunction<List<Pattern>> getPatterns) {
final List<String> grid = new ArrayList<>();

while (in.hasNextLine()) {
grid.add(in.nextLine());
}

final String text = String.join("\n", grid);
final int lineLength = grid.getFirst().length();
final Pattern pattern = Pattern.compile(X_MAS_PATTERN.formatted(lineLength - 2));
final Matcher matcher = pattern.matcher(String.join("", grid));
final List<Pattern> patterns = getPatterns.apply(lineLength);

long count = 0;
int start = 0;
for (Pattern pattern : patterns) {
final Matcher matcher = pattern.matcher(text);

while (matcher.find(start)) {
start = matcher.start() + 1;
int start = 0;
while (matcher.find(start)) {
start = matcher.start() + 1;

if (matcher.start() % lineLength < lineLength - 2) {
++count;
}
}
Expand Down

0 comments on commit a03312f

Please sign in to comment.