From 6c9fd37162aa4a2d92070e92caf5021397c13532 Mon Sep 17 00:00:00 2001 From: wim glenn Date: Fri, 1 Dec 2023 02:21:33 -0600 Subject: [PATCH] day 01 --- .github/workflows/publish.yml | 2 +- .github/workflows/tests.yml | 25 ++++++++++++------------- aoc_wim/aoc2023/__init__.py | 0 aoc_wim/aoc2023/q01.py | 20 ++++++++++++++++++++ aoc_wim/util.py | 7 ++++++- tests/2023/01/a.txt | 6 ++++++ tests/2023/01/b.txt | 9 +++++++++ 7 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 aoc_wim/aoc2023/__init__.py create mode 100644 aoc_wim/aoc2023/q01.py create mode 100644 tests/2023/01/a.txt create mode 100644 tests/2023/01/b.txt diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index cb4fac3a..44550664 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -11,7 +11,7 @@ jobs: - uses: "actions/checkout@v3" - uses: "actions/setup-python@v4" with: - python-version: "3.11" + python-version: "3.12" architecture: x64 cache: "pip" cache-dependency-path: "pyproject.toml" diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1d28c5a8..0497c2f3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,27 +7,26 @@ on: jobs: tests: - name: "Python ${{ matrix.python-version }} on ${{ matrix.os }}" + name: Python ${{ matrix.python-version }} on ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, macos-latest, windows-latest] - python-version: ["3.11"] + python-version: ["3.12"] steps: - - uses: "actions/checkout@v3" - - uses: "actions/setup-python@v4" + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 with: - python-version: "${{ matrix.python-version }}" + python-version: ${{ matrix.python-version }} architecture: x64 - cache: "pip" - cache-dependency-path: "pyproject.toml" - - name: "Install" + cache: pip + cache-dependency-path: pyproject.toml + - name: Install run: | python -VV - python -m pip install -U pip setuptools wheel - python -m pip install pytest>=7rc1 pytest-socket --editable . - python -m pip freeze --all - - name: "Run tests for ${{ matrix.python-version }} on ${{ matrix.os }}" - run: python -m pytest -v --durations=10 --disable-socket + python -m pip install -q pytest>=7 pytest-socket --editable . + python -m pip list + - name: Run tests for ${{ matrix.python-version }} on ${{ matrix.os }} + run: pytest -v --durations=10 --disable-socket diff --git a/aoc_wim/aoc2023/__init__.py b/aoc_wim/aoc2023/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/aoc_wim/aoc2023/q01.py b/aoc_wim/aoc2023/q01.py new file mode 100644 index 00000000..d9ba2c9c --- /dev/null +++ b/aoc_wim/aoc2023/q01.py @@ -0,0 +1,20 @@ +""" +--- Day 1: Trebuchet?! --- +https://adventofcode.com/2023/day/1 +""" +import regex as re +from aocd import data + +ns = "one, two, three, four, five, six, seven, eight, nine".split(", ") +d = dict(zip(ns, "123456789")) +a = b = 0 +pat_a = re.compile(r"\d") +pat_b = re.compile(rf"(\d|{'|'.join(ns)})") +for line in data.splitlines(): + nums_a = [int(x) for x in pat_a.findall(line)] or [0] + nums_b = [int(d.get(x, x)) for x in pat_b.findall(line, overlapped=True)] or [0] + a += nums_a[0] * 10 + nums_a[-1] + b += nums_b[0] * 10 + nums_b[-1] + +print("answer_a:", a) +print("answer_b:", b) diff --git a/aoc_wim/util.py b/aoc_wim/util.py index 7d954e51..c6bc8207 100644 --- a/aoc_wim/util.py +++ b/aoc_wim/util.py @@ -132,6 +132,11 @@ def start(): print(data) set_docstrings([src]) puzzle = Puzzle(year, day) + [example] = puzzle.examples if test.read_text() == "\n-\n-\n": - test.write_text("{}\n-\n-\n".format(puzzle.example_data)) + test.write_text(dedent(f"""\ + {example.input_data} + {example.answer_a or "-"} + {example.answer_b or "-"} + """)) webbrowser.open(puzzle.url) diff --git a/tests/2023/01/a.txt b/tests/2023/01/a.txt new file mode 100644 index 00000000..cc111395 --- /dev/null +++ b/tests/2023/01/a.txt @@ -0,0 +1,6 @@ +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet +142 +- diff --git a/tests/2023/01/b.txt b/tests/2023/01/b.txt new file mode 100644 index 00000000..9ce407cb --- /dev/null +++ b/tests/2023/01/b.txt @@ -0,0 +1,9 @@ +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen +- +281