From d60a100c55f5a2c384a6a26f4c55a2557c5814c4 Mon Sep 17 00:00:00 2001 From: Christopher Bohn <bohn@unl.edu> Date: Sat, 2 Dec 2023 07:38:48 -0600 Subject: [PATCH] Cleaned up 2023 Day 01 --- 2023/python/Day01.py | 71 +++++++++++++++++++------------------------- scaffolding/DayXX | 6 ++-- 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/2023/python/Day01.py b/2023/python/Day01.py index f751d71..47261da 100644 --- a/2023/python/Day01.py +++ b/2023/python/Day01.py @@ -5,47 +5,29 @@ from ImportData import import_data day: int = 1 -# sample_data: List[str] = ''' -# 1abc2 -# pqr3stu8vwx -# a1b2c3d4e5f -# treb7uchet -# '''.split('\n')[1:-1] +sample_data: List[List[str]] = [ + ''' + 1abc2 + pqr3stu8vwx + a1b2c3d4e5f + treb7uchet + '''.split('\n')[1:-1] + , + ''' + two1nine + eightwothree + abcone2threexyz + xtwone3four + 4nineeightseven2 + zoneight234 + 7pqrstsixteen + '''.split('\n')[1:-1] +] -sample_data: List[str] = ''' -two1nine -eightwothree -abcone2threexyz -xtwone3four -4nineeightseven2 -zoneight234 -7pqrstsixteen -'''.split('\n')[1:-1] - -data_structure: type = List[str] +data_structure: type = List[int] 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] = [] @@ -58,7 +40,7 @@ def part2(data: data_structure) -> int: digit = int(character) else: for digit_name in digit_names: - if datum[index:index+len(digit_name)] == digit_name: + 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 @@ -67,11 +49,20 @@ def part2(data: data_structure) -> int: first_digit = 0 last_digit = 0 values.append(10 * first_digit + last_digit) - return functools.reduce(lambda a, b: a + b, values) + return values + + +def part1(data: data_structure) -> int: + return functools.reduce(lambda a, b: a + b, data) + + +def part2(data: data_structure) -> int: + return functools.reduce(lambda a, b: a + b, data) if __name__ == '__main__': production_ready = True - raw_data = import_data(day) if production_ready else sample_data + raw_data = import_data(day) if production_ready else sample_data[0] print(part1(parse_data(raw_data))) + raw_data = import_data(day) if production_ready else sample_data[1] print(part2(parse_data(raw_data))) diff --git a/scaffolding/DayXX b/scaffolding/DayXX index 9740ec2..f1d1b16 100644 --- a/scaffolding/DayXX +++ b/scaffolding/DayXX @@ -4,9 +4,9 @@ from ImportData import import_data day: int = X -sample_data: List[str] = [ - -] +sample_data: List[str] = ''' +... +'''.split('\n')[1:-1] data_structure: type = Y -- GitLab