diff --git a/src/2024/01/2024_01.py b/src/2024/01/2024_01.py index ddd6515..d32bcbf 100644 --- a/src/2024/01/2024_01.py +++ b/src/2024/01/2024_01.py @@ -1,16 +1,36 @@ +import re +from collections import Counter +from typing import List, Tuple + from src.utils.data import load_data from src.utils.submission import submit_or_print def main(debug: bool) -> None: input_data = load_data(debug) + first, second = parse_lists(input_data) + + first = sorted(first) + second = sorted(second) + result_part1 = sum([abs(x[0] - x[1]) for x in zip(first, second)]) - result_part1 = None - result_part2 = None + counter = Counter(second) + result_part2 = sum([f * counter[f] for f in first]) submit_or_print(result_part1, result_part2, debug) +def parse_lists(input_data: str) -> Tuple[List[int], List[int]]: + first = [] + second = [] + for l in input_data.splitlines(): + spl = list(map(int, re.findall(r"\d+", l))) + assert len(spl) == 2 + first.append(int(spl[0])) + second.append(int(spl[1])) + return first, second + + if __name__ == "__main__": debug_mode = True # debug_mode = False diff --git a/src/2024/01/sample_input.txt b/src/2024/01/sample_input.txt index 30404ce..571a6ba 100644 --- a/src/2024/01/sample_input.txt +++ b/src/2024/01/sample_input.txt @@ -1 +1,6 @@ -TODO \ No newline at end of file +3 1 +23 2 +4 5 +3 3 +0 9 +11 99 \ No newline at end of file diff --git a/src/2024/02/2024_02.py b/src/2024/02/2024_02.py new file mode 100644 index 0000000..0c704aa --- /dev/null +++ b/src/2024/02/2024_02.py @@ -0,0 +1,63 @@ +from dataclasses import dataclass +from typing import List + +from src.utils.data import load_data +from src.utils.submission import submit_or_print + + +@dataclass +class Report: + levels: List[int] + + +def main(debug: bool) -> None: + input_data = load_data(debug) + + reports = parse_input(input_data) + + result_part1 = 0 + for report in reports: + if safe(report.levels): + result_part1 += 1 + + result_part2 = 0 + for report in reports: + has_safe_version = False + for excluded_index in range(len(report.levels)): + levels_with_exclusion = ( + report.levels[:excluded_index] + report.levels[excluded_index + 1 :] + ) + if safe(levels_with_exclusion): + has_safe_version = True + break + if has_safe_version: + result_part2 += 1 + + submit_or_print(result_part1, result_part2, debug) + + +def parse_input(input_data: str) -> List[Report]: + reports = [] + for line in input_data.splitlines(): + levels = [int(s) for s in line.split(" ")] + reports.append(Report(levels)) + return reports + + +def safe(levels: List[int]) -> bool: + pairs = list(zip(levels, levels[1:])) + diffs = list(map(lambda p: p[0] - p[1], pairs)) + signs = {d > 0 for d in diffs} + if len(signs) > 1: + return False + if any([abs(diff) > 3 for diff in diffs]): + return False + if 0 in diffs: + return False + return True + + +if __name__ == "__main__": + debug_mode = True + # debug_mode = False + main(debug_mode) diff --git a/src/2024/02/sample_input.txt b/src/2024/02/sample_input.txt new file mode 100644 index 0000000..217fa67 --- /dev/null +++ b/src/2024/02/sample_input.txt @@ -0,0 +1,8 @@ +1 2 3 4 5 +1 4 5 6 7 +7 6 5 4 1 +5 4 3 2 1 +1 1 2 3 4 +4 4 3 2 1 +1 10 2 3 4 +10 4 3 2 1 \ No newline at end of file