|
| 1 | +package Day08; |
| 2 | + |
| 3 | +import advent.utils.AdventFinderUtils; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +public class SevenSegmentParser { |
| 14 | + |
| 15 | + private final List<SevenSegmentDisplay> allDisplays; |
| 16 | + |
| 17 | + public SevenSegmentParser(List<String> inputLines) { |
| 18 | + allDisplays = parseAllDisplays(inputLines); |
| 19 | + } |
| 20 | + |
| 21 | + public int countUniqueValuesInOutput() { |
| 22 | + int uniqueValueCount = 0; |
| 23 | + for (SevenSegmentDisplay display : allDisplays) { |
| 24 | + if (display.outputInDigits.contains(1)) { |
| 25 | + uniqueValueCount += Collections.frequency(display.outputInDigits, 1); |
| 26 | + } |
| 27 | + if (display.outputInDigits.contains(4)) { |
| 28 | + uniqueValueCount += Collections.frequency(display.outputInDigits, 4); |
| 29 | + } |
| 30 | + if (display.outputInDigits.contains(7)) { |
| 31 | + uniqueValueCount += Collections.frequency(display.outputInDigits, 7); |
| 32 | + } |
| 33 | + if (display.outputInDigits.contains(8)) { |
| 34 | + uniqueValueCount += Collections.frequency(display.outputInDigits, 8); |
| 35 | + } |
| 36 | + } |
| 37 | + return uniqueValueCount; |
| 38 | + } |
| 39 | + |
| 40 | + public long sumOutput() { |
| 41 | + long sum = 0; |
| 42 | + for (SevenSegmentDisplay display : allDisplays) { |
| 43 | + int outputNumber = Integer.parseInt(display.outputInDigits.stream() |
| 44 | + .map(String::valueOf) |
| 45 | + .collect(Collectors.joining())); |
| 46 | + sum += outputNumber; |
| 47 | + } |
| 48 | + return sum; |
| 49 | + } |
| 50 | + |
| 51 | + public List<SevenSegmentDisplay> parseAllDisplays(List<String> inputLines) { |
| 52 | + List<SevenSegmentDisplay> allDisplays = parseInputIntoDisplays(inputLines); |
| 53 | + allDisplays.forEach(this::parseDisplay); |
| 54 | + return allDisplays; |
| 55 | + } |
| 56 | + |
| 57 | + public void parseDisplay(SevenSegmentDisplay d) { |
| 58 | + while (d.patternToDigit.size() < 10) { |
| 59 | + for (String pattern : d.signalPatterns) { |
| 60 | + if (d.patternToDigit.containsValue(pattern)) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + switch (pattern.length()) { |
| 64 | + case 2: |
| 65 | + d.patternToDigit.put(1, pattern); |
| 66 | + break; |
| 67 | + case 3: |
| 68 | + d.patternToDigit.put(7, pattern); |
| 69 | + break; |
| 70 | + case 4: |
| 71 | + d.patternToDigit.put(4, pattern); |
| 72 | + break; |
| 73 | + case 7: |
| 74 | + d.patternToDigit.put(8, pattern); |
| 75 | + break; |
| 76 | + case 6: |
| 77 | + if (d.patternToDigit.containsKey(4) && isWithinSegment(pattern, d.patternToDigit.get(4))) { |
| 78 | + d.patternToDigit.put(9, pattern); |
| 79 | + } else if (d.patternToDigit.containsKey(1) && d.patternToDigit.containsKey(9) |
| 80 | + && isWithinSegment(pattern, d.patternToDigit.get(1))) { |
| 81 | + d.patternToDigit.put(0, pattern); |
| 82 | + } else if (d.patternToDigit.containsKey(0) && d.patternToDigit.containsKey(9)) { |
| 83 | + d.patternToDigit.put(6, pattern); |
| 84 | + } else { |
| 85 | + continue; |
| 86 | + } |
| 87 | + break; |
| 88 | + case 5: |
| 89 | + if (d.patternToDigit.containsKey(1) && isWithinSegment(pattern, d.patternToDigit.get(1))) { |
| 90 | + d.patternToDigit.put(3, pattern); |
| 91 | + } else if (d.patternToDigit.containsKey(6) && isWithinSegment(d.patternToDigit.get(6), pattern)) { |
| 92 | + d.patternToDigit.put(5, pattern); |
| 93 | + } else if (d.patternToDigit.containsKey(3) && d.patternToDigit.containsKey(5)) { |
| 94 | + d.patternToDigit.put(2, pattern); |
| 95 | + } else { |
| 96 | + continue; |
| 97 | + } |
| 98 | + default: // dance the crab dance! |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + translateOutput(d); |
| 103 | + } |
| 104 | + |
| 105 | + public List<SevenSegmentDisplay> parseInputIntoDisplays(List<String> inputLines) { |
| 106 | + List<SevenSegmentDisplay> allDisplays = new ArrayList<>(); |
| 107 | + for (String line : inputLines) { |
| 108 | + String[] splitLine = line.trim().split(" \\| "); |
| 109 | + SevenSegmentDisplay sevenSegmentDisplay = new SevenSegmentDisplay(); |
| 110 | + sevenSegmentDisplay.signalPatterns = Arrays.stream(splitLine[0].trim().split("\\s")).collect(Collectors.toList()); |
| 111 | + sevenSegmentDisplay.output = Arrays.stream(splitLine[1].trim().split("\\s")).collect(Collectors.toList()); |
| 112 | + allDisplays.add(sevenSegmentDisplay); |
| 113 | + } |
| 114 | + return allDisplays; |
| 115 | + } |
| 116 | + |
| 117 | + private boolean isWithinSegment(String outerSegment, String innerSegment) { |
| 118 | + List<String> segments = Arrays.stream(innerSegment.split("")).collect(Collectors.toList()); |
| 119 | + return AdventFinderUtils.containsAllPatterns(segments, outerSegment); |
| 120 | + } |
| 121 | + |
| 122 | + private boolean isSameSegment(String outerSegment, String innerSegment) { |
| 123 | + return outerSegment.length() == innerSegment.length() && isWithinSegment(outerSegment, innerSegment); |
| 124 | + } |
| 125 | + |
| 126 | + private void translateOutput(SevenSegmentDisplay display) { |
| 127 | + for (String outputPattern : display.output) { |
| 128 | + Integer outputDigit = display.patternToDigit.entrySet().stream() |
| 129 | + .filter(entry -> isSameSegment(outputPattern, entry.getValue())) |
| 130 | + .map(Map.Entry::getKey) |
| 131 | + .findFirst() |
| 132 | + .get(); |
| 133 | + display.outputInDigits.add(outputDigit); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public static class SevenSegmentDisplay { |
| 138 | + List<String> signalPatterns; |
| 139 | + List<String> output; |
| 140 | + Map<Integer, String> patternToDigit; |
| 141 | + List<Integer> outputInDigits; |
| 142 | + |
| 143 | + public SevenSegmentDisplay() { |
| 144 | + signalPatterns = new ArrayList<>(); |
| 145 | + output = new ArrayList<>(); |
| 146 | + patternToDigit = new HashMap<>(); |
| 147 | + outputInDigits = new ArrayList<>(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public String toString() { |
| 152 | + return "SevenSegmentDisplay{" + "signalPatterns=" + signalPatterns + ", output=" + output + |
| 153 | + ", patternToDigit=" + patternToDigit + '}'; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
0 commit comments