-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay14.kt
54 lines (48 loc) · 1.97 KB
/
Day14.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
package aoc2017.day14
import aoc2017.day10.convertListToASCII
import aoc2017.day10.processListPart2
import util.Coord
import java.math.BigInteger
fun defragmentDisk(input: String): Int {
return generateHashList(input).sumBy { it -> it.count { it == '1' } }
}
fun countRegions(input: String): Int {
val hashArray = generateHashList(input).map { it -> it.map { (it - '0') }.toTypedArray() }.toTypedArray()
val neighborCoords = listOf(Coord(1, 0), Coord(-1, 0), Coord(0, 1), Coord(0, -1))
// Start from 2 to avoid overlapping with 0s and 1s - then subtract 2 at the end
var groupCount = 2
for ((rowId, row) in hashArray.withIndex())
for ((colId, char) in row.withIndex())
if (char == 1) {
hashArray[rowId][colId] = groupCount
val edge = mutableListOf(listOf(rowId, colId, groupCount))
// Implement BFS: assign counter to neighbours until they've all been visited
while (edge.isNotEmpty()) {
val (baseX, baseY, group) = edge.removeAt(0)
for (coord in neighborCoords) {
val x = baseX + coord.x
val y = baseY + coord.y
try {
if (hashArray[x][y] == 1) {
hashArray[x][y] = group
edge.add(listOf(x, y, group))
}
} catch (e: Exception) {}
}
}
groupCount++
}
return groupCount - 2
}
private fun printHashList(list: List<String>) {
for (line in list) {
line.forEach { char -> if (char == '1') print("#") else print(".") }
println()
}
}
private fun generateHashList(input: String): List<String> {
return (0 until 128).map {
val hash = processListPart2(convertListToASCII("$input-$it"))
BigInteger(hash, 16).toString(2).padStart(128, '0')
}
}