-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ted Cassirer
committed
Dec 2, 2024
1 parent
010e9e5
commit f70e9db
Showing
7 changed files
with
100 additions
and
160 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from collections import Counter | ||
|
||
|
||
def _parse_lists(data: str) -> tuple[list[int], list[int]]: | ||
left_list, right_list = [], [] | ||
for line in data.splitlines(): | ||
left, right = line.split(" ") | ||
left_list.append(int(left)) | ||
right_list.append(int(right)) | ||
return left_list, right_list | ||
|
||
|
||
def part_a(data: str) -> int: | ||
left, right = _parse_lists(data) | ||
left.sort() | ||
right.sort() | ||
total_dist = 0 | ||
for l, r in zip(left, right): | ||
total_dist += abs(l - r) | ||
return total_dist | ||
|
||
|
||
def part_b(data: str) -> int: | ||
left, right = _parse_lists(data) | ||
left_count, right_count = Counter(left), Counter(right) | ||
return sum(k * left_count[k] * right_count[k] for k in left_count.keys()) | ||
|
||
|
||
if __name__ == "__main__": | ||
from aoc_cas.util import solve_with_examples | ||
|
||
solve_with_examples(year=2024, day=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import typing as t | ||
|
||
|
||
def _parse_levels(data: str) -> t.Iterator[list[int]]: | ||
for line in data.splitlines(): | ||
level = [int(n) for n in line.split(" ")] | ||
yield level | ||
|
||
|
||
def _is_increasing(level: list[int]) -> bool: | ||
return all(level[i] < level[i + 1] for i in range(len(level) - 1)) | ||
|
||
|
||
def _is_decreasing(level: list[int]) -> bool: | ||
return all(level[i] > level[i + 1] for i in range(len(level) - 1)) | ||
|
||
|
||
def _max_distance(level: list[int], k: int) -> int: | ||
return all(abs(level[i + 1] - level[i]) <= k for i in range(len(level) - 1)) | ||
|
||
|
||
def _is_safe(level: list[int]) -> bool: | ||
return (_is_increasing(level) or _is_decreasing(level)) and _max_distance(level, 3) | ||
|
||
|
||
def part_a(data: str) -> int: | ||
safe_levels = 0 | ||
for level in _parse_levels(data): | ||
if _is_safe(level): | ||
safe_levels += 1 | ||
return safe_levels | ||
|
||
|
||
def part_b(data: str) -> int: | ||
safe_levels = 0 | ||
for level in _parse_levels(data): | ||
if _is_safe(level): | ||
safe_levels += 1 | ||
else: | ||
for i in range(len(level)): | ||
fixed = level[:i] + level[i + 1 :] | ||
if _is_safe(fixed): | ||
safe_levels += 1 | ||
break | ||
return safe_levels | ||
|
||
|
||
if __name__ == "__main__": | ||
from aoc_cas.util import solve_with_examples | ||
|
||
solve_with_examples(year=2024, day=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
3 4 | ||
4 3 | ||
2 5 | ||
1 3 | ||
3 9 | ||
3 3 | ||
11 | ||
31 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
7 6 4 2 1 | ||
1 2 7 8 9 | ||
9 7 6 2 1 | ||
1 3 2 4 5 | ||
8 6 4 4 1 | ||
1 3 6 7 9 | ||
2 | ||
4 |