diff options
author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2023-12-02 11:09:42 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2023-12-02 11:09:42 +0000 |
commit | 015d98ddddd200b2ff28a8d6fb0f06cf282c050e (patch) | |
tree | 9d3729d48df0cfcf3b723aa9d0467966d30842b4 | |
parent | 4ef476c0e730d625a3ac8063ed08ebe69d48cb34 (diff) | |
download | aoc2023-015d98ddddd200b2ff28a8d6fb0f06cf282c050e.tar.gz aoc2023-015d98ddddd200b2ff28a8d6fb0f06cf282c050e.tar.xz aoc2023-015d98ddddd200b2ff28a8d6fb0f06cf282c050e.zip |
day 2
-rw-r--r-- | 2.py | 32 |
1 files changed, 32 insertions, 0 deletions
@@ -0,0 +1,32 @@ +from sys import stdin +from dataclasses import dataclass +from functools import reduce +from typing import TypeAlias + + +CubeSet: TypeAlias = dict[str, int] + + +@dataclass +class Game: + id: int + subsets: list[CubeSet] + + +def parse(inp: str) -> Game: + game, subsets_str = inp.rstrip().split(": ") + _, game = game.split(" ") + subsets = list() + for s in subsets_str.split("; "): + cubes = [c.split(" ") for c in s.split(", ")] + subsets.append({c[1]: int(c[0]) for c in cubes}) + return Game(int(game), subsets) + + +games = [parse(l) for l in stdin] +limits = {"red": 12, "green": 13, "blue": 14} + +# fmt: off +print(sum(g.id for g in games if all(s[k] <= limits[k] for s in g.subsets for k in limits.keys() if k in s))) +print(sum(reduce(int.__mul__, (max(s[k] for s in g.subsets if k in s) for k in limits.keys())) for g in games)) +# fmt: on |