Created
December 2, 2021 15:09
-
-
Save MysterionRise/fd50a4a9827c98f071d1b063f75cd10f to your computer and use it in GitHub Desktop.
Cleanup2.java
This file contains 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
package org.mystic.amthauer; | |
import java.io.IOException; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.Scanner; | |
public class Cleanup2 { | |
//0 1 2 3 4 5 | |
//1218 2010 2118 2103 2097 3 | |
//1221 2116 2079 2122 2090 4 | |
//1217 2108 2042 2098 2160 3 | |
//1212 2085 2132 2057 2106 3 | |
//1208 2086 2145 2029 2131 3 | |
public static void main(String[] args) throws IOException { | |
final Random random = new Random(); | |
Scanner in = new Scanner(Paths.get("2.txt")); | |
while (in.hasNextLine()) { | |
String line = in.nextLine(); | |
if (!line.isEmpty()) { | |
String[] tokens = line.split(" "); | |
StringBuilder result = new StringBuilder(); | |
boolean firstTime = true; | |
int currentPos = 0; | |
int lastToken = Integer.parseInt(tokens[tokens.length - 1]); | |
List<Integer> arr = new ArrayList<>(); | |
for (String token : tokens) { | |
arr.add(Integer.parseInt(token)); | |
} | |
for (String token : tokens) { | |
int tokenValue = Integer.parseInt(token); | |
if (tokenValue >= 2081 && tokenValue <= 2160 && firstTime && currentPos != lastToken) { | |
int randomValue = generateRandom(random, arr); | |
result.append(randomValue).append(" "); | |
firstTime = false; | |
} else { | |
result.append(token).append(" "); | |
} | |
currentPos++; | |
} | |
System.out.println(result); | |
} | |
} | |
} | |
private static int generateRandom(Random random, List<Integer> arr) { | |
boolean isSatisfied = false; | |
int candidate = 0; | |
while (!isSatisfied) { | |
candidate = random.nextInt(80) + 2001; | |
isSatisfied = check(arr, candidate); | |
} | |
return candidate; | |
} | |
private static boolean check(List<Integer> arr, int candidate) { | |
return arr.stream().filter(x -> x >= candidate + 6 && x <= candidate - 6).toList().isEmpty(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment