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__':
|
||||
|
|
|
|||
|
|
@ -20,13 +20,13 @@ class TestThing(unittest.TestCase):
|
|||
}
|
||||
self.assertEqual(games, target)
|
||||
|
||||
def testMaxCubes(self):
|
||||
def testMaxNeededCubes(self):
|
||||
test_cases = [
|
||||
("3 blue, 2 red; 3 red, 4 blue", {"blue": 4, "red": 3}),
|
||||
("2 red, 1 blue; 4 blue, 2 yellow", {"red": 2, "blue": 4, "yellow": 2})
|
||||
]
|
||||
for grabs, max_cubes in test_cases:
|
||||
self.assertEqual(program.get_max_cubes(grabs), max_cubes)
|
||||
self.assertEqual(program.get_max_needed_cubes(grabs), max_cubes)
|
||||
|
||||
def testGameIsValid(self):
|
||||
cube_pools = {"red": 4, "green": 5, "blue": 6}
|
||||
|
|
@ -39,11 +39,16 @@ class TestThing(unittest.TestCase):
|
|||
for test_case, test_result in test_cases:
|
||||
self.assertEqual(program.game_is_valid(test_case, cube_pools), test_result)
|
||||
|
||||
def testSumOfValidGames(self):
|
||||
def testGetSumOfValidGames(self):
|
||||
lines = program.get_lines("test-input.txt")
|
||||
games = program.get_games(lines)
|
||||
cube_pools = {"red": 12, "green": 13, "blue": 14}
|
||||
self.assertEqual(program.sum_of_valid_games(games, cube_pools), 8)
|
||||
self.assertEqual(program.get_sum_of_valid_games(games, cube_pools), 8)
|
||||
|
||||
def testGetPowerOfGames(self):
|
||||
lines = program.get_lines("test-input.txt")
|
||||
games = program.get_games(lines)
|
||||
self.assertEqual(program.get_power_of_games(games), 2286)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue