Skip to content

Commit

Permalink
Add AoC 2023 day 1 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
iandioch committed Dec 1, 2023
1 parent d6b83dd commit 8a4555f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions advent_of_code/2023/01/part2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys

def substr_from(s, i, search):
if i + len(search) > len(s):
return False
return s[i:i+len(search)] == search

DIGITS = {c: int(c) for c in '0123456789'}
def get_dig(s, i):
if s[i] in DIGITS:
return DIGITS[s[i]]
for n, num in enumerate(['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']):
if substr_from(s, i, num):
return n+1
return None


def main():
tot = 0
for line in sys.stdin.readlines():
line = line.strip()
da, db = 0, 0
for i in range(len(line)):
x = get_dig(line, i)
if x is not None:
da = x
break
for i in range(len(line)-1, -1, -1):
x = get_dig(line, i)
if x is not None:
db = x
break
tot += da*10 + db
print(tot)


if __name__ == '__main__':
main()

0 comments on commit 8a4555f

Please sign in to comment.