Skip to content

Instantly share code, notes, and snippets.

@asunar
Last active November 27, 2024 23:50
Show Gist options
  • Select an option

  • Save asunar/f85c104041f7843ad88388a5abed9954 to your computer and use it in GitHub Desktop.

Select an option

Save asunar/f85c104041f7843ad88388a5abed9954 to your computer and use it in GitHub Desktop.
from aocd import get_data
inp=get_data(day=1, year=2023)
lines=inp.splitlines()
def collectNumberWords(line):
numberPairs = { "one" : 1, "two" : 2, "three" : 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9 }
indexNumberList = []
for x in numbers:
if x in line:
lastIndex = line.rindex(x)
firstIndex = line.index(x)
indexNumberList.append((firstIndex, numberPairs[x]))
if firstIndex != lastIndex: indexNumberList.append((lastIndex, numberPairs[x]))
return indexNumberList
def collectDigits(line):
indexNumberList = []
for i, o in enumerate(line):
if o.isdigit():
indexNumberList.append((i, int(o)))
return indexNumberList
def getRealNumber(line):
finalList = collectNumberWords(line) + collectDigits(line)
finalList.sort()
first = finalList[0][1]
last = finalList[-1][1]
return int(str(first)+str(last))
# lines = [
# 'two1nine',
# 'eightwothree',
# 'abcone2threexyz',
# 'xtwone3four',
# '4nineeightseven2',
# 'zoneight234',
# '7pqrstsixteen'
# ]
res = 0
for o in lines:
res += getRealNumber(o)
res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment