solved 2
This commit is contained in:
@@ -13,44 +13,73 @@ public class Problem2 {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
String input = readFileAsString(filePath);
|
String input = readFileAsString(filePath);
|
||||||
List<Long> numbers = parseRanges(input);
|
List<long[]> ranges = parseRanges(input);
|
||||||
|
long tally = solve(ranges);
|
||||||
for (Long num : numbers) {
|
System.out.println("Sum of invalid IDs: " + tally);
|
||||||
System.out.println(num);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Solve by generating only repeated IDs
|
||||||
|
public static long solve(List<long[]> ranges) {
|
||||||
|
long sum = 0;
|
||||||
|
|
||||||
|
for (long[] range : ranges) {
|
||||||
|
long start = range[0];
|
||||||
|
long end = range[1];
|
||||||
|
|
||||||
|
for (long num = start; num <= end; num++) {
|
||||||
|
String numStr = String.valueOf(num);
|
||||||
|
int len = numStr.length();
|
||||||
|
|
||||||
|
if (len % 2 != 0) continue;
|
||||||
|
|
||||||
|
boolean repeated = false;
|
||||||
|
for (int prefixLen = 1; prefixLen <= len / 2; prefixLen++) {
|
||||||
|
if (len % prefixLen != 0) continue; // must divide evenly
|
||||||
|
|
||||||
|
String prefix = numStr.substring(0, prefixLen);
|
||||||
|
String repeatedNum = prefix + prefix;
|
||||||
|
if (repeatedNum.equals(numStr)) {
|
||||||
|
repeated = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (repeated) sum += num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
public static String readFileAsString(String filePath) throws IOException {
|
public static String readFileAsString(String filePath) throws IOException {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
|
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
sb.append(line.trim()); // remove extra spaces/newlines
|
sb.append(line.trim());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Long> parseRanges(String input) {
|
public static List<long[]> parseRanges(String input) {
|
||||||
List<Long> result = new ArrayList<>();
|
List<long[]> result = new ArrayList<>();
|
||||||
String[] ranges = input.split(",");
|
String[] ranges = input.split(",");
|
||||||
|
|
||||||
for (String range : ranges) {
|
for (String range : ranges) {
|
||||||
String[] parts = range.split("-");
|
String[] parts = range.trim().split("-");
|
||||||
if (parts.length != 2) {
|
if (parts.length != 2) {
|
||||||
throw new IllegalArgumentException("Invalid range: " + range);
|
throw new IllegalArgumentException("Invalid range: " + range);
|
||||||
}
|
}
|
||||||
long start = Long.parseLong(parts[0]);
|
long start = Long.parseLong(parts[0]);
|
||||||
long end = Long.parseLong(parts[1]);
|
long end = Long.parseLong(parts[1]);
|
||||||
result.add(start);
|
result.add(new long[]{start, end});
|
||||||
result.add(end);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user