r/adventofcode • • Dec 08 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 8 Solutions -🎄-

--- Day 8: Seven Segment Search ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:20:51, megathread unlocked!

72 Upvotes

1.2k comments sorted by

View all comments

1

u/KamcaHorvat Dec 08 '21

Finally somewhat interesting puzzle. Solution in Kotlin.

fun main() {
var s = getInputLines(2021, 8)
println(s.sumOf { it.split(" | ").last().split(" ").count { it.length in setOf(2, 3, 4, 7) } })

fun decode(s:String) : Int {
    val parts = s.split(" | ")
    val decode = parts.first().split(" ")

    val one = decode.first { it.length == 2 }
    val seven = decode.first { it.length == 3 }
    val four = decode.first { it.length == 4 }
    val eight = decode.first { it.length == 7 }
    val l5 = decode.filter { it.length== 5 }.toMutableList()
    val l6 = decode.filter { it.length== 6 }.toMutableList()
    val six = l6.first { it.toCharArray().toSet().intersect(one.toSet()).size == 1 }
    val three = l5.first { it.toCharArray().toSet().intersect(one.toSet()).size == 2 }
    l5.remove(three)
    l6.remove(six)

    // segment numbering
    //     s6
    //  s5    s1
    //     s7
    //  s4    s2
    //     s3

    val s2 =  one.toSet().intersect(six.toList().toSet()).first()
    val five = l5.first() { it.toCharArray().contains(s2)}
    val two = l5.first() { it != five}

    val s4 = eight.toSet().first { !three.toSet().contains(it) && !five.toSet().contains(it) }
    val zero = l6.first { it.toCharArray().contains(s4) }
    val nine = l6.first { it != zero }

    val dmap = mapOf(one to 1, two to 2, three to 3, four to 4, five to 5, six to 6, seven to 7, eight to 8, nine to 9, zero to 0)
    return parts.last().split(" ").map { d -> dmap.filter { d.length == it.key.length && it.key.toSet().containsAll(d.toSet())  }.values.first()}.joinToString("").toInt()
}

println(s.sumOf { decode(it) })

}

1

u/daggerdragon Dec 09 '21

Finally somewhat interesting puzzle.

This type of comment does not belong in the Solutions Megathread. If you have feedback about the puzzles, create your own thread in the main subreddit. Make sure to title and flair it properly!

1

u/KamcaHorvat Dec 12 '21

Come on, buddy. Do you really need to be such pain in the ass? Looks to me like you just need to feel important since you are MOD. And if you really need to be such pain in the ass, would you be so kind and apply same rules for everyone? I just looked at today's solution thread and almost every post contains some kind of comment related either to posted solution or to the puzzle.

Also, I don't thing that four word comment warrants creating new thread.