1
0

Compare commits

..

1 Commits

Author SHA1 Message Date
e72f71c1bf Use custom class for faster hashcode 2020-12-24 16:17:24 +01:00

View File

@ -12,7 +12,7 @@ import org.eclipse.collections.api.factory.Bags
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)
fun translate(other: HexPoint) = HexPoint(this.x + other.x, this.y + other.y, this.z + other.z)
}
private enum class Direction(vararg coordinates: Int) {
@ -23,7 +23,7 @@ class Day24(@Lines val input: Input<List<String>>) {
NW(0, 1, -1),
NE(1, 0, -1);
val coordinates = coordinates
val coordinates = HexPoint(coordinates[0], coordinates[1], coordinates[2])
}
private fun parseTile(line: String) = "e|se|sw|w|nw|ne".toRegex()
@ -33,23 +33,14 @@ class Day24(@Lines val input: Input<List<String>>) {
.toList()
private val tiles = input.value.map { parseTile(it) }.map {
it.map { it.coordinates }
.reduce { acc, ints -> intArrayOf(acc[0] + ints[0], acc[1] + ints[1], acc[2] + ints[2]) }
.let { (x, y, z) -> HexPoint(x, y, z) }
it.map { it.coordinates }.reduce { acc, ints -> acc.translate(ints) }
}
fun part1() = Bags.immutable.ofAll(tiles).selectBlacks().size()
private fun Bag<HexPoint>.selectBlacks() = selectByOccurrences { it % 2 == 1 }
private fun HexPoint.adjacents() = listOf(
translate(1, -1, 0),
translate(0, -1, 1),
translate(-1, 0, 1),
translate(-1, 1, 0),
translate(0, 1, -1),
translate(1, 0, -1)
)
private fun HexPoint.adjacents() = Direction.values().map { this.translate(it.coordinates) }
// black -> odd
// white -> even || not in bag -> !in black