2023 day 4 but 3x faster
This commit is contained in:
parent
a9fc4c9e60
commit
8cbfa778c2
9 changed files with 3168 additions and 5 deletions
1000
2023/01/cast.txt
Normal file
1000
2023/01/cast.txt
Normal file
File diff suppressed because it is too large
Load diff
1000
2023/01/filtered.txt
Normal file
1000
2023/01/filtered.txt
Normal file
File diff suppressed because it is too large
Load diff
1000
2023/01/input.txt
Normal file
1000
2023/01/input.txt
Normal file
File diff suppressed because it is too large
Load diff
15
2023/01/part1.py
Normal file
15
2023/01/part1.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
path = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
nums = []
|
||||
|
||||
with open(path+'/filtered.txt', 'r') as input:
|
||||
for text in input.readlines():
|
||||
a = int(text[0])
|
||||
b = int(text[-2])
|
||||
nums.append((10*a) + b)
|
||||
|
||||
print(nums)
|
||||
print(sum(nums))
|
58
2023/01/part2.py
Normal file
58
2023/01/part2.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# 52746 too low
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
path = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
nums = []
|
||||
|
||||
def toInt(txt: str):
|
||||
try:
|
||||
num = int(txt)
|
||||
except:
|
||||
if txt == 'one':
|
||||
num = 1
|
||||
elif txt == 'two':
|
||||
num = 2
|
||||
elif txt == 'three':
|
||||
num = 3
|
||||
elif txt == 'four':
|
||||
num = 4
|
||||
elif txt == 'five':
|
||||
num = 5
|
||||
elif txt == 'six':
|
||||
num = 6
|
||||
elif txt == 'seven':
|
||||
num = 7
|
||||
elif txt == 'eight':
|
||||
num = 8
|
||||
elif txt == 'nine':
|
||||
num = 9
|
||||
finally:
|
||||
return num
|
||||
|
||||
with open(path+'/input.txt', 'r') as input:
|
||||
for text in input.readlines():
|
||||
text = text.strip()
|
||||
|
||||
print('original:',text)
|
||||
|
||||
left = re.findall(r"(?=(one|two|three|four|five|six|seven|eight|nine|1|2|3|4|5|6|7|8|9))", text)
|
||||
|
||||
print('search:',left)
|
||||
|
||||
a = toInt(left[0])
|
||||
b = toInt(left[-1])
|
||||
|
||||
print(a, b)
|
||||
|
||||
total = (10*a) + b
|
||||
|
||||
print('total:',total)
|
||||
|
||||
nums.append(total)
|
||||
|
||||
print(sum(nums))
|
Loading…
Add table
Add a link
Reference in a new issue