Solve day 1
This commit is contained in:
19
01/__main__.py
Normal file
19
01/__main__.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from puzzle import solve_pt_1, solve_pt_2
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def main():
|
||||
input = (
|
||||
Path(__file__)
|
||||
.parent.joinpath("calibration_document.txt")
|
||||
.read_text()
|
||||
.strip()
|
||||
.split("\n")
|
||||
)
|
||||
print("Calibration Numbers")
|
||||
print("Part 1 - ", solve_pt_1(input))
|
||||
print("Part 2 - ", solve_pt_2(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
1000
01/calibration_document.txt
Normal file
1000
01/calibration_document.txt
Normal file
File diff suppressed because it is too large
Load Diff
67
01/puzzle.py
Normal file
67
01/puzzle.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import re
|
||||
|
||||
pt_1_first_digit_pattern = re.compile("([0-9])")
|
||||
pt_1_last_digit_pattern = pt_1_first_digit_pattern
|
||||
pt_1_symbol_map = {str(n): str(n) for n in range(10)}
|
||||
|
||||
pt_2_first_digit_pattern = re.compile(
|
||||
"([0-9]|one|two|three|four|five|six|seven|eight|nine|zero)"
|
||||
)
|
||||
pt_2_last_digit_pattern = re.compile(
|
||||
"([0-9]|eno|owt|eerht|ruof|evif|xis|neves|thgie|enin|orez)"
|
||||
)
|
||||
pt_2_symbol_map = {
|
||||
**pt_1_symbol_map,
|
||||
"one": "1",
|
||||
"two": "2",
|
||||
"three": "3",
|
||||
"four": "4",
|
||||
"five": "5",
|
||||
"six": "6",
|
||||
"seven": "7",
|
||||
"eight": "8",
|
||||
"nine": "9",
|
||||
"zero": "0",
|
||||
}
|
||||
|
||||
|
||||
def _solve(
|
||||
input: list[str],
|
||||
first_digit_pattern: re.Pattern,
|
||||
last_digit_pattern: re.Pattern,
|
||||
symbol_map: dict[str, str],
|
||||
) -> int:
|
||||
total = 0
|
||||
|
||||
for line in input:
|
||||
first_digit_match = first_digit_pattern.search(line)
|
||||
last_digit_match = last_digit_pattern.search(line[::-1])
|
||||
|
||||
if not first_digit_match or not last_digit_match:
|
||||
raise ValueError(f'Line "{line}" calibration document contains no digits.')
|
||||
|
||||
first_digit = symbol_map[first_digit_match.group(1)]
|
||||
last_digit = symbol_map[last_digit_match.group(1)[::-1]]
|
||||
|
||||
line_calibration_number = int(first_digit + last_digit)
|
||||
total += line_calibration_number
|
||||
|
||||
return total
|
||||
|
||||
|
||||
def solve_pt_1(input: list[str]) -> int:
|
||||
return _solve(
|
||||
input,
|
||||
pt_1_first_digit_pattern,
|
||||
pt_1_first_digit_pattern,
|
||||
pt_1_symbol_map,
|
||||
)
|
||||
|
||||
|
||||
def solve_pt_2(input: list[str]) -> int:
|
||||
return _solve(
|
||||
input,
|
||||
pt_2_first_digit_pattern,
|
||||
pt_2_last_digit_pattern,
|
||||
pt_2_symbol_map,
|
||||
)
|
37
01/puzzle_test.py
Normal file
37
01/puzzle_test.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from puzzle import (
|
||||
pt_1_first_digit_pattern,
|
||||
pt_1_last_digit_pattern,
|
||||
solve_pt_1,
|
||||
solve_pt_2,
|
||||
)
|
||||
|
||||
|
||||
def test_solve_pt_1():
|
||||
assert (
|
||||
solve_pt_1(
|
||||
[
|
||||
"1abc2",
|
||||
"pqr3stu8vwx",
|
||||
"a1b2c3d4e5f",
|
||||
"treb7uchet",
|
||||
]
|
||||
)
|
||||
== 142
|
||||
)
|
||||
|
||||
|
||||
def test_solve_pt_2():
|
||||
assert (
|
||||
solve_pt_2(
|
||||
[
|
||||
"two1nine",
|
||||
"eightwothree",
|
||||
"abcone2threexyz",
|
||||
"xtwone3four",
|
||||
"4nineeightseven2",
|
||||
"zoneight234",
|
||||
"7pqrstsixteen",
|
||||
]
|
||||
)
|
||||
== 281
|
||||
)
|
Reference in New Issue
Block a user