Commit 0ca21ce3 authored by Caleb C. Sander's avatar Caleb C. Sander
Browse files

Update Phase 2

parent da81d832
No related merge requests found
Showing with 28 additions and 27 deletions
+28 -27
import java.util.*;
import java.util.stream.Collectors;
public class Bomb {
public String shufflePassword(String s) {
String code = "" + s.hashCode();
List<String> l = code.codePoints().boxed().map((x) -> "" + (char)(int)x).collect(Collectors.toList());
List<String> l = new ArrayList<>();
for (char c : code.toCharArray()) {
l.add("" + c);
}
Random r = new Random(1337);
Collections.shuffle(l, r);
......@@ -21,35 +23,41 @@ public class Bomb {
}
public void phase1(String password) {
String psave = password;
Set<String> animals = new HashSet<>(2, 1.0f);
animals.add("cow");
animals.add("horse");
animals.add("dog");
int i = 0;
for (String animal : animals) {
if (password.charAt(0) != animal.charAt(0)) {
if (password.charAt(i) != animal.charAt(0)) {
System.err.println("BOOM!");
System.exit(2);
}
password = password.substring(1);
i++;
}
System.err.println("You passed phase 1 with the password \"" + psave + "\"");
System.err.println("You passed phase 1 with the password \"" + password + "\"");
}
public void phase2(String password) {
String[] passwordPieces = password.split(" ");
Random r = new Random(1337);
Set<Integer> numbers = new HashSet<>();
for (int i = 0; i < 10000; i++) {
while (numbers.size() < 10000) {
numbers.add(r.nextInt());
}
String[] passwordPieces = password.split(" ");
Iterator<Integer> numbersIterator = numbers.iterator();
for (int i = 0; i < 10000; i++) {
if (Integer.parseInt(passwordPieces[i]) != (int)numbersIterator.next() && i == 500) {
System.err.println("BOOM!");
System.exit(3);
boolean correct = false;
int i = 0;
for (int number : numbers) {
if (i == 5000 && Integer.parseInt(passwordPieces[i]) == number) {
correct = true;
}
i++;
}
if (!correct) {
System.err.println("BOOM!");
System.exit(3);
}
System.err.println("You passed phase 2 with the password \"" + password + "\"");
}
......
......@@ -14,24 +14,17 @@ public class BombTests {
@DisplayName("Test BombMain")
@Test
public void testBombMain() {
PrintStream systemErr = System.err;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
System.setErr(new PrintStream(outputStream));
BombMain.main(null);
System.setErr(new PrintStream(new FileOutputStream(FileDescriptor.err)));
System.setErr(systemErr);
String output = outputStream.toString();
System.out.println(output);
String[] lines = output.replace("\r\n", "\n").split("\n");
for (int i = 0; i < 3; i++) {
if (i == 0) {
assertEquals(54955992, lines[i].split("\"")[1].hashCode());
}
else if (i == 1) {
assertEquals(103143, lines[i].split("\"")[1].hashCode());
}
else {
assertEquals(-602269950, lines[i].split("\"")[1].split(" ")[500].hashCode());
}
}
String[] lines = output.split("\r?\n");
assertEquals(54955992, lines[0].split("\"")[1].hashCode());
assertEquals(103143, lines[1].split("\"")[1].hashCode());
assertEquals(-1099484678, lines[2].split("\"")[1].split(" ")[5000].hashCode());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment