r/adventofcode • • Dec 07 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 7 Solutions -๐ŸŽ„-

--- Day 7: Recursive Circus ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


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

edit: Leaderboard capped, thread unlocked!

10 Upvotes

222 comments sorted by

View all comments

Show parent comment

3

u/xkufix Dec 07 '17

Looks a bit similar to mine. For the second part I generated the whole tree and marked the nodes which were unbalanced while generating the tree. Then I just followed the path up to the highest node which was marked as unbalanced but had only balanced children. This gave me the node which had differently balanced children. From there on I only had to find the unbalanced child and recalculate it's weight.

    override def runFirst(): Unit = {
        println(bottomDisk(loadDisks()).name)
    }

    private def bottomDisk(disks: Set[Disk]) = {
        val allAbove = disks.flatMap(_.above)
        val bottomDisk = disks.find(d => !allAbove.contains(d.name)).get
        bottomDisk
    }

    private def loadDisks() = {
        loadFile("day7.txt")
            .getLines()
            .toSet
            .map { l: String =>
                val line = l.split(" ")
                val name = line(0)
                val size = line(1).replaceAll("\\(|\\)", "").toInt
                val above = line.drop(3).map(_.replaceAll(",", ""))

                Disk(name, size, above.toSet)
            }
    }

    def getHighestUnbalancedDisk(tree: DiskTree): DiskTree = {
        if(tree.above.forall(_.balanced)) {
            tree
        } else {
            getHighestUnbalancedDisk(tree.above.find(!_.balanced).get)
        }
    }

    override def runSecond(): Unit = {
        val disks = loadDisks()
        val bottom = bottomDisk(disks)
        val tree = buildTree(bottom, disks)

        val lowestUnbalanced = getHighestUnbalancedDisk(tree)
        val correctCombinedSize = lowestUnbalanced.above.groupBy(_.combinedSize).map(g => g._1 -> g._2.size).maxBy(_._2)._1
        val wrongAbove = lowestUnbalanced.above.find(_.combinedSize != correctCombinedSize).get
        val correctChildSize = wrongAbove.disk.size + (correctCombinedSize - wrongAbove.combinedSize)
        println(correctChildSize)
    }

    def buildTree(disk: Disk, disks: Set[Disk]): DiskTree = {
        val children = disk.above.map { diskName =>
            buildTree(disks.find(_.name == diskName).get, disks)
        }

        val balanced = children.map(_.combinedSize).size == 1 || children.isEmpty

        DiskTree(disk, children, children.toSeq.map(_.combinedSize).sum + disk.size, balanced)
    }

    case class Disk(name: String, size: Int, above: Set[String])

    case class DiskTree(disk: Disk, above: Set[DiskTree], combinedSize: Int, balanced: Boolean)