solved 2023 day 1b and made 1a better

kwout 10 months ago
parent 5d22aae8fe
commit e4a80294c7
  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 sum = 0
for l in lines: for l in lines:
n = "".join(c for c in l if not c.isalpha()) first = ""
sum += int(n[0] + n[-1]) 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) 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