Skip to content

Commit

Permalink
Merge pull request #257 from Stegallo/2024
Browse files Browse the repository at this point in the history
2024
  • Loading branch information
Stegallo authored Dec 15, 2024
2 parents 34abff1 + 2eed55a commit 78b624b
Show file tree
Hide file tree
Showing 14 changed files with 651 additions and 91 deletions.
19 changes: 19 additions & 0 deletions common/grid.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
from typing import Optional

from pydantic.dataclasses import dataclass

# from dataclasses import dataclass # disabling pydantic may lead to 5x speed
from collections import defaultdict

DIRS = {"^": (-1, 0), ">": (0, 1), "v": (1, 0), "<": (0, -1)}
DIRS_8 = {
"^": (-1, 0),
">": (0, 1),
"v": (1, 0),
"<": (0, -1),
"1": (-1, -1),
"7": (-1, 1),
"L": (1, 1),
"J": (1, -1),
}


@dataclass
Expand Down Expand Up @@ -53,6 +65,13 @@ def from_symbol(symbol: str):
symbol,
)

@staticmethod
def from_symbol8(symbol: str):
return Direction(
*DIRS_8[symbol],
symbol,
)


@dataclass
class Cursor:
Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
numpy
pydantic
requests
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ ignore = E203,W503,W605,E741

[mypy]
plugins = pydantic.mypy
ignore_missing_imports = True
35 changes: 35 additions & 0 deletions tests/y_2024/test_2024_day10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# from __future__ import annotations

from unittest.mock import mock_open, patch

from y_2024.day10 import Day

with patch(
"builtins.open",
mock_open(
read_data="""89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732
""", # noqa: E501
),
):
day = Day()


def test__preprocess_input():
assert True


def test_calculate_1():
r = day._calculate_1()
assert r == 36


def test_calculate_2():
r = day._calculate_2()
assert r == 81
27 changes: 27 additions & 0 deletions tests/y_2024/test_2024_day11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# from __future__ import annotations

from unittest.mock import mock_open, patch

from y_2024.day11 import Day

with patch(
"builtins.open",
mock_open(
read_data="""125 17""", # noqa: E501
),
):
day = Day()


def test__preprocess_input():
assert True


def test_calculate_1():
r = day._calculate_1()
assert r == 55312


def test_calculate_2():
r = day._calculate_2()
assert r == 65601038650482
42 changes: 42 additions & 0 deletions tests/y_2024/test_2024_day13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# from __future__ import annotations

from unittest.mock import mock_open, patch

from y_2024.day13 import Day

with patch(
"builtins.open",
mock_open(
read_data="""Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
""", # noqa: E501
),
):
day = Day()


def test__preprocess_input():
assert True


def test_calculate_1():
r = day._calculate_1()
assert r == 480


def test_calculate_2():
r = day._calculate_2()
assert r == 875318608908
27 changes: 27 additions & 0 deletions tests/y_2024/test_2024_day9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# from __future__ import annotations

from unittest.mock import mock_open, patch

from y_2024.day9 import Day

with patch(
"builtins.open",
mock_open(
read_data="""2333133121414131402""", # noqa: E501
),
):
day = Day()


def test__preprocess_input():
assert True


def test_calculate_1():
r = day._calculate_1()
assert r == 1928


def test_calculate_2():
r = day._calculate_2()
assert r == 2858
6 changes: 5 additions & 1 deletion y_2024/day0.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pydantic.dataclasses import dataclass

from common.aoc import AoCDay
from common.grid import Grid


@dataclass
Expand All @@ -23,13 +24,16 @@ def _preprocess_input(self):
print(f"{self._input_data=}")
print(f"{len(self._input_data)=}")
print(f"{len(self._input_data[0])=}")
self.grid = Grid.from_input(self._input_data)
self.grid.display()
# self.__input_data = [Row(i) for i in self._input_data[0]]
self.__input_data = [Row(i) for j in self._input_data for i in j]
for x in self.__input_data:
print(f"{x}")

