Skip to content
Snippets Groups Projects
Verified Commit e4b8d8e7 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Completed 2023 Day 01

parent f0f55486
No related branches found
No related tags found
No related merge requests found
import functools
from typing import List, Optional, Dict
from ImportData import import_data
day: int = 1
# sample_data: List[str] = '''
# 1abc2
# pqr3stu8vwx
# a1b2c3d4e5f
# treb7uchet
# '''.split('\n')[1:-1]
sample_data: List[str] = '''
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
'''.split('\n')[1:-1]
data_structure: type = List[str]
def parse_data(data: List[str]) -> data_structure:
return data
def part1(data: data_structure) -> int:
values: List[int] = []
for datum in data:
first_digit: Optional[int] = None
last_digit: int = 0
for character in datum:
if character.isdigit():
first_digit = int(character) if first_digit is None else first_digit
last_digit = int(character)
if first_digit is None:
first_digit = 0
last_digit = 0
values.append(10 * first_digit + last_digit)
return functools.reduce(lambda a, b: a + b, values)
def part2(data: data_structure) -> int:
digit_names: Dict[str, int] = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5,
'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'zero': 0}
values: List[int] = []
for datum in data:
first_digit: Optional[int] = None
last_digit: int = 0
for index, character in enumerate(datum):
digit: Optional[int] = None
if character.isdigit():
digit = int(character)
else:
for digit_name in digit_names:
if datum[index:index+len(digit_name)] == digit_name:
digit = digit_names[digit_name]
if digit is not None:
first_digit = digit if first_digit is None else first_digit
last_digit = digit
if first_digit is None:
first_digit = 0
last_digit = 0
values.append(10 * first_digit + last_digit)
return functools.reduce(lambda a, b: a + b, values)
if __name__ == '__main__':
production_ready = True
raw_data = import_data(day) if production_ready else sample_data
print(part1(parse_data(raw_data)))
print(part2(parse_data(raw_data)))
../../scaffolding/DayXX
\ No newline at end of file
from typing import List
from ImportData import import_data
day: int = X
sample_data: List[str] = [
]
data_structure: type = Y
def parse_data(data: List[str]) -> data_structure:
return None
def part1(data: data_structure) -> int:
return -1
def part2(data: data_structure) -> int:
return -1
if __name__ == '__main__':
production_ready = False
raw_data = import_data(day) if production_ready else sample_data
print(part1(parse_data(raw_data)))
print(part2(parse_data(raw_data)))
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment