adventofcode/2023/d1/d1.py
Debucquoy Anthony cd48a3bfec First day finish
2023-12-02 12:29:19 +01:00

56 lines
1.7 KiB
Python

sum = 0
nums = {"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9}
# 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():
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)