From a03312fcbd7f5c16c1b87e38861e3d9fefccd25f Mon Sep 17 00:00:00 2001 From: TheCK Date: Wed, 4 Dec 2024 13:23:11 +0100 Subject: [PATCH] aoc 2024 day 4 refactor --- .../org/ck/adventofcode/year2024/Day04.java | 96 ++++++------------- 1 file changed, 27 insertions(+), 69 deletions(-) diff --git a/adventofcode/src/main/java/org/ck/adventofcode/year2024/Day04.java b/adventofcode/src/main/java/org/ck/adventofcode/year2024/Day04.java index 0cfc1fc2..0af12574 100644 --- a/adventofcode/src/main/java/org/ck/adventofcode/year2024/Day04.java +++ b/adventofcode/src/main/java/org/ck/adventofcode/year2024/Day04.java @@ -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; @@ -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 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> getPatterns) { final List 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 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; } }