Optimize Day14 + benchmarks
This commit is contained in:
@@ -6,7 +6,7 @@ import be.vandewalleh.aoc.utils.input.Lines
|
||||
import be.vandewalleh.aoc.utils.input.createDay
|
||||
import kotlin.math.pow
|
||||
import org.eclipse.collections.impl.factory.primitive.IntObjectMaps
|
||||
import org.eclipse.collections.impl.factory.primitive.ObjectIntMaps
|
||||
import org.eclipse.collections.impl.factory.primitive.LongIntMaps
|
||||
|
||||
@Day(14)
|
||||
class Day14(@Lines val input: Input<List<String>>) {
|
||||
@@ -42,7 +42,7 @@ class Day14(@Lines val input: Input<List<String>>) {
|
||||
}
|
||||
|
||||
fun part2(): Long {
|
||||
val mem = ObjectIntMaps.mutable.empty<String>()
|
||||
val mem = LongIntMaps.mutable.empty()
|
||||
|
||||
var currentMask = ""
|
||||
|
||||
@@ -55,10 +55,10 @@ class Day14(@Lines val input: Input<List<String>>) {
|
||||
add.toLong().toBin36() to value.toInt()
|
||||
}
|
||||
|
||||
val builders = generateMutations(currentMask, address)
|
||||
val mutations = generateMutations(currentMask, address)
|
||||
|
||||
for (builder in builders) {
|
||||
mem.put(builder.toString(), value)
|
||||
for (mutation in mutations) {
|
||||
mem.put(String(mutation).toLong(2), value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,9 +66,9 @@ class Day14(@Lines val input: Input<List<String>>) {
|
||||
return mem.values().sum()
|
||||
}
|
||||
|
||||
private fun generateMutations(mask: String, address: String): Array<StringBuilder> {
|
||||
val mutations = mask.count { it == 'X' }.let { 2.0.pow(it.toDouble()).toInt() }
|
||||
val builders = Array(mutations) { StringBuilder(36) }
|
||||
private fun generateMutations(mask: String, address: String): Array<CharArray> {
|
||||
val mutationCount = mask.count { it == 'X' }.let { 2.0.pow(it.toDouble()).toInt() }
|
||||
val mutations = Array(mutationCount) { CharArray(36) }
|
||||
|
||||
var groups = 1
|
||||
|
||||
@@ -76,24 +76,20 @@ class Day14(@Lines val input: Input<List<String>>) {
|
||||
when (mask[i]) {
|
||||
'X' -> {
|
||||
groups *= 2
|
||||
val groupSize = mutations / groups
|
||||
var j = 1
|
||||
for (b in builders.indices) {
|
||||
val builder = builders[b]
|
||||
val groupSize = mutationCount / groups
|
||||
var currentChar = '1'
|
||||
for (b in mutations.indices) {
|
||||
val builder = mutations[b]
|
||||
val flip = b % groupSize == 0
|
||||
if (flip) j = if (j == 0) 1 else 0
|
||||
builder.append(j)
|
||||
if (flip) currentChar = if (currentChar == '0') '1' else '0'
|
||||
builder[i] = currentChar
|
||||
}
|
||||
}
|
||||
'1' -> {
|
||||
builders.forEach { it.append('1') }
|
||||
}
|
||||
else -> {
|
||||
builders.forEach { it.append(address[i]) }
|
||||
}
|
||||
'1' -> mutations.forEach { it[i] = '1' }
|
||||
else -> mutations.forEach { it[i] = address[i] }
|
||||
}
|
||||
}
|
||||
return builders
|
||||
return mutations
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user