Unable to load image

r/drama Advent of Code S02E01 - Official DISCUSSION Thread

Ok you Python-using heathens. What did you think for difficulty this time around?

Post your coding atrocities below.

For those of you who missed the early signups. Feel free to join the leaderboard late (though rankings are based on time).

42
Jump in the discussion.

No email address required.

Ugly but it works.

# Part One
with open("1.txt") as infile:
   data = infile.read()

total = 0
for line in data.splitlines():
   digits = [c for c in line if c.isdigit()]
   v = int(digits[0]+ digits[-1])
   total += v
print(total)

# Part Two
import re

words = ['zero','one','two','three','four','five','six','seven','eight','nine']
ws = '|'.join(words)
pat = re.compile(fr'(?=({ws}|\d))')
total = 0
for line in data.splitlines():
   g = pat.findall(line)
   a, b = g[0], g[-1]
   v = str(a if a.isdigit() else words.index(a)) + str(b if b.isdigit() else words.index(b))
   total += int(v)
print(total)
Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.