2023-day1
This commit is contained in:
commit
56a5368ade
5 changed files with 1048 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.vscode/
|
4
2023/day1/example_part1.txt
Normal file
4
2023/day1/example_part1.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
1abc2
|
||||||
|
pqr3stu8vwx
|
||||||
|
a1b2c3d4e5f
|
||||||
|
treb7uchet
|
7
2023/day1/example_part2.txt
Normal file
7
2023/day1/example_part2.txt
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
two1nine
|
||||||
|
eightwothree
|
||||||
|
abcone2threexyz
|
||||||
|
xtwone3four
|
||||||
|
4nineeightseven2
|
||||||
|
zoneight234
|
||||||
|
7pqrstsixteen
|
1000
2023/day1/input.txt
Normal file
1000
2023/day1/input.txt
Normal file
File diff suppressed because it is too large
Load diff
36
2023/day1/main.py
Normal file
36
2023/day1/main.py
Normal 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"))
|
Loading…
Reference in a new issue