<Advent of Code 2021 day 8> :thread:
# advent-of-code
a
d
Well this looks like a hard problem
The first hard part being understanding the problem statement 🤣
d
Yes, lots of tteeexxxttt - but finally I figured out the pattern
d
Yeah I now get the question but have to come back to it
d
I subtracted the segments of the 1 from the 4 to help me distinguish the 5 from the 2.
e
@Marcin Wisniowski Btw,
s.chunked(1)
can be simplified to
s.toList()
m
@elizarov That would return a
List<Char>
, where
chunked(1)
returns a
List<String>
. Of course I could make the solution work with
Char
but the behavior is different.
e
Exactly and that’s the point. Char is a bit cleaner than a string of length one.
m
Sure, updated. : )
e
also
.filter(predicate).size == .count(predicate)
👍 1
I have a bit of a different partitioning to derive the different elements than that, https://github.com/ephemient/aoc2021/blob/main/kt/src/commonMain/kotlin/com/github/ephemient/aoc2021/Day8.kt
👌 1
but I also tried out brute force, it works. the size of the input isn't so big and just trying every permutation is easily doable, just slower than those approach of course
m
Yeah, I saw that a bunch of people on reddit brute forced it.
d
Updated my solution with part 2
I went a different route and wrote a solver for the a-g mappings
p
https://github.com/Pitel/aoc21/blob/main/day08/src/main/kotlin/com/24i/adventofcode/App.kt my solution with coroutines bruteforcing. Not pretty. stdlib needs
permuations()
method! And it takes 12 seconds on Raspberry Pi 4.
p
Easy as pie 🥧😋
I'm basically doing it logically and checking intersections of numbers
p
https://github.com/tKe/aoc-21/blob/main/kotlin/src/main/kotlin/year2021/Day08.kt that was fun 🙂 I expect there are many different solutions coming out of this one…
e
We’ve just discussed during lunch whether Kotlin stdlib needs
permutations
method. No one have ever needed it in any real-life code. We could not figure out why on earth C++ stdlib has methods to generate permutations.
e
But why? There are tons of other combinatorial objects that you might(?) need to generate - subsets, selections, etc. Why adding permutations? Do they have any real-life use-cases?
m
median()
for lists is a missing method that I actually needed in real world code multiple times (and
average()
exists). Permutations? Only ever in puzzles. (My point is that if we don't need something as simple and useful as
median()
, then we certainly don't need permutations either).
p
I need permations in Tests quite often
e
@Paul Woitaschek Please, tell more in here https://youtrack.jetbrains.com/issue/KT-50137
@Marcin Wisniowski Can you, please, tell more on your uses of
median()
in https://youtrack.jetbrains.com/issue/KT-50138
m
Will do.
c
Not the prettiest, but gets the job done 😂: https://github.com/cak/advent-of-code-2021/blob/main/src/Day08.kt
m
Copy code
object Day8 : Challenge() {
    val parsed = input.lines().map { it.split(" | ").map { it.split(" ").map { it.toSortedSet() } } }

    override fun part1() = parsed.sumOf { (_, output) -> output.map{ it.size }.count(listOf(2, 4, 3, 7)::contains) }
    override fun part2() = parsed.sumOf { (input, output) ->
        val wires = Array<Set<Char>>(10) { emptySet() }.apply {
            this[1] = input.single { it.size == 2 }
            this[4] = input.single { it.size == 4 }
            this[7] = input.single { it.size == 3 }
            this[8] = input.single { it.size == 7 }
            this[3] = input.single { it.size == 5 && this[7] in it }
            this[2] = input.single { it.size == 5 && this[8] - this[4] in it }
            this[5] = input.single { it.size == 5 && this[8] - this[2] in it }
            this[9] = input.single { it.size == 6 && this[3] in it }
            this[6] = input.single { it.size == 6 && this[8] - this[1] in it }
            this[0] = input.single { it.size == 6 && this[5] !in it }
        }
        output.fold(0L) { int, set -> int * 10 + wires.indexOf(set) }
    }

    operator fun <T> Set<T>.contains(other: Set<T>) = containsAll(other)
}
t
I ended up doing what a few others did - treated the inputs as sets and then found unique aspects of each digit (size, overlaps another, etc). I enjoyed that one: • BlogCode
👍 1
p
Instead of using a map I used properties, that makes it less error prone as an order mixup would lead to a compilation error. https://github.com/PaulWoitaschek/Advent-of-Code-2021/blob/main/src/main/kotlin/Day8.kt
d
Treating the a-g mapping as a substitution cipher, the ciphertext is highly vulnerable to frequency analysis plus disambiguation based on known word-level ciphertext -> plaintext mappings
e
I ended up switching from Set<Char> to Int (as bit set) - just as easy, and faster
p
Thank you very much for sharing your explanation
p
@ephemient you made me intrigued so I pushed a bitset based version of my solution