1
0

Use custom class for faster hashcode

This commit is contained in:
Hubert Van De Walle 2020-12-24 16:11:12 +01:00
parent 5d73f12e43
commit 02f18471cb

View File

@ -11,6 +11,10 @@ import org.eclipse.collections.api.factory.Bags
@Day(24) @Day(24)
class Day24(@Lines val input: Input<List<String>>) { class Day24(@Lines val input: Input<List<String>>) {
private data class HexPoint(val x: Int, val y: Int, val z: Int) {
fun translate(x: Int, y: Int, z: Int) = HexPoint(this.x + x, this.y + y, this.z + z)
}
private enum class Direction(vararg coordinates: Int) { private enum class Direction(vararg coordinates: Int) {
E(1, -1, 0), E(1, -1, 0),
SE(0, -1, 1), SE(0, -1, 1),
@ -31,28 +35,25 @@ class Day24(@Lines val input: Input<List<String>>) {
private val tiles = input.value.map { parseTile(it) }.map { private val tiles = input.value.map { parseTile(it) }.map {
it.map { it.coordinates } it.map { it.coordinates }
.reduce { acc, ints -> intArrayOf(acc[0] + ints[0], acc[1] + ints[1], acc[2] + ints[2]) } .reduce { acc, ints -> intArrayOf(acc[0] + ints[0], acc[1] + ints[1], acc[2] + ints[2]) }
.toList() .let { (x, y, z) -> HexPoint(x, y, z) }
} }
fun part1() = Bags.immutable.ofAll(tiles).selectBlacks().size() fun part1() = Bags.immutable.ofAll(tiles).selectBlacks().size()
private fun Bag<List<Int>>.selectBlacks() = selectByOccurrences { it % 2 == 1 } private fun Bag<HexPoint>.selectBlacks() = selectByOccurrences { it % 2 == 1 }
private fun List<Int>.adjacents(): List<List<Int>> { private fun HexPoint.adjacents() = listOf(
val (x, y, z) = this translate(1, -1, 0),
return listOf( translate(0, -1, 1),
listOf(x + 1, y - 1, z), translate(-1, 0, 1),
listOf(x, y - 1, z + 1), translate(-1, 1, 0),
listOf(x - 1, y, z + 1), translate(0, 1, -1),
listOf(x - 1, y + 1, z), translate(1, 0, -1)
listOf(x, y + 1, z - 1), )
listOf(x + 1, y, z - 1),
)
}
// black -> odd // black -> odd
// white -> even || not in bag -> !in black // white -> even || not in bag -> !in black
private fun day(bag: MutableBag<List<Int>>) { private fun day(bag: MutableBag<HexPoint>) {
val blacks = bag.selectBlacks().toSet() val blacks = bag.selectBlacks().toSet()
val all = (bag + bag.flatMap { it.adjacents() }).toSet() val all = (bag + bag.flatMap { it.adjacents() }).toSet()