From 918e918584c9385af03d5dc57c24fe979c4ad794 Mon Sep 17 00:00:00 2001 From: Ikatono Date: Sat, 2 Dec 2023 04:59:54 -0600 Subject: [PATCH] Day 2 Part B passes --- adv02b.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 adv02b.py diff --git a/adv02b.py b/adv02b.py new file mode 100644 index 0000000..eafe202 --- /dev/null +++ b/adv02b.py @@ -0,0 +1,29 @@ +import re + +def numpart(s): + return int(''.join(c for c in s if c in '0123456789')) + +def game(line): + id_part, cube_part = line.split(':') + id = numpart(id_part) + rounds = cube_part.split(';') + bp = 0 + rp = 0 + gp = 0 + for round in rounds: + cubes = round.split(',') + for cube in cubes: + count = numpart(cube) + if 'blue' in cube: + bp = max(bp, count) + elif 'red' in cube: + rp = max(rp, count) + elif 'green' in cube: + gp = max(gp, count) + return bp * rp * gp + +with open('adv02a.txt') as f: + txt = f.readlines() + +s = sum(game(line) for line in txt) +print(s)