diff --git a/2023/Justfile b/2023/Justfile new file mode 100644 index 0000000..fe6664c --- /dev/null +++ b/2023/Justfile @@ -0,0 +1,12 @@ + +# check +default: check + +# show this help (just --list) +help: + just --list + +# lint and run tests +check: + poetry run black . --check + poetry run pytest diff --git a/2023/day01.py b/2023/day01.py new file mode 100644 index 0000000..414b008 --- /dev/null +++ b/2023/day01.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 + +from typing import TextIO +import re + +# Part 2: Overlaps +a = { + "one": "o1e", + "two": "t2o", + "three": "t3e", + "four": "f4r", + "five": "f5e", + "six": "s6x", + "seven": "s7n", + "eight": "e8t", + "nine": "n9e", + "ten": "t10e", +} + + +def calc(line: str) -> int: + e = re.findall(r"\d", line) + + try: + first, *_, last = e + except ValueError: + first, last = e[0], e[0] + + return int("".join([first, last])) + + +def part1(f: TextIO) -> int: + values: int = 0 + + for line in f: + line = line.strip() + + values += calc(line) + + return values + + +def part2(f: TextIO) -> int: + values: int = 0 + + for line in f: + line = line.strip() + + for w, r in a.items(): + line = line.replace(w, r) + + values += calc(line) + + return values + + +if __name__ == "__main__": + with open("day01.txt") as f: + print(part1(f)) + with open("day01.txt") as f: + print(part2(f)) diff --git a/2023/day01_test.py b/2023/day01_test.py new file mode 100644 index 0000000..0cb4022 --- /dev/null +++ b/2023/day01_test.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +from day01 import * + + +def test_part1() -> None: + with open("day01.txt") as p: + assert part1(p) == 54601 + + +def test_part2() -> None: + with open("day01.txt") as p: + assert part2(p) == 54078 diff --git a/2023/pyproject.toml b/2023/pyproject.toml new file mode 100644 index 0000000..ba8fdc4 --- /dev/null +++ b/2023/pyproject.toml @@ -0,0 +1,15 @@ +[tool.poetry] +name = "2023" +version = "0.1.0" +description = "" +authors = ["Your Name "] +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.11" +pytest = "^7.4.3" + + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api"