2023/day01: Complete!
parent
b58397645e
commit
0c17e66cfd
|
@ -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
|
|
@ -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))
|
|
@ -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
|
|
@ -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"
|
Loading…
Reference in New Issue