Optimize part2
This commit is contained in:
parent
1850a2145c
commit
e268a50409
@ -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.ObjectLongMaps
|
||||
import org.eclipse.collections.impl.factory.primitive.ObjectIntMaps
|
||||
|
||||
@Day(14)
|
||||
class Day14(@Lines val input: Input<List<String>>) {
|
||||
@ -42,48 +42,23 @@ class Day14(@Lines val input: Input<List<String>>) {
|
||||
}
|
||||
|
||||
fun part2(): Long {
|
||||
val mem = ObjectLongMaps.mutable.empty<String>()
|
||||
val mem = ObjectIntMaps.mutable.empty<String>()
|
||||
|
||||
var currentMask: String = ""
|
||||
var currentMask = ""
|
||||
|
||||
for (line in input.value) {
|
||||
if (line.startsWith("mask")) {
|
||||
currentMask = line.removePrefix("mask = ")
|
||||
if (line[1] == 'a') {
|
||||
currentMask = line.substring(7)
|
||||
} else {
|
||||
|
||||
val (address, value) = memRe.find(line)!!.destructured.let { (add, value) ->
|
||||
add.toLong().toBin36() to value.toLong()
|
||||
add.toLong().toBin36() to value.toInt()
|
||||
}
|
||||
|
||||
val addresses = address.zip(currentMask).map { (address, mask) ->
|
||||
when (mask) {
|
||||
'X' -> arrayOf('0', '1')
|
||||
'1' -> arrayOf('1')
|
||||
else -> arrayOf(address)
|
||||
}
|
||||
}
|
||||
val builders = generateMutations(currentMask, address)
|
||||
|
||||
val mutations = currentMask.count { it == 'X' }.let { 2.0.pow(it.toDouble()).toInt() }
|
||||
val builders = Array(mutations) { StringBuilder() }
|
||||
|
||||
var groups = 1
|
||||
|
||||
for (address in addresses) {
|
||||
if (address.size > 1) {
|
||||
groups *= 2
|
||||
val groupSize = mutations / groups
|
||||
var j = 0
|
||||
for (i in 0 until mutations) {
|
||||
val flip = i % groupSize == 0
|
||||
if (flip) j = if (j == 0) 1 else 0
|
||||
builders[i].append(address[j])
|
||||
}
|
||||
} else {
|
||||
builders.forEach { it.append(address[0]) }
|
||||
}
|
||||
}
|
||||
|
||||
builders.map { it.toString() }.forEach { address ->
|
||||
mem.put(address, value)
|
||||
for (builder in builders) {
|
||||
mem.put(builder.toString(), value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -91,6 +66,36 @@ 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) }
|
||||
|
||||
var groups = 1
|
||||
|
||||
for (i in mask.indices) {
|
||||
when (mask[i]) {
|
||||
'X' -> {
|
||||
groups *= 2
|
||||
val groupSize = mutations / groups
|
||||
var j = 1
|
||||
for (b in builders.indices) {
|
||||
val builder = builders[b]
|
||||
val flip = b % groupSize == 0
|
||||
if (flip) j = if (j == 0) 1 else 0
|
||||
builder.append(j)
|
||||
}
|
||||
}
|
||||
'1' -> {
|
||||
builders.forEach { it.append('1') }
|
||||
}
|
||||
else -> {
|
||||
builders.forEach { it.append(address[i]) }
|
||||
}
|
||||
}
|
||||
}
|
||||
return builders
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun main() = with(createDay<Day14>()) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user