solved 2023 day 1b and made 1a better

master
kwout 10 months ago
parent f7dbd317f8
commit 09d24e0c2f
  1. 17
      2023/day1/1a.py
  2. 32
      2023/day1/1b.py

@ -3,7 +3,18 @@ lines = [i for i in open("input.txt", 'r').read().splitlines()]
sum = 0
for l in lines:
n = "".join(c for c in l if not c.isalpha())
sum += int(n[0] + n[-1])
first = ""
firstFound = False
last = ""
lastFound = False
for i in range(len(l)):
if not firstFound and l[i].isnumeric():
first = l[i]
firstFound = True
if not lastFound and l[-i-1].isnumeric():
last = l[-i-1]
lastFound = True
if firstFound and lastFound:
continue
sum += int(first + last)
print(sum)

@ -0,0 +1,32 @@
lines = [i for i in open("input.txt", 'r').read().splitlines()]
sum = 0
words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
for l in lines:
first = ""
firstFound = False
last = ""
lastFound = False
n = len(l)
for i in range(n):
if not firstFound and l[i].isnumeric():
first = l[i]
firstFound = True
if not lastFound and l[-i-1].isnumeric():
last = l[-i-1]
lastFound = True
for j in range(len(words)):
m = len(words[j])
if not firstFound and l[i:i+m] == words[j]:
first = str(j)
firstFound = True
if not lastFound and l[-(i+m):n-i] == words[j]:
last = str(j)
lastFound = True
if firstFound and lastFound:
break
s = first + last
sum += int(s)
print(sum)
Loading…
Cancel
Save