solve part a, not happy with this solution though...
This commit is contained in:
41
2/cubes.py
Normal file
41
2/cubes.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
from aocd.models import Puzzle
|
||||||
|
from aocd import submit
|
||||||
|
|
||||||
|
def valid_games(games:list) -> list:
|
||||||
|
"""
|
||||||
|
Solve part a.
|
||||||
|
- check which of the games are possible with the follwing limitations:
|
||||||
|
- 12 red
|
||||||
|
- 13 green
|
||||||
|
- 14 blue
|
||||||
|
- add up their IDs
|
||||||
|
"""
|
||||||
|
rules = {"red":12, "green":13, "blue":14}
|
||||||
|
|
||||||
|
valid_ids = []
|
||||||
|
for i, game in enumerate(games.split("\n"), start=1):
|
||||||
|
throws = game.split(":")[1].split(";")
|
||||||
|
valid = True
|
||||||
|
for throw in throws:
|
||||||
|
cubes = throw.split(",")
|
||||||
|
for cube in cubes:
|
||||||
|
_, amount, color = cube.split(" ")
|
||||||
|
if int(amount) > rules[color]:
|
||||||
|
valid = False
|
||||||
|
if valid:
|
||||||
|
valid_ids.append(i)
|
||||||
|
return valid_ids
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# get puzzle and parse data
|
||||||
|
puzzle = Puzzle(year=2023, day=2)
|
||||||
|
|
||||||
|
# part a:
|
||||||
|
answer_a = sum(valid_games(puzzle.input_data))
|
||||||
|
print(f"{answer_a}")
|
||||||
|
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)
|
||||||
Reference in New Issue
Block a user