Skip to content

Commit

Permalink
Add day 1 and 2 from 2024 (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkopec87 authored Dec 2, 2024
1 parent 54ce74b commit 25661d5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
24 changes: 22 additions & 2 deletions src/2024/01/2024_01.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/2024/01/sample_input.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
TODO
3 1
23 2
4 5
3 3
0 9
11 99
63 changes: 63 additions & 0 deletions src/2024/02/2024_02.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions src/2024/02/sample_input.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 25661d5

Please sign in to comment.