2023 day 1 star 1
This commit is contained in:
+1001
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,25 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
def number_with_regex(line):
|
||||||
|
matches = re.findall(r"\d", line)
|
||||||
|
return int(matches[0] + matches[-1])
|
||||||
|
|
||||||
|
def number_with_ascii(line):
|
||||||
|
first = None
|
||||||
|
for character in line:
|
||||||
|
if character >= '0' and character <= '9':
|
||||||
|
if not first:
|
||||||
|
first = character
|
||||||
|
last = character
|
||||||
|
return int(first + last)
|
||||||
|
|
||||||
|
regex_total = 0
|
||||||
|
ascii_total = 0
|
||||||
|
with open("input.txt", "r") as f:
|
||||||
|
for line in f.readlines():
|
||||||
|
line = line.strip()
|
||||||
|
regex_total += number_with_regex(line)
|
||||||
|
ascii_total += number_with_ascii(line)
|
||||||
|
|
||||||
|
print(regex_total)
|
||||||
|
print(ascii_total)
|
||||||
Reference in New Issue
Block a user