You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.2 KiB
Python
58 lines
1.2 KiB
Python
import re
|
|
|
|
|
|
def parse_line(line):
|
|
match = re.match(r"Game (\d+): (.+)", line)
|
|
|
|
game_id = int(match.group(1))
|
|
cubes = parse_cubes(match.group(2))
|
|
|
|
return (game_id, cubes)
|
|
|
|
|
|
def parse_cubes(cubes):
|
|
return [parse_pull(pull) for pull in cubes.split("; ")]
|
|
|
|
|
|
def parse_pull(pull):
|
|
r = g = b = 0
|
|
|
|
for color in pull.split(", "):
|
|
[n, c] = color.split(" ")
|
|
if c == "red":
|
|
r += int(n)
|
|
elif c == "green":
|
|
g += int(n)
|
|
else:
|
|
b += int(n)
|
|
|
|
return (r, g, b)
|
|
|
|
|
|
def process_input(infile):
|
|
id_sum = 0
|
|
|
|
for game in [parse_line(line) for line in infile]:
|
|
r = g = b = 0
|
|
for pr, pg, pb in game[1]:
|
|
r = max(r, pr)
|
|
g = max(g, pg)
|
|
b = max(b, pb)
|
|
|
|
if r <= 12 and g <= 13 and b <= 14:
|
|
# print(f"Game {game[0]} legal.")
|
|
id_sum += game[0]
|
|
|
|
return id_sum
|
|
|
|
|
|
with open("day02/data/input100.txt") as infile:
|
|
result = process_input(infile)
|
|
print(result)
|
|
print(result == 8)
|
|
|
|
with open("day02/data/input101.txt") as infile:
|
|
result = process_input(infile)
|
|
print(result)
|
|
print(result == 2512)
|