From c80204a4e20ade4c08629aa921e9bd017c0c4fc8 Mon Sep 17 00:00:00 2001 From: wim glenn Date: Sat, 2 Dec 2023 00:41:49 -0600 Subject: [PATCH] day 02 --- .github/workflows/publish.yml | 10 +++++----- aoc_wim/aoc2023/q02.py | 28 ++++++++++++++++++++++++++++ aoc_wim/util.py | 10 +++++----- tests/2023/02/a.txt | 7 +++++++ 4 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 aoc_wim/aoc2023/q02.py create mode 100644 tests/2023/02/a.txt diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 44550664..b13787e2 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -8,14 +8,14 @@ jobs: publish: runs-on: ubuntu-latest steps: - - uses: "actions/checkout@v3" - - uses: "actions/setup-python@v4" + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 with: python-version: "3.12" architecture: x64 - cache: "pip" - cache-dependency-path: "pyproject.toml" - - name: "Create release" + cache: pip + cache-dependency-path: pyproject.toml + - name: Create release run: | python -VV python -m pip install -q -U wheel setuptools twine luddite build diff --git a/aoc_wim/aoc2023/q02.py b/aoc_wim/aoc2023/q02.py new file mode 100644 index 00000000..6b734e78 --- /dev/null +++ b/aoc_wim/aoc2023/q02.py @@ -0,0 +1,28 @@ +""" +--- Day 2: Cube Conundrum --- +https://adventofcode.com/2023/day/2 +""" +from aocd import data + +bag = dict(zip("rgb", [12, 13, 14])) +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"] + +print("answer_a:", a) +print("answer_b:", b) diff --git a/aoc_wim/util.py b/aoc_wim/util.py index c6bc8207..03353861 100644 --- a/aoc_wim/util.py +++ b/aoc_wim/util.py @@ -117,10 +117,10 @@ def start(): - - print("answer_a:", ) print("answer_b:", ) + + # from aocd import submit; submit(a) ''')) test = here.parent / "tests" / str(year) / str(day).zfill(2) / "a.txt" if not test.exists(): @@ -135,8 +135,8 @@ def start(): [example] = puzzle.examples if test.read_text() == "\n-\n-\n": test.write_text(dedent(f"""\ - {example.input_data} - {example.answer_a or "-"} - {example.answer_b or "-"} + {example.input_data} + {example.answer_a or "-"} + {example.answer_b or "-"} """)) webbrowser.open(puzzle.url) diff --git a/tests/2023/02/a.txt b/tests/2023/02/a.txt new file mode 100644 index 00000000..38663104 --- /dev/null +++ b/tests/2023/02/a.txt @@ -0,0 +1,7 @@ +Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green +Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue +Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red +Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red +Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green +8 +2286