advent of code 2023 edition
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aoc_2023/aoc_02b.py

43 lines
1007 B

10 months ago
#!python3
import re
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("filename", help="the file from which to take the list of games")
args = parser.parse_args()
line_re = re.compile("Game (\d+):(.*)")
red_re = re.compile(".*?(\d+) red.*")
green_re = re.compile(".*?(\d+) green.*")
blue_re = re.compile(".*?(\d+) blue.*")
def game_from_line(string):
m = line_re.match(string)
game_number = int(m.group(1))
game_strings = m.group(2).split(";")
games = []
for g in game_strings:
reds = red_re.match(g)
reds = int(reds.group(1)) if reds else 0
greens = green_re.match(g)
greens = int(greens.group(1)) if greens else 0
blues = blue_re.match(g)
blues = int(blues.group(1)) if blues else 0
games.append((reds, greens, blues))
return (game_number, games)
all_games = []
with open(args.filename, "r") as f:
for l in f.readlines():
all_games.append(game_from_line(l))
answer = 0
for l in all_games:
r, g, b = map(max, zip(*l[1]))
answer += r*g*b
print(answer)