-
Notifications
You must be signed in to change notification settings - Fork 2
/
aoc202301_replace.py
64 lines (49 loc) · 1.54 KB
/
aoc202301_replace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""AoC 1, 2023: Trebuchet?!."""
# Standard library imports
import pathlib
import sys
def parse_data(puzzle_input):
"""Parse input."""
return puzzle_input.split("\n")
def part1(data):
"""Solve part 1."""
return sum(find_calibration_value(line) for line in data)
def part2(data):
"""Solve part 2."""
for word, digit in {
"one": "o1e", # Keep leading and trailing characters to handle overlaps
"two": "t2o",
"three": "t3e",
"four": "f4r",
"five": "f5e",
"six": "s6x",
"seven": "s7n",
"eight": "e8t",
"nine": "n9e",
}.items():
data = [line.replace(word, digit) for line in data]
return sum(find_calibration_value(line) for line in data)
def find_calibration_value(line):
"""Find the calibration value of a line of text, only care about numeric characters.
## Examples:
>>> find_calibration_value("123")
13
>>> find_calibration_value("4")
44
>>> find_calibration_value("t2e8o1j")
21
>>> find_calibration_value("five6seven")
66
"""
digits = [char for char in line if char.isdigit()]
return int(digits[0] + digits[-1])
def solve(puzzle_input):
"""Solve the puzzle for the given input."""
data = parse_data(puzzle_input)
yield part1(data)
yield part2(data)
if __name__ == "__main__":
for path in sys.argv[1:]:
print(f"\n{path}:")
solutions = solve(puzzle_input=pathlib.Path(path).read_text().rstrip())
print("\n".join(str(solution) for solution in solutions))