This commit is contained in:
IamTheFij 2023-12-02 10:46:14 -08:00
parent a4f9512e01
commit 8f2c45d1bf
4 changed files with 1086 additions and 0 deletions

1000
d01/input.txt Normal file

File diff suppressed because it is too large Load Diff

75
d01/main.py Normal file
View File

@ -0,0 +1,75 @@
from collections.abc import Iterable
from pathlib import Path
def find_all(needle: str, haystack: str) -> Iterable[int]:
start = 0
index = haystack.find(needle, start)
while index >= 0:
yield index
start = index + 1
index = haystack.find(needle, start)
def numberfy(s: str) -> str:
nums = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
}
result = [""] * len(s)
for num_s, num_i in nums.items():
for index in find_all(num_s, s):
result[index] = str(num_i)
for i, c in enumerate(s):
if c.isnumeric():
result[i] = c
return "".join(result)
def part1(path: Path) -> int:
total = 0
with path.open() as f:
for line in f:
first: str = ""
last: str = ""
for c in line:
if not c.isnumeric():
continue
if not first:
first = c
continue
last = c
if not last:
last = first
line_total = int(first+last)
total += line_total
return total
def part2(path: Path) -> int:
total = 0
with path.open() as f:
for line in f:
line_nums = numberfy(line)
line_total = int(line_nums[0]+line_nums[-1])
total += line_total
return total
if __name__ == "__main__":
input = Path("input.txt")
print("part 1", part1(input))
print("part 2", part2(input))

4
d01/sample.txt Normal file
View File

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

7
d01/sample2.txt Normal file
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen