-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
10 changed files
with
4,700 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Advent of code 2024 | ||
|
||
This repository contains my solutions to the [Advent of Code 2024](https://adventofcode.com/2024) challenges | ||
by [Kaj](https://github.com/kws). | ||
|
||
## Running the code | ||
|
||
The code is written in Python 3.10 and uses [Poetry](https://python-poetry.org/) for dependency management. | ||
|
||
To run the code, first install Poetry and then run: | ||
|
||
```bash | ||
poetry install | ||
poetry shell | ||
aoc24 day<day> | ||
``` | ||
|
||
where `<day>` is the day number, e.g. | ||
|
||
```bash | ||
aoc24 day01 | ||
``` | ||
|
||
First time you run the code you will be prompted for a username. This username | ||
corresponds to the subdirectory within inputs that contains your personalised inputs. | ||
|
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,27 @@ | ||
import importlib | ||
import os | ||
from pathlib import Path | ||
|
||
import click | ||
|
||
from .config import settings | ||
|
||
|
||
@click.group() | ||
@click.option("--user", "-u", type=str) | ||
def main(user): | ||
if user: | ||
settings.username = user | ||
settings.save_settings() | ||
|
||
if not settings.username: | ||
user = click.prompt("Username", type=str, default=os.getlogin()) | ||
settings.username = user | ||
settings.save_settings() | ||
|
||
|
||
for name in Path(__file__).parent.glob("day*.py"): | ||
module = name.stem | ||
mod = importlib.import_module(f"aoc_2024_kws.{module}") | ||
|
||
mod = importlib.import_module(f"aoc_2024_kws.setup") |
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,19 @@ | ||
from pathlib import Path | ||
|
||
import usersettings | ||
|
||
settings = usersettings.Settings("kws.aoc24") | ||
settings.add_setting("username", str) | ||
settings.load_settings() | ||
|
||
|
||
class Config: | ||
INPUTS_DIR = Path(__file__).parent.parent / "inputs" | ||
SAMPLE_DIR = INPUTS_DIR / "samples" | ||
|
||
@property | ||
def USER_DIR(self): | ||
return self.INPUTS_DIR / settings.username | ||
|
||
|
||
config = Config() |
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,42 @@ | ||
|
||
from collections import Counter | ||
import click | ||
from aocd import submit | ||
|
||
from aoc_2024_kws.cli import main | ||
from aoc_2024_kws.config import config | ||
|
||
|
||
@main.command() | ||
@click.option("--sample", "-s", is_flag=True) | ||
def day01(sample): | ||
if sample: | ||
input_data = (config.SAMPLE_DIR / "day01.txt").read_text() | ||
else: | ||
input_data = (config.USER_DIR / "day01.txt").read_text() | ||
|
||
data = input_data.splitlines() | ||
data = [tuple(map(int, line.split())) for line in data] | ||
|
||
list_a = [x[0] for x in data] | ||
list_b = [x[1] for x in data] | ||
|
||
list_a.sort() | ||
list_b.sort() | ||
|
||
pairwise = zip(list_a, list_b) | ||
|
||
differences = [abs(b - a) for a, b in pairwise] | ||
|
||
|
||
print(sum(differences)) | ||
# submit(my_answer, part="a", day=01, year=2024) | ||
|
||
frequencies = Counter(list_b) | ||
|
||
similarity_score = 0 | ||
for a in list_a: | ||
similarity_score += frequencies.get(a, 0) * a | ||
|
||
print(similarity_score) | ||
# submit(my_answer, part="b", day=01, year=2024) |
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 @@ | ||
from pathlib import Path | ||
|
||
import click | ||
from aoc_2024_kws.cli import main | ||
from aoc_2024_kws.config import config | ||
from aocd import get_data | ||
from dateutil.utils import today | ||
|
||
|
||
@main.command() | ||
@click.argument("day", type=int, default=today().day) | ||
def create(day): | ||
"""Create a new day""" | ||
day = str(day).zfill(2) | ||
filename = Path(__file__).parent / f"day_{day}.py" | ||
if filename.exists(): | ||
click.echo(f"Day {day} already exists") | ||
else: | ||
filename.write_text( | ||
f""" | ||
import click | ||
from aocd import submit | ||
from aoc_2024_kws.cli import main | ||
from aoc_2024_kws.config import config | ||
@main.command() | ||
@click.option("--sample", "-s", is_flag=True) | ||
def day{day}(sample): | ||
if sample: | ||
input_data = (config.SAMPLE_DIR / "day{day}.txt").read_text() | ||
else: | ||
input_data = (config.USER_DIR / "day{day}.txt").read_text() | ||
# submit(my_answer, part="a", day={day}, year=2024) | ||
""" | ||
) | ||
|
||
config.SAMPLE_DIR.mkdir(parents=True, exist_ok=True) | ||
config.USER_DIR.mkdir(parents=True, exist_ok=True) | ||
|
||
sample_file = config.SAMPLE_DIR / f"day{day}.txt" | ||
if not sample_file.exists(): | ||
sample_file.touch() | ||
|
||
data = get_data(day=int(day), year=2024) | ||
|
||
data_file = config.USER_DIR / f"day{day}.txt" | ||
if not data_file.exists(): | ||
data_file.write_text(data) |
Oops, something went wrong.