-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.kt
48 lines (38 loc) · 1.52 KB
/
Day2.kt
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
package aoc2020.day02
import java.io.File
import java.io.InputStream
fun readInputFileToList(path: String): List<PasswordInfo> {
val inputList = mutableListOf<PasswordInfo>()
val inputStream: InputStream = File(path).inputStream()
val lineList = mutableListOf<String>()
inputStream.bufferedReader().forEachLine { lineList.add(it) }
lineList
.asSequence()
.map { it.split("-", " ", ": ") }
.mapTo(inputList) { PasswordInfo(it[0].toInt(), it[1].toInt(), it[2].single(), it[3]) }
return inputList;
}
class PasswordInfo(var min: Int, var max: Int, var char: Char, var password: String)
fun isValidPassword(min: Int, max: Int, char: Char, password: String) : Boolean {
val count = password.count {
i -> i == char
}
return count in min..max
}
fun isValidPassword(passwordInfo: PasswordInfo) : Boolean {
return isValidPassword(passwordInfo.min, passwordInfo.max, passwordInfo.char, passwordInfo.password)
}
fun countValidPasswords(passwordList: List<PasswordInfo>, validationMethod: (PasswordInfo) -> Boolean): Int {
var count = 0
passwordList
.filter { validationMethod(it) }
.forEach { _ -> count += 1 }
return count;
}
// Part 2
fun isValidPassword2(passwordInfo: PasswordInfo) : Boolean {
return isValidPassword2(passwordInfo.min, passwordInfo.max, passwordInfo.char, passwordInfo.password)
}
fun isValidPassword2(min: Int, max: Int, char: Char, password: String) : Boolean {
return (password[min - 1] == char).xor(password[max - 1] == char)
}