this time with no legal info
This commit is contained in:
34
2/Ex1-redo.py
Normal file
34
2/Ex1-redo.py
Normal file
@@ -0,0 +1,34 @@
|
||||
ranQ = []
|
||||
|
||||
count = 0
|
||||
|
||||
with open("input.txt") as file:
|
||||
input = file.read().strip()
|
||||
|
||||
ranges = input.split(",")
|
||||
|
||||
for ran in ranges:
|
||||
start, end = ran.split("-")
|
||||
if len(start) == len(end):
|
||||
if len(start) % 2 == 0:
|
||||
ranQ.append((int(start), int(end)))
|
||||
else:
|
||||
if len(start) % 2 == 0:
|
||||
ranQ.append((int(start), 10**len(start)- 1))
|
||||
elif len(end) % 2 == 0:
|
||||
ranQ.append((10**len(start), int(end)))
|
||||
|
||||
|
||||
for start, end in ranQ:
|
||||
done = []
|
||||
print(f"Processing range: {start} to {end}")
|
||||
start_prefix = str(start)[:len(str(start))//2]
|
||||
end_prefix = str(end)[:len(str(end))//2]
|
||||
for prefix in range(int(start_prefix), int(end_prefix) + 1):
|
||||
candidate = int(str(prefix) * 2)
|
||||
if start <= candidate <= end and candidate not in done:
|
||||
print(f"Adding: {candidate}")
|
||||
count += int(candidate)
|
||||
done.append(candidate)
|
||||
|
||||
print(count)
|
||||
129
2/Ex1.java
Normal file
129
2/Ex1.java
Normal file
@@ -0,0 +1,129 @@
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Ex1 {
|
||||
public static void main(String[] args) {
|
||||
File file = new File("input.txt");
|
||||
String line = null;
|
||||
long count = 0;
|
||||
try (Scanner scanner = new Scanner(file)) {
|
||||
while (scanner.hasNextLine()) {
|
||||
line = scanner.nextLine();
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
System.err.println("File not found: " + e.getMessage());
|
||||
}
|
||||
String[] ranges = line.split(",");
|
||||
for (String range : ranges) {
|
||||
System.out.println("Processing range: " + range);
|
||||
String[] bounds = range.split("-");
|
||||
// Case 1: bounds same size and odd length
|
||||
if (bounds[0].length() == bounds[1].length() && bounds[0].length() % 2 == 1)
|
||||
continue;
|
||||
// Case 2: bounds same size and even length
|
||||
else if (bounds[0].length() == bounds[1].length() && bounds[0].length() % 2 == 0) {
|
||||
// split each in half, iterate first half of first bound to first half of second bound as longs; get other halves too
|
||||
long start = Long.parseLong(bounds[0].substring(0, bounds[0].length() / 2));
|
||||
long end = Long.parseLong(bounds[1].substring(0, bounds[1].length() / 2));
|
||||
long startLow = Long.parseLong(bounds[0].substring(bounds[0].length() / 2));
|
||||
long endLow = Long.parseLong(bounds[1].substring(bounds[1].length() / 2));
|
||||
for (long i = start; i <= end; i++) {
|
||||
|
||||
if (start == end) { // you little prick.
|
||||
if (i >= startLow && i <= endLow) {
|
||||
System.out.println("Adding: " + String.valueOf(i) + String.valueOf(i));
|
||||
count += Long.parseLong(String.valueOf(i) + String.valueOf(i));
|
||||
}
|
||||
|
||||
} else if (i == start) {
|
||||
if (i >= startLow) {
|
||||
System.out.println("Adding: " + String.valueOf(i) + String.valueOf(i));
|
||||
count += Long.parseLong(String.valueOf(i) + String.valueOf(i));
|
||||
}
|
||||
} else if (i == end) {
|
||||
if (i <= endLow) {
|
||||
System.out.println("Adding: " + String.valueOf(i) + String.valueOf(i));
|
||||
count += Long.parseLong(String.valueOf(i) + String.valueOf(i));
|
||||
}
|
||||
} else {
|
||||
System.out.println("Adding: " + String.valueOf(i) + String.valueOf(i));
|
||||
count += Long.parseLong(String.valueOf(i) + String.valueOf(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Case 3: bounds different sizes
|
||||
else {
|
||||
// Case 3a: first odd, second even
|
||||
if (bounds[0].length() % 2 == 1 && bounds[1].length() % 2 == 0) {
|
||||
long startNum = (long) Math.pow(10, bounds[1].length() - 1);
|
||||
long startHigh = Long.parseLong(String.valueOf(startNum).substring(0, bounds[1].length() / 2));
|
||||
long endHigh = Long.parseLong(bounds[1].substring(0, bounds[1].length() / 2));
|
||||
long startLow = 0;
|
||||
long endLow = Long.parseLong(bounds[1].substring(bounds[1].length() / 2));
|
||||
|
||||
for (long i = startHigh; i <= endHigh; i++) {
|
||||
|
||||
if (startHigh == endHigh) {
|
||||
if (i >= startLow && i <= endLow) {
|
||||
System.out.println("Adding: " + String.valueOf(i) + String.valueOf(i));
|
||||
count += Long.parseLong(String.valueOf(i) + String.valueOf(i));
|
||||
}
|
||||
}
|
||||
|
||||
else if (i == startHigh) {
|
||||
if (i >= startLow) {
|
||||
System.out.println("Adding: " + String.valueOf(i) + String.valueOf(i));
|
||||
count += Long.parseLong(String.valueOf(i) + String.valueOf(i));
|
||||
}
|
||||
} else if (i == endHigh) {
|
||||
if (i <= endLow) {
|
||||
System.out.println("Adding: " + String.valueOf(i) + String.valueOf(i));
|
||||
count += Long.parseLong(String.valueOf(i) + String.valueOf(i));
|
||||
}
|
||||
} else {
|
||||
System.out.println("Adding: " + String.valueOf(i) + String.valueOf(i));
|
||||
count += Long.parseLong(String.valueOf(i) + String.valueOf(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Case 3b: first even, second odd
|
||||
else if (bounds[0].length() % 2 == 0 && bounds[1].length() % 2 == 1) {
|
||||
// here endnum should be highest number with same length as bound0 p.e. 1200-15000 -> endnum = 9999
|
||||
long endNum = (long) Math.pow(10, bounds[1].length() - 1) - 1;
|
||||
long startHigh = Long.parseLong(bounds[0].substring(0, bounds[0].length() / 2));
|
||||
long endHigh = Long.parseLong(String.valueOf(endNum).substring(0, bounds[0].length() / 2));
|
||||
long startLow = Long.parseLong(bounds[0].substring(bounds[0].length() / 2));
|
||||
long endLow = Long.parseLong(String.valueOf(endNum).substring(bounds[0].length() / 2));
|
||||
|
||||
for (long i = startHigh; i <= endHigh; i++) {
|
||||
|
||||
if (startHigh == endHigh) {
|
||||
if (i >= startLow && i <= endLow) {
|
||||
System.out.println("Adding: " + String.valueOf(i) + String.valueOf(i));
|
||||
count += Long.parseLong(String.valueOf(i) + String.valueOf(i));
|
||||
}
|
||||
}
|
||||
|
||||
else if (i == startHigh) {
|
||||
if (i >= startLow) {
|
||||
System.out.println("Adding: " + String.valueOf(i) + String.valueOf(i));
|
||||
count += Long.parseLong(String.valueOf(i) + String.valueOf(i));
|
||||
}
|
||||
} else if (i == endHigh) {
|
||||
if (i <= endLow) {
|
||||
System.out.println("Adding: " + String.valueOf(i) + String.valueOf(i));
|
||||
count += Long.parseLong(String.valueOf(i) + String.valueOf(i));
|
||||
}
|
||||
} else {
|
||||
System.out.println("Adding: " + String.valueOf(i) + String.valueOf(i));
|
||||
count += Long.parseLong(String.valueOf(i) + String.valueOf(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Count: " + count);
|
||||
}
|
||||
}
|
||||
37
2/Ex2.py
Normal file
37
2/Ex2.py
Normal file
@@ -0,0 +1,37 @@
|
||||
ranQ = []
|
||||
|
||||
count = 0
|
||||
|
||||
with open("input.txt") as file:
|
||||
input = file.read().strip()
|
||||
|
||||
ranges = input.split(",")
|
||||
|
||||
for ran in ranges:
|
||||
start, end = ran.split("-")
|
||||
if len(start) == len(end):
|
||||
ranQ.append((int(start), int(end)))
|
||||
else:
|
||||
if len(start) == 1:
|
||||
ranQ.append((10, int(end)))
|
||||
else:
|
||||
ranQ.append((int(start), 10**len(start)- 1))
|
||||
ranQ.append((10**len(start), int(end)))
|
||||
|
||||
for start, end in ranQ:
|
||||
done = []
|
||||
print(f"Processing range: {start} to {end}")
|
||||
for i in range(1, len(str(start)) // 2 + 1):
|
||||
if len(str(start)) % i != 0:
|
||||
continue
|
||||
start_prefix = str(start)[:i]
|
||||
end_prefix = str(end)[:i]
|
||||
num_repeats = len(str(start)) // i
|
||||
for prefix in range(int(start_prefix), int(end_prefix) + 1):
|
||||
candidate = int(str(prefix) * num_repeats)
|
||||
if start <= candidate <= end and candidate not in done:
|
||||
print(f"Adding: {candidate}")
|
||||
count += int(candidate)
|
||||
done.append(candidate)
|
||||
|
||||
print(count)
|
||||
1
2/input.txt
Normal file
1
2/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
1061119-1154492,3-23,5180469-5306947,21571-38630,1054-2693,141-277,2818561476-2818661701,21177468-21246892,40-114,782642-950030,376322779-376410708,9936250-10074071,761705028-761825622,77648376-77727819,2954-10213,49589608-49781516,9797966713-9797988709,4353854-4515174,3794829-3861584,7709002-7854055,7877419320-7877566799,953065-1022091,104188-122245,25-39,125490-144195,931903328-931946237,341512-578341,262197-334859,39518-96428,653264-676258,304-842,167882-252124,11748-19561
|
||||
Reference in New Issue
Block a user