This commit is contained in:
Tyrel Souza 2022-12-19 01:10:23 -05:00
parent d40c2fec64
commit 2392cf3e57
1 changed files with 44 additions and 48 deletions

View File

@ -5,30 +5,6 @@ from dataclasses import dataclass
from scanf import scanf from scanf import scanf
@dataclass
class Blueprint():
blueprint:int
ore_ore_cost: int
clay_ore_cost: int
obsidian_ore_cost: int
obsidian_clay_cost: int
geode_ore_cost: int
geode_obsidian_cost: int
def produce_ore_robot(self, r):
return r.ore >= self.ore_cost
def produce_clay_robot(self, r):
return r.ore >= self.clay_ore_cost
def produce_obsidian_robot(self, r):
return r.ore >= self.obsidian_ore_cost and r.clay >= self.obsidian_clay_cost
def produce_geode_robot(self, r):
return r.ore >= self.geode_ore_cost and r.obsidian >= self.geode_obsidian_cost
def load_blueprints(rows): def load_blueprints(rows):
blueprints = {} blueprints = {}
for row in rows: for row in rows:
@ -38,9 +14,33 @@ def load_blueprints(rows):
"Each obsidian robot costs %d ore and %d clay. " "Each obsidian robot costs %d ore and %d clay. "
"Each geode robot costs %d ore and %d", row) "Each geode robot costs %d ore and %d", row)
bp = Blueprint(b, o_c, c_c, o_o_c, o_c_c, g_o_c, g_o_c) bp = Blueprint(b, o_c, c_c, o_o_c, o_c_c, g_o_c, g_o_c)
blueprints[bp.blueprint] = bp blueprints[bp.id] = bp
return blueprints return blueprints
@dataclass
class Blueprint():
id :int
ore_ore_cost: int
clay_ore_cost: int
obsidian_ore_cost: int
obsidian_clay_cost: int
geode_ore_cost: int
geode_obsidian_cost: int
def can_produce_ore_robot(self, r):
return r.ore >= self.ore_cost
def can_produce_clay_robot(self, r):
return r.ore >= self.clay_ore_cost
def can_produce_obsidian_robot(self, r):
return r.ore >= self.obsidian_ore_cost and r.clay >= self.obsidian_clay_cost
def can_produce_geode_robot(self, r):
return r.ore >= self.geode_ore_cost and r.obsidian >= self.geode_obsidian_cost
@dataclass @dataclass
class Resources(): class Resources():
ore: int = 0 ore: int = 0
@ -48,6 +48,7 @@ class Resources():
obsidian: int = 0 obsidian: int = 0
geodes: int = 0 geodes: int = 0
@dataclass @dataclass
class Robots(): class Robots():
ore: int = 1 ore: int = 1
@ -60,11 +61,11 @@ class Robots():
#def part1(rows, minutes=12): #def part1(rows, minutes=12):
def part1(rows, minutes=24): def part1(rows, minutes=24):
blueprints = load_blueprints(rows) blueprints = load_blueprints(rows)
quality = 0
for _,bp in blueprints.items(): for _,bp in blueprints.items():
resources = Resources() resources = Resources()
robots = Robots() robots = Robots()
print(robots)
for minute in range(1, minutes+1): for minute in range(1, minutes+1):
print(f"== Minute {minute} ==") print(f"== Minute {minute} ==")
@ -75,13 +76,14 @@ def part1(rows, minutes=24):
# SPEND THE RESOURCES # SPEND THE RESOURCES
if bp.produce_geode_robot(resources): if bp.can_produce_clay_robot(resources):
print(f"Spend {bp.geode_ore_cost} ore and {bp.geode_obsidian_cost} obsidian to start building a geode-cracking robot.") # Do we have enough to build obsidian?
resources.ore -= bp.geode_ore_cost if robots.clay < bp.obsidian_ore_cost:
resources.obsidian -= bp.geode_obsidian_cost print(f"Spend {bp.clay_ore_cost} ore to start building a clay-collecting robot.")
_add_geode = True resources.ore -= bp.clay_ore_cost
_add_clay = True
if bp.produce_obsidian_robot(resources): if bp.can_produce_obsidian_robot(resources):
# Do we have enough to build geodes? # Do we have enough to build geodes?
if robots.obsidian < bp.geode_obsidian_cost: if robots.obsidian < bp.geode_obsidian_cost:
print(f"Spend {bp.obsidian_ore_cost} ore and {bp.obsidian_clay_cost} clay to start building an obsidian-collecting robot.") print(f"Spend {bp.obsidian_ore_cost} ore and {bp.obsidian_clay_cost} clay to start building an obsidian-collecting robot.")
@ -89,12 +91,12 @@ def part1(rows, minutes=24):
resources.clay -= bp.obsidian_clay_cost resources.clay -= bp.obsidian_clay_cost
_add_obsidian = True _add_obsidian = True
if bp.produce_clay_robot(resources): if bp.can_produce_geode_robot(resources):
# Do we have enough to build obsidian? print(f"Spend {bp.geode_ore_cost} ore and {bp.geode_obsidian_cost} obsidian to start building a geode-cracking robot.")
if robots.clay < bp.obsidian_ore_cost: resources.ore -= bp.geode_ore_cost
print(f"Spend {bp.clay_ore_cost} ore to start building a clay-collecting robot.") resources.obsidian -= bp.geode_obsidian_cost
resources.ore -= bp.clay_ore_cost _add_geode = True
_add_clay = True
# GATHER RESOURCES # GATHER RESOURCES
resources.ore += robots.ore resources.ore += robots.ore
@ -123,18 +125,12 @@ def part1(rows, minutes=24):
robots.obsidian += 1 robots.obsidian += 1
print(f"The new obsidian-collecting robot is ready; you now have {robots.obsidian} of them.") print(f"The new obsidian-collecting robot is ready; you now have {robots.obsidian} of them.")
if _add_geode: if _add_geode:
robots.geode += 1 robots.geodes += 1
print(f"The new geode-cracking robot is ready; you now have {robots.geode} of them.") print(f"The new geode-cracking robot is ready; you now have {robots.geodes} of them.")
print() print()
print(geodes) quality += (bp.id * resources.geodes)
break print(quality)