-
Notifications
You must be signed in to change notification settings - Fork 0
/
3a.kt
53 lines (49 loc) · 1.25 KB
/
3a.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
import kotlin.io.path.Path
import kotlin.io.path.readLines
import kotlin.math.*
fun readInput(name: String) = Path("$name").readLines()
fun Any?.println() = println(this)
fun checkNeighbours(x: Int, y: Int, input: List<String>): Boolean {
for (dx in arrayOf(-1, 0, 1)) {
for (dy in arrayOf(-1, 0, 1)) {
val nx = x + dx
val ny = y + dy
if (0 <= nx && nx < input.size && 0 <= ny && ny < input[0].length) {
if (!input[nx][ny].isDigit() && input[nx][ny] != '.') {
return true
}
}
}
}
return false
}
fun main() {
val fileName = "3.in"
val input = readInput(fileName)
var result = 0
for (i in 0..input.size-1) {
var j = 0
while (j < input[i].length) {
if (input[i][j].isDigit()) {
var touches : Boolean = false
touches = touches || checkNeighbours(i, j, input)
var k = j+1
while (k < input[i].length) {
if (input[i][k].isDigit()) {
touches = touches || checkNeighbours(i, k, input)
} else {
break
}
k += 1
}
val number = input[i].slice(j..k-1).toInt()
if (touches) {
result += number
}
j = k
}
j += 1
}
}
result.println()
}