Piotr Czajkowski 1 year ago
parent
commit
ced4416bef
1 changed files with 51 additions and 12 deletions
  1. 51 12
      19/code.go

+ 51 - 12
19/code.go

@@ -82,30 +82,69 @@ func produce(robots inventory, resources inventory) inventory {
 }
 
 func shouldProduceClay(plan blueprint, robots inventory, resources inventory) bool {
-	catchUp := resources
-	for i := 0; i < plan.clayCost; i++ {
-		catchUp = produce(robots, catchUp)
+	if robots.clay == 0 {
+		return true
 	}
 
-	if canProduceObsidian(plan, catchUp) {
-		return false
+	countWithout := 0
+	without := resources
+	for {
+		if canProduceObsidian(plan, without) {
+			break
+		}
+
+		without = produce(robots, without)
+		countWithout++
 	}
 
-	return true
+	countWith := 0
+	with := resources
+	with = produceClay(plan, with)
+	robots.clay++
+	for {
+		if canProduceObsidian(plan, with) {
+			break
+		}
+
+		with = produce(robots, with)
+		countWith++
+	}
+
+	return countWith < countWithout
 }
 
 func shouldProduceObsidian(plan blueprint, robots inventory, resources inventory) bool {
-	catchUp := resources
-	for i := 0; i < plan.obsidianCost[0]; i++ {
-		catchUp = produce(robots, catchUp)
+	if robots.obsidian == 0 {
+		return true
 	}
 
-	if canProduceGeode(plan, catchUp) {
-		return false
+	countWithout := 0
+	without := resources
+	for {
+		if canProduceGeode(plan, without) {
+			break
+		}
+
+		without = produce(robots, without)
+		countWithout++
+	}
+
+	countWith := 0
+	with := resources
+	with = produceObsidian(plan, with)
+	robots.obsidian++
+	for {
+		if canProduceGeode(plan, with) {
+			break
+		}
+
+		with = produce(robots, with)
+		countWith++
 	}
 
-	return true
+	return countWith < countWithout
 }
+
 func checkPlan(plan blueprint) int {
 	var robots inventory
 	robots.ore++