def _calculate_1(self):
result = 0
for x in self.__input_data:
print(f"{x}")
...
return result

Expand Down
100 changes: 100 additions & 0 deletions y_2024/day10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from typing import Optional

from pydantic.dataclasses import dataclass
from collections import deque
from common.aoc import AoCDay
from common.grid import Grid, Cursor, DIRS, Direction


@dataclass
class Row:
original: str
processed: Optional[str] = None

def __post_init__(self) -> None:
self.processed = "" # self.original


class Day(AoCDay):
def __init__(self, test=0):
super().__init__(__name__, test)

def _preprocess_input(self):
self.grid = Grid.from_input(self._input_data)
self.grid.display()
self.__input_data = [Row(i) for j in self._input_data for i in j]

def _calculate_1(self):
result = 0

for i in self.grid.values["0"]:
visited = set()
frontier = deque()
for j in DIRS:
cur = Cursor(i, Direction.from_symbol(j))

if (
self.grid.grid.get(cur.ahead())
and int(self.grid.grid.get(cur.ahead()))
- int(self.grid.grid.get(i))
== 1
):
frontier.append(cur.ahead())

while len(frontier) > 0:
k = frontier.popleft()

if self.grid.grid.get(k) == "9":
visited.add(k)
continue

for j in DIRS:
cur = Cursor(k, Direction.from_symbol(j))

if (
self.grid.grid.get(cur.ahead())
and int(self.grid.grid.get(cur.ahead()))
- int(self.grid.grid.get(k))
== 1
):
frontier.append(cur.ahead())

result += len(visited)

return result

def _calculate_2(self):
result = 0
for i in self.grid.values["0"]:
visited = 0
frontier = deque()
for j in DIRS:
cur = Cursor(i, Direction.from_symbol(j))

if (
self.grid.grid.get(cur.ahead())
and int(self.grid.grid.get(cur.ahead()))
- int(self.grid.grid.get(i))
== 1
):
frontier.append(cur.ahead())

while len(frontier) > 0:
k = frontier.popleft()

if self.grid.grid.get(k) == "9":
visited += 1

for j in DIRS:
cur = Cursor(k, Direction.from_symbol(j))
if (
self.grid.grid.get(cur.ahead())
and int(self.grid.grid.get(cur.ahead()))
- int(self.grid.grid.get(k))
== 1
):
frontier.append(cur.ahead())

result += visited

return result
74 changes: 74 additions & 0 deletions y_2024/day11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from typing import Optional

from pydantic.dataclasses import dataclass

from common.aoc import AoCDay
import functools


@dataclass
class Row:
original: str
processed: Optional[list[str]] = None

def __post_init__(self) -> None:
self.processed = self.original.split(" ")


@functools.lru_cache(maxsize=128000000, typed=False)
def inner(i):
res = []
if i == "0":
res.append("1")
return res

if len(i) % 2 == 0:
res.append(str(int(i[: len(i) // 2])))
res.append(str(int(i[len(i) // 2 :])))
return res

res.append(str(int(i) * 2024))
return res


@functools.lru_cache(maxsize=128000000, typed=False)
def calc_len(stones, i):
stones = stones.split("#")
if i == 1:
return len(stones)

r = 0
for j in stones:
x = inner(j)

rex = calc_len("#".join(x), i - 1)

r += rex

return r


class Day(AoCDay):
def __init__(self, test=0):
super().__init__(__name__, test)

def _preprocess_input(self):
self.__input_data = [Row(i) for j in self._input_data for i in j]

def _calculate_1(self):
state = []
for j in self.__input_data:
for x in j.processed:
state.append(x)

T = 25
return calc_len("#".join(state), T + 1)

def _calculate_2(self):
state = []
for j in self.__input_data:
for x in j.processed:
state.append(x)

T = 75
return calc_len("#".join(state), T + 1)
Loading

0 comments on commit 78b624b

Please sign in to comment.