Solve 2023:01 "Trebuchet?!"
Turns out re methods are non-overlapping. And in true AoC manners, no provided test cases had overlaps. Luckily for me, some of the last lines in the input contained the string "oneight", so I was able to find it out quite fast. Revisions: 1) Reverse strings to find last digit 2) Use isdigit() and skip regex. 3) Use regexp with positive look-ahead.
This commit is contained in:
parent
661f18dca4
commit
aae14797ea
1 changed files with 55 additions and 0 deletions
55
2023-python/output/day_01.py
Normal file
55
2023-python/output/day_01.py
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
import re
|
||||||
|
from output import answer, puzzleinput
|
||||||
|
|
||||||
|
n = 1
|
||||||
|
title = "Trebuchet?!"
|
||||||
|
|
||||||
|
|
||||||
|
@answer(1, "Calibration values sum: {}, excluding spelled out digits")
|
||||||
|
def part_1(data):
|
||||||
|
def value(s):
|
||||||
|
s = [int(c) for c in s if c.isdigit()]
|
||||||
|
return s[0] * 10 + s[-1]
|
||||||
|
|
||||||
|
return sum(value(line) for line in data)
|
||||||
|
|
||||||
|
|
||||||
|
@answer(2, "Calibration values sum: {}, including spelled out digits")
|
||||||
|
def part_2(data):
|
||||||
|
mp = {
|
||||||
|
"one": 1,
|
||||||
|
"two": 2,
|
||||||
|
"three": 3,
|
||||||
|
"four": 4,
|
||||||
|
"five": 5,
|
||||||
|
"six": 6,
|
||||||
|
"seven": 7,
|
||||||
|
"eight": 8,
|
||||||
|
"nine": 9,
|
||||||
|
}
|
||||||
|
|
||||||
|
def value(l):
|
||||||
|
s = [
|
||||||
|
int(c) if c.isdigit() else mp[c]
|
||||||
|
for c in re.findall(
|
||||||
|
r"(?=(\d|one|two|three|four|five|six|seven|eight|nine))", l
|
||||||
|
)
|
||||||
|
]
|
||||||
|
return s[0] * 10 + s[-1]
|
||||||
|
|
||||||
|
return sum(value(line) for line in data)
|
||||||
|
|
||||||
|
|
||||||
|
@puzzleinput(n)
|
||||||
|
def parse_input(data):
|
||||||
|
return data.split()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parsed = parse_input()
|
||||||
|
|
||||||
|
a = part_1(parsed)
|
||||||
|
b = part_2(parsed)
|
||||||
|
|
||||||
|
assert a == 54634
|
||||||
|
assert b == 53855
|
||||||
Loading…
Add table
Reference in a new issue