|
| 1 | +package fr.nbrouand.aoc2023.day3; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.tuple.Pair; |
| 4 | + |
| 5 | +import java.awt.*; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.concurrent.atomic.AtomicInteger; |
| 10 | +import java.util.regex.Pattern; |
| 11 | + |
| 12 | +public class Engine { |
| 13 | + Pattern patternNumber = Pattern.compile("\\d+"); |
| 14 | + Pattern patterSymbol = Pattern.compile("\\D"); |
| 15 | + List<EngineNumber> numbers = new ArrayList<>(); |
| 16 | + List<EngineSymbol> symbols = new ArrayList<>(); |
| 17 | + |
| 18 | + |
| 19 | + public Engine(String input) { |
| 20 | + AtomicInteger lineIndex = new AtomicInteger(); |
| 21 | + input.lines().forEach(s -> { |
| 22 | + for (int i = 0; i < s.length(); i++) { |
| 23 | + char currentChar = s.charAt(i); |
| 24 | + if (Character.isDigit(currentChar)) { |
| 25 | + StringBuilder number = new StringBuilder(); |
| 26 | + number.append(currentChar); |
| 27 | + List<Point> points = new ArrayList<>(); |
| 28 | + points.add(new Point(i, lineIndex.get())); |
| 29 | + int j = i + 1; |
| 30 | + while (j < s.length() && Character.isDigit(s.charAt(j))) { |
| 31 | + points.add(new Point(j, lineIndex.get())); |
| 32 | + number.append(s.charAt(j)); |
| 33 | + j++; |
| 34 | + } |
| 35 | + numbers.add(new EngineNumber(Integer.parseInt(number.toString()), points)); |
| 36 | + i = j - 1; |
| 37 | + } else { |
| 38 | + if (!String.valueOf(currentChar).equals(".")) { |
| 39 | + symbols.add(new EngineSymbol(String.valueOf(currentChar), new Point(i, lineIndex.get()))); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + lineIndex.getAndIncrement(); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + /* public Engine(String input) { |
| 48 | + AtomicInteger lineIndex = new AtomicInteger(); |
| 49 | + input.lines().forEach(s -> { |
| 50 | + Matcher matcher = patternNumber.matcher(s); |
| 51 | + while (matcher.find()) { |
| 52 | + String group = matcher.group(); |
| 53 | + int number = Integer.parseInt(group); |
| 54 | + int start = s.indexOf(group); |
| 55 | + List<Point> points = new ArrayList<>(); |
| 56 | + for (int i = 0; i < group.length(); i++) { |
| 57 | + points.add(new Point(start + i, lineIndex.get())); |
| 58 | + } |
| 59 | + numbers.add(new EngineNumber(number, points)); |
| 60 | + } |
| 61 | + Matcher matcher2 = patterSymbol.matcher(s); |
| 62 | + while (matcher2.find()) { |
| 63 | + String group = matcher2.group(); |
| 64 | + if (!group.equals(".")) { |
| 65 | + int start = s.indexOf(group); |
| 66 | + List<Point> points = new ArrayList<>(); |
| 67 | + for (int i = 0; i < group.length(); i++) { |
| 68 | + points.add(new Point(start + i, lineIndex.get())); |
| 69 | + } |
| 70 | + symbols.add(new EngineSymbol(group, points)); |
| 71 | + } |
| 72 | + } |
| 73 | + lineIndex.getAndIncrement(); |
| 74 | + }); |
| 75 | + }*/ |
| 76 | + |
| 77 | + public List<Integer> computeAdditiveAllSymbol() { |
| 78 | + List<Integer> result = new ArrayList<>(); |
| 79 | + numbers.forEach(n -> { |
| 80 | + Optional<Integer> adjacent = searchIfAdjacent(n); |
| 81 | + adjacent.ifPresent(result::add); |
| 82 | + }); |
| 83 | + return result; |
| 84 | + } |
| 85 | + |
| 86 | + public List<Integer> computeMultiplicativeOnly() { |
| 87 | + List<Integer> result = new ArrayList<>(); |
| 88 | + symbols.stream().filter(s -> s.symbol.equals("*")).forEach(s -> { |
| 89 | + Optional<Pair<Integer, Integer>> adjacent = searchTwoNumberAdjacent(s); |
| 90 | + adjacent.ifPresent(a -> { |
| 91 | + result.add(a.getLeft() * a.getRight()); |
| 92 | + }); |
| 93 | + }); |
| 94 | + return result; |
| 95 | + } |
| 96 | + |
| 97 | + private Optional<Integer> searchIfAdjacent(EngineNumber n) { |
| 98 | + for (int i = 0; i < n.points.size(); i++) { |
| 99 | + Point pointNumber = n.points.get(i); |
| 100 | + for (EngineSymbol symbol : symbols) { |
| 101 | + Point pointSymbol = symbol.point; |
| 102 | + if (areAdjacent(pointNumber, pointSymbol)) { |
| 103 | + return Optional.of(n.number); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + return Optional.empty(); |
| 108 | + } |
| 109 | + |
| 110 | + private Optional<Pair<Integer, Integer>> searchTwoNumberAdjacent(EngineSymbol s) { |
| 111 | + List<Integer> integersFound = new ArrayList<>(); |
| 112 | + for (EngineNumber number : numbers) { |
| 113 | + for (Point numberPoint : number.points) { |
| 114 | + if (areAdjacent(numberPoint, s.point)) { |
| 115 | + if (integersFound.size() < 2) { |
| 116 | + integersFound.add(number.number); |
| 117 | + break; |
| 118 | + } else { |
| 119 | + return Optional.empty(); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (integersFound.size() == 2) { |
| 126 | + return Optional.of(Pair.of(integersFound.get(0), integersFound.get(1))); |
| 127 | + } else { |
| 128 | + return Optional.empty(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public static boolean areAdjacent(Point p1, Point p2) { |
| 133 | + int xDiff = Math.abs(p1.x - p2.x); |
| 134 | + int yDiff = Math.abs(p1.y - p2.y); |
| 135 | + |
| 136 | + return xDiff <= 1 && yDiff <= 1; |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + public List<EngineNumber> getNumbers() { |
| 141 | + return numbers; |
| 142 | + } |
| 143 | + |
| 144 | + public List<EngineSymbol> getSymbols() { |
| 145 | + return symbols; |
| 146 | + } |
| 147 | +} |
0 commit comments