-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay02A.java
81 lines (65 loc) · 2.45 KB
/
Day02A.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
80
81
package advent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Day02A {
public static void main(String[] args) throws IOException {
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Paste the input:");
final ArrayList<Password> passwordList = new ArrayList<>();
String readLine;
do {
readLine = bufferedReader.readLine();
if (readLine != null && !readLine.isEmpty()) {
final String[] split = readLine.trim().split(" ");
final String[] boundsSplit = split[0].split("-");
passwordList.add(new Password(Integer.parseInt(boundsSplit[0]), Integer.parseInt(boundsSplit[1]), split[1].charAt(0), split[2]));
} else {
break;
}
} while (true);
bufferedReader.close();
System.out.println("got " + passwordList.size() + " passwords");
int count = 0;
for (final Password password : passwordList) {
final boolean valid = password.isValid();
System.out.println(password.toString() + " is " + (valid ? "valid" : "INVALID"));
if (valid) {
count++;
}
}
System.out.println("Valid passwords: " + count);
}
private static class Password {
private final Integer min;
private final Integer max;
private final char character;
private final String password;
public Password(final Integer min, final Integer max, final char character, final String password) {
this.min = min;
this.max = max;
this.character = character;
this.password = password;
}
public boolean isValid() {
int count = 0;
for (int i = 0; i < password.length(); i++) {
if (password.charAt(i) == character) {
if (count == max) return false;
count++;
}
}
return count >= min;
}
@Override
public String toString() {
return "Password{" +
"min=" + min +
", max=" + max +
", character='" + character + '\'' +
", password='" + password + '\'' +
'}';
}
}
}