AdventOfCode/2023/day1/part2/part2.py

42 lines
1018 B
Python
Raw Normal View History

2023-12-01 10:47:08 +01:00
#!/bin/python3
import re
import sys
input_f = sys.argv[1]
count = 0
#list of values that can be found
lst = ['one','two','three','four','five','six','seven','eight','nine','1','2','3','4','5','6','7','8','9','0']
#list to match numbers with
num = ['zero','one','two','three','four','five','six','seven','eight','nine']
#open file
with open(input_f) as file:
for line in file:
tmp = line.rstrip() #read the line
z = []
#find all matches from lst in the line
for i in lst:
for m in re.finditer(i,tmp):
#make a 2d array
z.append([m.start(),m.group(0)])
#sort the 2d array with first column as key
y=sorted(z,key=lambda z: z[0])
d1 = y[0][1]
try:
d1 = num.index(d1)
except:
pass
d2 = y[len(y)-1][1]
try:
d2 = num.index(d2)
except:
pass
count = count + int(str(d1) + str(d2))
print(count)