2023/day01: Complete!

main
earnest ma 2023-12-01 14:18:05 -05:00
parent b58397645e
commit 0c17e66cfd
Signed by: earnest ma
GPG Key ID: A343F43342EB6E2A
4 changed files with 101 additions and 0 deletions

12
2023/Justfile Normal file
View File

@ -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

61
2023/day01.py Normal file
View File

@ -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))

13
2023/day01_test.py Normal file
View File

@ -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

15
2023/pyproject.toml Normal file
View File

@ -0,0 +1,15 @@
[tool.poetry]
name = "2023"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
pytest = "^7.4.3"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"