I posted this in the solution thread, but since it...
# advent-of-code
m
I posted this in the solution thread, but since it's actually a question, I'll post it here too ⚠️ SPOILERS ⚠️
can anyone tell me why this works:
Copy code
private val parsed = input.split("\r\n\r\n").map { it.lines().map(String::toSet) }

override fun part1() = parsed.sumBy{ it.reduce { acc, set -> acc.union(set) }.size }
override fun part2() = parsed.sumBy{ it.reduce { acc, set -> acc.intersect(set) }.size }
but this only solves correctly for part1? The logic is the same (tried to speed things up)
Copy code
private val parsed = input.split("\r\n\r\n")
        .map {
            it.lines().map {
                it.fold(BitSet(26)){ acc: BitSet, c: Char ->
                    acc.apply { set(c - 'a') }
                }
            }
        }

override fun part1() = parsed.sumBy { it.reduce { acc, bitSet -> acc.apply { or(bitSet) } }.cardinality() } //CORRECT
override fun part2() = parsed.sumBy { it.reduce { acc, bitSet -> acc.apply { and(bitSet) } }.cardinality() } //INCORRECT ?!?
I'm mapping every string to a bitset where 'a' is bit 0 and 'z' is bit 25 then doing 'or' for union and 'and' for intersection, and then counting the 1 bits. the 'and' for intersection part doesn't work for some reason?