Skip to content

Commit 8258f30

Browse files
committed
Added solution for part 2
1 parent 190b965 commit 8258f30

File tree

1 file changed

+16
-56
lines changed

1 file changed

+16
-56
lines changed

Day-07/kotlin/NyCodeGHG/solution.kts

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,27 @@ import kotlin.system.exitProcess
55
val allBags = arrayListOf<Bag>()
66

77
val contentRegex = "([0-9]+) ([\\w]+ [\\w]+) bag[s]?".toRegex()
8-
val noBagsRegex = "([\\w]+ [\\w]+) bags contain no other bags.".toRegex()
98

10-
val rules = arrayListOf<BagRule>()
9+
val bags = Files.readAllLines(Paths.get("input.txt"))
10+
.map { it.removeSuffix(".") }
11+
.filterNot { it.contains("no other") }
12+
.map { it.replace("bag[s]?".toRegex(), "").replace("contain", ",").split(",") }
13+
.map { it.map(String::trim) }
14+
.map { it[0] to Bag(it[0], toChildBags(it.subList(1, it.size))) }.toMap()
1115

12-
Files.readAllLines(Paths.get("input.txt")).forEach { line ->
16+
println(bags.filter { it.value.hasColor("shiny gold", bags) }.count())
17+
println(bags["shiny gold"]?.getChildBagNumber(bags))
1318

14-
val noBagsResult = noBagsRegex.find(line)
15-
if (noBagsResult != null) {
16-
val (type) = noBagsResult.destructured
17-
allBags.add(Bag(type))
18-
return@forEach
19-
}
19+
data class Bag(val color: String, val childBags: Map<String, Int>)
2020

21-
val parts = line.split(" bags contain ")
22-
val bagType = parts[0]
23-
allBags.add(Bag(bagType))
21+
fun Bag.hasColor(color: String, bags: Map<String, Bag>): Boolean =
22+
if (this.childBags.keys.contains(color)) true
23+
else this.childBags.keys.any { bags[it]?.hasColor(color, bags) == true }
2424

25-
parts[1].split(", ").map { it.replace(".", "") }.forEach {
26-
val result = contentRegex.find(it)
27-
val (count, type) = result?.destructured ?: error("parsing error")
28-
rules.add(BagRule(bagType, type, count.toInt()))
29-
}
25+
fun toChildBags(bags: List<String>): Map<String, Int> {
26+
return bags.map { it.substring(2) to Integer.parseInt(it.substring(0, 1)) }.toMap()
3027
}
31-
println("${rules.size} Rules")
3228

33-
val workingBags = arrayListOf<Bag>()
34-
35-
rules.forEach { rule ->
36-
var bag = getBag(rule.root)
37-
bag.bags.add(getBag(rule.type))
38-
}
39-
40-
var counter = 0
41-
workingBags.forEach {
42-
if (it.bags.containsShinyGolden()) {
43-
counter++
44-
}
45-
}
46-
println(counter)
47-
48-
fun getBag(type: String): Bag {
49-
var bag = workingBags.find { it.type == type }
50-
if (bag == null) {
51-
bag = Bag(type)
52-
workingBags.add(bag)
53-
}
54-
return bag
55-
}
56-
57-
data class Bag(val type: String, val bags: MutableList<Bag> = ArrayList())
58-
59-
data class BagRule(val root: String, val type: String, val count: Int)
60-
61-
fun MutableList<Bag>.containsShinyGolden(): Boolean {
62-
return map {
63-
if (it.type == "shiny gold")
64-
return@map true
65-
66-
if (it.bags.isEmpty())
67-
return@map false
68-
69-
return@map it.bags.containsShinyGolden()
70-
}.any { it }
29+
fun Bag.getChildBagNumber(bags: Map<String, Bag>): Int {
30+
return this.childBags.map { (bags[it.key]?.getChildBagNumber(bags) ?: 0) * it.value + it.value }.sum()
7131
}

0 commit comments

Comments
 (0)