-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from marcelblijleven/2024-01
2024 01
- Loading branch information
Showing
7 changed files
with
106 additions
and
10 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -132,5 +132,5 @@ dmypy.json | |
.idea | ||
|
||
# Advent of code related | ||
.session | ||
.setup.json | ||
src/adventofcode/inputs |
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,4 @@ | ||
{ | ||
"user_agent": "github.com/your-username/your-repo", | ||
"session_cookie": "session cookie value" | ||
} |
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
File renamed without changes.
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,64 @@ | ||
from collections import Counter | ||
|
||
from adventofcode.registry.decorators import register_solution | ||
from adventofcode.util.exceptions import SolutionNotFoundError | ||
from adventofcode.util.input_helpers import get_input_for_day | ||
|
||
|
||
def parse_input(input_data: list[str]) -> tuple[list, list]: | ||
left_list: list[int] = [] | ||
right_list: list[int] = [] | ||
|
||
for row in input_data: | ||
left, right = row.split(" ") | ||
left_list.append(int(left)) | ||
right_list.append(int(right)) | ||
|
||
return sorted(left_list), sorted(right_list) | ||
|
||
|
||
def calculate_distances(data: tuple[list[int], list[int]]) -> int: | ||
total = 0 | ||
for left, right in zip(*data, strict=True): | ||
total += abs(left - right) | ||
|
||
return total | ||
|
||
|
||
def calculate_similarity(data: tuple[list[int], list[int]]) -> int: | ||
total = 0 | ||
left_list, right_list = data | ||
right_counter = Counter(right_list) | ||
|
||
for num in left_list: | ||
total += right_counter[num] * num | ||
|
||
return total | ||
|
||
|
||
@register_solution(2024, 1, 1) | ||
def part_one(input_data: list[str]): | ||
parsed_input = parse_input(input_data) | ||
answer = calculate_distances(parsed_input) | ||
|
||
if not answer: | ||
raise SolutionNotFoundError(2024, 1, 1) | ||
|
||
return answer | ||
|
||
|
||
@register_solution(2024, 1, 2) | ||
def part_two(input_data: list[str]): | ||
parsed_input = parse_input(input_data) | ||
answer = calculate_similarity(parsed_input) | ||
|
||
if not answer: | ||
raise SolutionNotFoundError(2024, 1, 2) | ||
|
||
return answer | ||
|
||
|
||
if __name__ == "__main__": | ||
data = get_input_for_day(2024, 1) | ||
part_one(data) | ||
part_two(data) |
Empty file.
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,11 @@ | ||
from adventofcode.year_2024.day_01_2024 import part_one, part_two | ||
|
||
test_input = ["3 4", "4 3", "2 5", "1 3", "3 9", "3 3"] | ||
|
||
|
||
def test_part_one(): | ||
assert part_one(test_input) == 11 | ||
|
||
|
||
def test_part_two(): | ||
assert part_two(test_input) == 31 |