Optimise day 1
This commit is contained in:
parent
bd1cf6ffa0
commit
f72708ca06
1 changed files with 26 additions and 10 deletions
28
day1.py
28
day1.py
|
@ -12,6 +12,7 @@ digit_map = {
|
|||
"nine": 9,
|
||||
}
|
||||
digits = list(digit_map)
|
||||
firstletters = set(s[0] for s in digits)
|
||||
|
||||
inp = list(fileinput.input())
|
||||
|
||||
|
@ -19,13 +20,11 @@ inp = list(fileinput.input())
|
|||
# Part 1
|
||||
#
|
||||
part1 = sum(
|
||||
[
|
||||
int(
|
||||
next(x for x in y if x.isdigit())
|
||||
+ next(x for x in reversed(y) if x.isdigit())
|
||||
next(x for x in line if x.isdigit())
|
||||
+ next(x for x in reversed(line) if x.isdigit())
|
||||
)
|
||||
for y in inp
|
||||
]
|
||||
for line in inp
|
||||
)
|
||||
|
||||
print(part1)
|
||||
|
@ -39,13 +38,30 @@ for line in inp:
|
|||
newline = []
|
||||
|
||||
for i in range(len(line)):
|
||||
l = line[i:]
|
||||
if line[i].isdigit():
|
||||
newline.append(line[i])
|
||||
break
|
||||
for digit in digits:
|
||||
if line[i:].startswith(digit):
|
||||
if l.startswith(digit):
|
||||
newline.append(str(digit_map[digit]))
|
||||
break
|
||||
else:
|
||||
continue
|
||||
break
|
||||
|
||||
for i in reversed(range(len(line))):
|
||||
l = line[i:]
|
||||
if line[i].isdigit():
|
||||
newline.append(line[i])
|
||||
break
|
||||
for digit in digits:
|
||||
if l.startswith(digit):
|
||||
newline.append(str(digit_map[digit]))
|
||||
break
|
||||
else:
|
||||
continue
|
||||
break
|
||||
|
||||
part2 += int(newline[0] + newline[-1])
|
||||
|
||||
|
|
Loading…
Reference in a new issue