day 02: solve part 2
This commit is contained in:
parent
186304801e
commit
cb8cc8034e
2 changed files with 28 additions and 9 deletions
|
|
@ -19,7 +19,7 @@ def get_games(lines: list) -> dict:
|
|||
return games
|
||||
|
||||
|
||||
def get_max_cubes(grabs: str) -> dict:
|
||||
def get_max_needed_cubes(grabs: str) -> dict:
|
||||
max_cubes = {}
|
||||
for grab in grabs.split(";"):
|
||||
for cube_string in grab.split(", "):
|
||||
|
|
@ -37,22 +37,36 @@ def game_is_valid(game: dict, cube_pools: dict) -> bool:
|
|||
return True
|
||||
|
||||
|
||||
def sum_of_valid_games(games: dict, cube_pools: dict) -> int:
|
||||
def get_sum_of_valid_games(games: dict, cube_pools: dict) -> int:
|
||||
valid_games = 0
|
||||
for game_num in games.keys():
|
||||
grabs = games[game_num]
|
||||
max_cubes = get_max_cubes(grabs)
|
||||
max_cubes = get_max_needed_cubes(grabs)
|
||||
if game_is_valid(max_cubes, cube_pools):
|
||||
valid_games += game_num
|
||||
return valid_games
|
||||
|
||||
|
||||
def get_power_of_games(games: dict) -> int:
|
||||
power_of_games = 0
|
||||
for game_num in games.keys():
|
||||
power = 1
|
||||
grabs = games[game_num]
|
||||
max_cubes = get_max_needed_cubes(grabs)
|
||||
for colour in max_cubes.keys():
|
||||
power *= max_cubes[colour]
|
||||
power_of_games += power
|
||||
return power_of_games
|
||||
|
||||
|
||||
def main():
|
||||
lines = get_lines("input.txt")
|
||||
cube_pools = {"red": 12, "green": 13, "blue": 14}
|
||||
games = get_games(lines)
|
||||
result = sum_of_valid_games(games, cube_pools)
|
||||
print(f"Part 1: The sum of valid games is: {result}")
|
||||
sovg = get_sum_of_valid_games(games, cube_pools)
|
||||
print(f"Part 1: The sum of valid games is: {sovg}")
|
||||
power = get_power_of_games(games)
|
||||
print(f"Part 2: The power of valid games is: {power}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue