-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay11.java
79 lines (63 loc) · 2.21 KB
/
Day11.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package net.eugenpaul.adventofcode.y2015.day11;
import java.util.List;
import java.util.logging.Level;
import lombok.Getter;
import net.eugenpaul.adventofcode.helper.SolutionTemplate;
import net.eugenpaul.adventofcode.y2015.day11.rules.NotContainLetter;
import net.eugenpaul.adventofcode.y2015.day11.rules.StraightOfThree;
import net.eugenpaul.adventofcode.y2015.day11.rules.TwoDifferentPairs;
public class Day11 extends SolutionTemplate {
@Getter
private String nextPassword = null;
@Getter
private String nextPassword2 = null;
private List<IRule> rules;
public static void main(String[] args) {
Day11 puzzle = new Day11();
puzzle.doPuzzleFromFile("y2015/day11/puzzle1.txt");
}
public Day11() {
rules = List.of(//
new NotContainLetter('i'), //
new NotContainLetter('o'), //
new NotContainLetter('l'), //
new StraightOfThree(), //
new TwoDifferentPairs());
}
@Override
public boolean doEvent(String eventData) {
char[] password = eventData.toCharArray();
password = computeNextPassword(password);
nextPassword = new String(password);
logger.log(Level.INFO, () -> "nextPassword " + getNextPassword());
password = computeNextPassword(password);
nextPassword2 = new String(password);
logger.log(Level.INFO, () -> "nextPassword2 " + getNextPassword2());
return true;
}
private char[] computeNextPassword(char[] password) {
do {
password = incrementPassword(password, password.length - 1);
} while (!isPasswordOk(password));
return password;
}
private boolean isPasswordOk(char[] password) {
for (IRule rule : rules) {
if (!rule.checkPassword(password)) {
return false;
}
}
return true;
}
private char[] incrementPassword(char[] input, int position) {
if (position < 0) {
return input;
}
if (input[position] == 'z') {
input[position] = 'a';
return incrementPassword(input, position - 1);
}
input[position]++;
return input;
}
}