Skip to content

Commit

Permalink
day 02
Browse files Browse the repository at this point in the history
  • Loading branch information
wimglenn committed Dec 2, 2023
1 parent c36f2f8 commit 790a1c8
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions aoc_wim/aoc2023/q02.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,19 @@
https://adventofcode.com/2023/day/2
"""
from aocd import data
from math import prod
from collections import Counter

bag = dict(zip("rgb", [12, 13, 14]))
bag = Counter("rgb"*12 + "g" + "bb")
a = b = 0
for line in data.splitlines():
maxs = dict.fromkeys("rgb", 0)
g, line = line.split(": ")
g = int(g.removeprefix("Game "))
handfuls = line.split("; ")
game_possible = True
for handful in handfuls:
counts = dict.fromkeys("rgb", 0)
for n_c in handful.split(", "):
n, c = n_c.split()
counts[c[0]] += int(n)
for c in "rgb":
maxs[c] = max(maxs[c], counts[c])
if any(counts[c] > bag[c] for c in "rgb"):
game_possible = False
a += g * game_possible
b += maxs["r"] * maxs["g"] * maxs["b"]
for line in data.replace(";", ",").splitlines():
g, line = line.removeprefix("Game").split(":")
d = Counter()
for n_c in line.split(","):
n, c = n_c.split()
d[c[0]] = max(d[c[0]], int(n))
a += int(g) * (d <= bag)
b += prod(d.values())

print("answer_a:", a)
print("answer_b:", b)

0 comments on commit 790a1c8

Please sign in to comment.