Day 1
This commit is contained in:
commit
bd1cf6ffa0
1 changed files with 52 additions and 0 deletions
52
day1.py
Normal file
52
day1.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
import fileinput
|
||||
|
||||
digit_map = {
|
||||
"one": 1,
|
||||
"two": 2,
|
||||
"three": 3,
|
||||
"four": 4,
|
||||
"five": 5,
|
||||
"six": 6,
|
||||
"seven": 7,
|
||||
"eight": 8,
|
||||
"nine": 9,
|
||||
}
|
||||
digits = list(digit_map)
|
||||
|
||||
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())
|
||||
)
|
||||
for y in inp
|
||||
]
|
||||
)
|
||||
|
||||
print(part1)
|
||||
|
||||
#
|
||||
# Part 2
|
||||
#
|
||||
part2 = 0
|
||||
|
||||
for line in inp:
|
||||
newline = []
|
||||
|
||||
for i in range(len(line)):
|
||||
for digit in digits:
|
||||
if line[i:].startswith(digit):
|
||||
newline.append(str(digit_map[digit]))
|
||||
break
|
||||
else:
|
||||
if line[i].isdigit():
|
||||
newline.append(line[i])
|
||||
|
||||
part2 += int(newline[0] + newline[-1])
|
||||
|
||||
print(part2)
|
Loading…
Reference in a new issue