Hi, I'm in Day7 part 1. I have the right algo for ...
# advent-of-code
j
Hi, I'm in Day7 part 1. I have the right algo for the example input, but not with the real input, I didn't see how to investigate that, other people having the same issue ? What I have missed ?
b
show us your code
馃憜 1
j
Are you sure ? it's a big mess at the moment 馃槃
Copy code
fun String.asBags(): HashMap<String, List<String>> {
    val bags = hashMapOf<String, List<String>>()

    lines().filterNot { it.endsWith("no other bags.") }
        .filterNot { it.startsWith("shiny gold") }
        .also { println("size ${it.size}") }
        .forEach { line ->
            val (bagName, children) = line.split(" bags contain ")
            val childrenList = children.dropLast(1)
                .split(", ")
                .map { bag -> bag.drop(2) }
                .map { it.replace(" bags", "").replace(" bag", "") }
            bags += bagName to childrenList
        }
    return bags
}
This is for the data structure creation
a
Otherwise, not sure we can help you
j
Copy code
fun isShinyInside(bagName: String, bags: HashMap<String, List<String>>): Boolean {
    println("next bag name: $bagName")
    val children = bags[bagName]
    if (children?.any { child -> child == "shiny gold" } == true) {
        println(" ** shiny found **")
        return true
    } else {
        println("no shiny found, go deeper")
        indent++
        children?.forEach { child ->
            return isShinyInside(child, bags)
        }
    }
    return false
}
Recursive part
a
can you just send the entire file @J茅r么me Gully
j
Copy code
@Test
fun `sum of shiny gold bags`() {
    val bags = realInput.asBags()
    val sum = bags.keys.sumBy { bagName ->
        if (isShinyInside(bagName, bags)) 1 else 0
    }
    println("Part A: $sum")
}
Use real input with
isShinyInside
to count shiny gold
Yep sorry
Copy code
class Day7 {

    @Nested
    inner class PartA {

        @Nested
        inner class `Example data` {
            @Test
            fun `assert sum of shiny gold bags is 4`() {
                val bags = exampleInput.asBags()
                val sum = bags.keys.sumBy { bagName ->
//            println("loop $bagName")
                    if (isShinyInside(bagName, bags)) 1 else 0
                }
                assertEquals(4, sum)
            }
        }

        @Nested
        inner class `Real data` {
            @Test
            fun `sum of shiny gold bags`() {
                val bags = realInput.asBags()
                val sum = bags.keys.sumBy { bagName ->
                    if (isShinyInside(bagName, bags)) 1 else 0
                }
                println("Part A: $sum")
            }
        }
    }

    fun isShinyInside(bagName: String, bags: HashMap<String, List<String>>): Boolean {
        println("next bag name: $bagName")
        val children = bags[bagName]
        if (children?.any { child -> child == "shiny gold" } == true) {
            println(" ** shiny found **")
            return true
        } else {
            println("no shiny found, go deeper")
            children?.forEach { child ->
                return isShinyInside(child, bags)
            }
        }
        return false
    }

//    data class Bag(val name: String, val childrenBags: List<Bag> = emptyList(), var count: Int = 1)

    fun String.asBags(): HashMap<String, List<String>> {
        val bags = hashMapOf<String, List<String>>()

        lines().filterNot { it.endsWith("no other bags.") }
            .filterNot { it.startsWith("shiny gold") }
            .also { println("size ${it.size}") }
            .forEach { line ->
                val (bagName, children) = line.split(" bags contain ")
                val childrenList = children.dropLast(1)
                    .split(", ")
                    .map { bag -> bag.drop(2) }
                    .map { it.replace(" bags", "").replace(" bag", "") }
                bags += bagName to childrenList
            }
        return bags
    }
}
I used the test approach, I ommited the companion object with input
Sorry for your eyes, this code is so dirty 馃檭
b
are you using the number of bags anywhere?
j
no, I should ?
I guessed it's for part B
b
oh yeah sorry
Copy code
children?.forEach { child ->
            return isShinyInside(child, bags)
        }
not sure that's right
that mean if it is not in the first child, you return false
a
yes
b
but what you want is to continue checking
(I'll let you work that solution, come back here if you have trouble}
a
fixing that will solve your issue 馃槃 just checked
j
Ho yes right, thanks !
Yeah I will try to fix and clean it up, it's a shame to share a code like that 馃槀
a
you could even simplify that function further to
Copy code
fun isShinyInside(bagName: String, bags: HashMap<String, List<String>>): Boolean {
    val children = bags[bagName]
    return bagName == "shiny gold" || children != null && 
            (children.any { child -> child == "shiny gold" } || children.any { child -> isShinyInside(child, bags) })
}
b
not all code is born beautiful
馃槃 1
j
yeah I changed forEach for any, i must wait 1 minute to submit the answer, 馃
I like all the smart collections function we have, but in my day to day work i'm not used to use it a lot
true @bjonnh I try to have a simple/dirty code working, and after I refactor. Well the code above did not pass the second step 馃槀
Yeah thanks was that, part A validated
b
if you try to optimize too early you may not be able to do part2 without redoing everything anyway
馃憜 1
2015 day 9 part 2 took me a few seconds yesterday
I just had to write out.maxOrNull() instead of out.minOrNull()
馃敟 1
a
I had to write out another function for part 2 but the idea was similar, so did not take long
j
Thanks for your code @adamratzman I will analyse it when I finish the day 7, erf for day 8 there is only one hour left for France till midnight, I'm late ^^
Something is easyer/faster to copy/past solution part A and adapt it a bit for part B yeah
b
oh this I never do
I just keep my data structures not too optimized (keeping all values, arguments etc)
for example in the case of the bags
since part1 my data structure contained the numbers
sometimes it is not used, but when it is used, you gain valuable time
a
yeah I did that too