2023-day1

This commit is contained in:
Mylloon 2023-12-02 04:02:42 +01:00
commit 56a5368ade
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 1048 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.vscode/

View file

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

View file

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

1000
2023/day1/input.txt Normal file

File diff suppressed because it is too large Load diff

36
2023/day1/main.py Normal file
View file

@ -0,0 +1,36 @@
import re
def part1(file):
acc = []
with open(file) as f:
for line in f:
digits = [match for match in re.findall(r"\d", line)]
acc.append(int(f"{digits[0]}{digits[-1]}"))
return sum(acc)
def part2(file):
numbers = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
acc = []
with open(file) as f:
for line in f:
line = re.sub(
"|".join(f"(?=({number}))" for number in numbers),
lambda match: str(
numbers.index([i for i in match.groups() if i is not None][0]) + 1
),
line,
)
digits = [match for match in re.findall(r"\d", line)]
acc.append(int(f"{digits[0]}{digits[-1]}"))
return sum(acc)
if __name__ == "__main__":
assert part1("example_part1.txt") == 142
print(part1("input.txt"))
assert part2("example_part2.txt") == 281
print(part2("input.txt"))