-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay04.kt
60 lines (54 loc) · 1.39 KB
/
Day04.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
49
50
51
52
53
54
55
56
57
58
59
60
import java.io.File
import java.lang.IllegalArgumentException
fun main() {
val reader = File("files/day4.txt").bufferedReader()
val a = reader.readLine().split("-").map { x -> x.toInt() }
println(Day04().part1(a[0], a[1]))
println(Day04().part2(a[0], a[1]))
}
class Day04 {
fun part1(min: Int, max: Int): Int {
var res = 0
for (i in min..max) {
if (checkNumber1(i)) {
res++
}
}
return res
}
private fun checkNumber1(n: Int): Boolean {
val s: String = n.toString()
var double = false
for (i in 0 until s.length - 1) {
if (s[i] > s[i + 1]) {
return false
}
if (s[i] == s[i + 1]) {
double = true
}
}
return double
}
fun part2(min: Int, max: Int): Int {
var res = 0
for (i in min..max) {
if (checkNumber2(i)) {
res++
}
}
return res
}
private fun checkNumber2(n: Int): Boolean {
val s: String = n.toString()
var double = false
for (i in 0 until s.length - 1) {
if (s[i] > s[i + 1]) {
return false
}
if (s[i] == s[i + 1] && s.count { x -> x == s[i] } == 2) {
double = true
}
}
return double
}
}