add solution to part b, still not happy

This commit is contained in:
2023-12-02 16:22:12 +01:00
parent 35051f0ef3
commit 325146fe36

View File

@@ -1,7 +1,8 @@
from aocd.models import Puzzle
from aocd import submit
def valid_games(games:list) -> list:
def valid_games(games: list) -> list:
"""
Solve part a.
- check which of the games are possible with the follwing limitations:
@@ -10,7 +11,7 @@ def valid_games(games:list) -> list:
- 14 blue
- add up their IDs
"""
rules = {"red":12, "green":13, "blue":14}
rules = {"red": 12, "green": 13, "blue": 14}
valid_ids = []
for i, game in enumerate(games.split("\n"), start=1):
@@ -26,6 +27,34 @@ def valid_games(games:list) -> list:
valid_ids.append(i)
return valid_ids
def minimum_sets(games: list) -> list:
"""
Solve part b.
- figure out the minimum amount of cubes necessary of each color for each throw
- multiply the values of each minimum number together
- to solve sum these powers up
"""
# find all minimas
minimas = []
for i, game in enumerate(games.split("\n"), start=1):
minimum = {"red": 0, "green": 0, "blue": 0}
throws = game.split(":")[1].split(";")
for throw in throws:
cubes = throw.split(",")
for cube in cubes:
_, amount, color = cube.split(" ")
if int(amount) > minimum[color]:
minimum[color] = int(amount)
minimas.append(minimum)
# calculate all powers
powers = []
for minimum in minimas:
powers.append(minimum["red"] * minimum["green"] * minimum["blue"])
return powers
if __name__ == "__main__":
# get puzzle and parse data
puzzle = Puzzle(year=2023, day=2)
@@ -36,6 +65,6 @@ if __name__ == "__main__":
submit(answer_a, part="a", day=2, year=2023)
# part b:
#answer_b = 0
#print(f"{answer_b}")
#submit(answer_b, part="b", day=2, year=2023)
answer_b = sum(minimum_sets(puzzle.input_data))
print(f"{answer_b}")
submit(answer_b, part="b", day=2, year=2023)