First day finish
This commit is contained in:
parent
c7a4428965
commit
cd48a3bfec
@ -1,20 +1,55 @@
|
||||
sum = 0
|
||||
|
||||
nums = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
||||
nums = {"one": 1,
|
||||
"two": 2,
|
||||
"three": 3,
|
||||
"four": 4,
|
||||
"five": 5,
|
||||
"six": 6,
|
||||
"seven": 7,
|
||||
"eight": 8,
|
||||
"nine": 9}
|
||||
|
||||
with open("test.txt") as f:
|
||||
# with open("input.txt") as f:
|
||||
# for curr in f.readlines():
|
||||
# for n in nums:
|
||||
# print(curr[:-1])
|
||||
# num = []
|
||||
# for c in range(len(curr)):
|
||||
# if curr[c].isnumeric():
|
||||
# num.append(curr[c])
|
||||
# break
|
||||
|
||||
# for c in range(len(curr), 0, -1):
|
||||
# if curr[c-1].isnumeric():
|
||||
# num.append(curr[c-1])
|
||||
# break
|
||||
# print(int("".join(num)))
|
||||
# sum += int("".join(num))
|
||||
# print(sum)
|
||||
|
||||
with open("input.txt") as f:
|
||||
for curr in f.readlines():
|
||||
print(curr[:-1])
|
||||
num = []
|
||||
for c in range(len(curr)):
|
||||
if curr[c].isnumeric():
|
||||
num.append(curr[c])
|
||||
break
|
||||
|
||||
for c in range(len(curr), 0, -1):
|
||||
if curr[c-1].isnumeric():
|
||||
num.append(curr[c-1])
|
||||
break
|
||||
print(int("".join(num)))
|
||||
sum += int("".join(num))
|
||||
num_pos = { 1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [], 9: [], }
|
||||
for k, v in enumerate(curr):
|
||||
if v.isnumeric():
|
||||
num_pos[int(v)].append(k)
|
||||
for k, v in nums.items():
|
||||
foundL = curr.find(k)
|
||||
foundR = curr.rfind(k)
|
||||
foundR = -1 if foundL == foundR else foundR
|
||||
if foundL >= 0:
|
||||
num_pos[v].append(foundL)
|
||||
if foundR >= 0:
|
||||
num_pos[v].append(foundR)
|
||||
minpos = (999, -1)
|
||||
maxpos = (-1, -1)
|
||||
for k, v in num_pos.items():
|
||||
for e in v:
|
||||
minpos = (min(minpos[0], e), k) if min(minpos[0], e) != minpos[0] else minpos
|
||||
maxpos = (max(maxpos[0], e), k) if max(maxpos[0], e) != maxpos[0] else maxpos
|
||||
print(num_pos)
|
||||
print(minpos, maxpos)
|
||||
print(int("".join([str(minpos[1]), str(maxpos[1])])))
|
||||
sum += int("".join([str(minpos[1]), str(maxpos[1])]))
|
||||
print(sum)
|
||||
|
7
2023/d1/test.txt
Normal file
7
2023/d1/test.txt
Normal file
@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
Loading…
Reference in New Issue
Block a user