Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024 01 #62

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,5 @@ dmypy.json
.idea

# Advent of code related
.session
.setup.json
src/adventofcode/inputs
4 changes: 4 additions & 0 deletions .setup.json.template
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"
}
35 changes: 26 additions & 9 deletions src/adventofcode/scripts/get_inputs.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import json
import os
from typing import Required, TypedDict

from httpx import get

from adventofcode.config import ROOT_DIR


class Setup(TypedDict):
user_agent: Required[str]
session_cookie: Required[str]


def get_input(year: int, day: int):
session = _read_session()
data = _download_input(year, day, session)
"""
Retrieves input from the Advent of Code website for the given year/day.
After retrieving the input, the input is stored in the project.
"""
setup = _read_setup()
data = _download_input(year, day, setup)
_save_input(data, year, day)


def _download_input(year: int, day: int, session: str) -> bytes:
def _download_input(year: int, day: int, setup: Setup) -> bytes:
"""
Downloads the input as text from the advent of code site
"""
cookies = {"session": session}
cookies = {"session": setup["session_cookie"]}
url = f"https://adventofcode.com/{year}/day/{day}/input"
resp = get(url, cookies=cookies)
resp = get(url, cookies=cookies, headers={"User-Agent": setup["user_agent"]})
resp.raise_for_status()
return resp.content # type: ignore
return resp.content


def _save_input(data: bytes, year: int, day: int) -> None:
Expand All @@ -32,9 +43,15 @@ def _save_input(data: bytes, year: int, day: int) -> None:
file.write(data)


def _read_session():
target = os.path.join(ROOT_DIR, "../../.session")
def _read_setup() -> Setup:
"""
Reads .setup.json from the projects' root directory

Returns:
Setup: a typed dict
"""
target = os.path.join(ROOT_DIR, "../../.setup.json")
path = os.path.abspath(target)

with open(path) as f:
return f.read()
return json.load(f)
File renamed without changes.
64 changes: 64 additions & 0 deletions src/adventofcode/year_2024/day_01_2024.py
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.
11 changes: 11 additions & 0 deletions tests/adventofcode/year_2024/test_day_01_2024.py
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
Loading