4 ms·
I think it's tricky for those of us trying to be a bit too clever. Certainly it's designed to catch out the obvious replacement strategy. However the 'naive'
by kristjansson 3y ago
I think it's tricky for those of us trying to be a bit too clever. Certainly it's designed to catch out the obvious replacement strategy.
However the 'naive' solution still feels quite nice. Some python, at the risk of sharing spoilers:
fixes = { "seven":"7", "two":"2", "one":"1", "nine":"9", "eight":"8", "three":"3", "four":"4", "five":"5", "six":"6" }
cands = set(fixes.keys()) | set(fixes.values())
total = 0
for line in Path("input/1.txt").read_text().splitlines():
matches = list(filter(lambda k: k in line, cands))
first, *_ = sorted(matches, key=line.index)
*_, last = sorted(matches, key=line.rindex)
total += int(fixes.get(first, first) + fixes.get(last, last))
print(total)