Created
December 11, 2015 06:44
-
-
Save desrtfx/b4e849ba5249693cd0d0 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.util.HashSet; | |
| import java.util.Set; | |
| public class Day11 { | |
| public static void main(String[] args) { | |
| String test1 = "abcdefgh"; | |
| String expected1 = "abcdffaa"; | |
| String test2 = "ghijklmn"; | |
| String expected2 = "ghjaabcc"; | |
| String result1 = generatePass(test1); | |
| String result2 = generatePass(test2); | |
| System.out.println("Test 1: " + test1 + " expected: " + expected1 | |
| + " generated: " + result1 + " ---> Test " | |
| + (expected1.equals(result1) ? "passed" : "failed")); | |
| System.out.println("Test 2: " + test2 + " expected: " + expected2 | |
| + " generated: " + result2 + " ---> Test " | |
| + (expected2.equals(result2) ? "passed" : "failed")); | |
| String password1 = "hxbxwxba"; | |
| String password2 = generatePass(password1); | |
| System.out.println("Part 1: " + password1 + " ---> " + password2); | |
| System.out.println("Part 2: " + password2 + " ---> " | |
| + generatePass(password2)); | |
| } | |
| public static boolean threeStraightLetters(char[] pass) { | |
| for (int i = 0; i < pass.length - 2; i++) { | |
| if ((pass[i] == pass[i + 1] - 1) | |
| && (pass[i + 1] == pass[i + 2] - 1)) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| public static boolean noForbiddenLetters(char[] pass) { | |
| for (char ch : pass) { | |
| if ((ch == 'i') || (ch == 'l') || (ch == 'o')) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| public static boolean doubleDifferentPair(char[] pass) { | |
| int count = 0; | |
| Set<Character> chars = new HashSet<>(); | |
| for (int i = 0; i < pass.length - 1; i++) { | |
| if ((pass[i] == pass[i + 1]) | |
| && ((chars.isEmpty()) || (!chars.contains(pass[i])))) { | |
| count++; | |
| chars.add(pass[i]); | |
| if (count >= 2) { | |
| return true; | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| public static String generatePass(String password) { | |
| char[] pass = password.toCharArray(); | |
| int passLen = pass.length - 1; | |
| while ((!(threeStraightLetters(pass) && noForbiddenLetters(pass) && doubleDifferentPair(pass))) | |
| || (password.equals(String.valueOf(pass)))) { | |
| for (int i = passLen; i >= 0; i--) { | |
| pass[i] = (char) ((int) pass[i] + 1); | |
| if (!(pass[i] == (char) ((int) 'z') + 1)) { | |
| break; | |
| } else { | |
| pass[i] = 'a'; | |
| } | |
| } | |
| } | |
| return String.valueOf(pass); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment