Created
December 15, 2025 03:52
-
-
Save jaspermayone/fd85222865dfb823b422663505737ca3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedReader; | |
| import java.io.FileReader; | |
| import java.io.IOException; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| public class RangeMerger { | |
| // Simple class to represent an integer range with inclusive start and end | |
| static class Range { | |
| int start; | |
| int end; | |
| public Range(int start, int end) { | |
| this.start = start; | |
| this.end = end; | |
| } | |
| // Calculate the number of integers in this range (inclusive) | |
| public int size() { | |
| return end - start + 1; | |
| } | |
| } | |
| public static void main(String[] args) { | |
| final String INPUT_PATH = "input.txt"; | |
| List<Range> freshRanges = new ArrayList<>(); | |
| // Regex pattern to match lines in format: "number-number" | |
| // Supports optional whitespace and negative numbers | |
| Pattern rangePattern = Pattern.compile("^\\s*(-?\\d+)\\s*-\\s*(-?\\d+)\\s*$"); | |
| // Read the input file line by line | |
| try (BufferedReader reader = new BufferedReader(new FileReader(INPUT_PATH))) { | |
| String line; | |
| while ((line = reader.readLine()) != null) { | |
| Matcher matcher = rangePattern.matcher(line); | |
| if (matcher.matches()) { | |
| // Parse the two numbers from the matched groups | |
| int a = Integer.parseInt(matcher.group(1)); | |
| int b = Integer.parseInt(matcher.group(2)); | |
| freshRanges.add(new Range(a, b)); | |
| } else { | |
| // Warn about invalid lines | |
| System.err.println("Skipping invalid range line: " + line); | |
| } | |
| } | |
| } catch (IOException e) { | |
| System.err.println("Error reading file: " + e.getMessage()); | |
| return; | |
| } | |
| // Sort ranges by their starting value | |
| freshRanges.sort((r1, r2) -> Integer.compare(r1.start, r2.start)); | |
| // Merge overlapping or adjacent ranges | |
| List<Range> merged = new ArrayList<>(); | |
| for (Range r : freshRanges) { | |
| if (merged.isEmpty() || r.start > merged.get(merged.size() - 1).end + 1) { | |
| // Range is disjoint (doesn't overlap with last range) | |
| // Add it as a new separate range | |
| merged.add(new Range(r.start, r.end)); | |
| } else { | |
| // Range overlaps or is adjacent to the last range | |
| // Extend the last range's end to cover both ranges | |
| Range last = merged.remove(merged.size() - 1); | |
| int newEnd = Math.max(last.end, r.end); | |
| merged.add(new Range(last.start, newEnd)); | |
| } | |
| } | |
| // Calculate total count of unique integers across all merged ranges | |
| int uniqueCount = 0; | |
| for (Range r : merged) { | |
| uniqueCount += r.size(); | |
| } | |
| System.out.println(uniqueCount); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment