Skip to content

Commit

Permalink
Merge branch 'master' into 2024_12
Browse files Browse the repository at this point in the history
  • Loading branch information
Stegallo committed Dec 15, 2024
2 parents 28ccc9d + 78b624b commit e626b8c
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 194 deletions.
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
37 changes: 9 additions & 28 deletions y_2024/day10.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,34 @@ def __init__(self, test=0):
super().__init__(__name__, test)

def _preprocess_input(self):
# self.__input_data = [[int(i) for i in chunk] for chunk in self._input_data]
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]

def _calculate_1(self):
result = 0
return 0
# print(self.grid.values['0'])

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

# print(f"{cur=}, {cur.ahead()=}, {self.grid.grid.get(cur.ahead())}")
# print(self.grid.grid.get(i))
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())
# print(f"{frontier=}, {len(frontier)=}")

while len(frontier) > 0:
k = frontier.popleft()
# print(f"{k=}, {self.grid.grid.get(k)=}")

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

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

Expand All @@ -67,42 +58,33 @@ def _calculate_1(self):
== 1
):
frontier.append(cur.ahead())
# if :
# visited.add()
# print(f"{len(visited)=}")

result += len(visited)
# break

# ...
return result

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

# print(f"{cur=}, {cur.ahead()=}, {self.grid.grid.get(cur.ahead())}")
# print(self.grid.grid.get(i))
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())
print(f"{frontier=}, {len(frontier)=}")
# break

while len(frontier) > 0:
k = frontier.popleft()
# print(f"{k=}, {self.grid.grid.get(k)=}")

if self.grid.grid.get(k) == "9":
visited += 1
# continue
# print(k, self.grid.grid.get(k))

for j in DIRS:
cur = Cursor(k, Direction.from_symbol(j))
if (
Expand All @@ -113,7 +95,6 @@ def _calculate_2(self):
):
frontier.append(cur.ahead())

# print(f"{len(visited)=}")
result += visited
# break

return result
71 changes: 6 additions & 65 deletions y_2024/day11.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def __post_init__(self) -> None:

@functools.lru_cache(maxsize=128000000, typed=False)
def inner(i):
# print(type(i))
res = []
# return res
if i == "0":
res.append("1")
return res
Expand All @@ -37,17 +35,14 @@ def inner(i):
def calc_len(stones, i):
stones = stones.split("#")
if i == 1:
# print(stones)
return len(stones)

r = 0
for j in stones:
# print(f"stone: {j}")
x = inner(j)
# print(f"flipped: {x}")

rex = calc_len("#".join(x), i - 1)
# print(f"{rex=}")
# continue

r += rex

return r
Expand All @@ -58,76 +53,22 @@ def __init__(self, test=0):
super().__init__(__name__, test)

def _preprocess_input(self):
# self.__input_data = [[int(i) for i in chunk] for chunk in self._input_data]
# 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[:3]:
# print(f"{x}")
# print('.')
# print('.')
# for x in self.__input_data[-3:]:
# print(f"{x}")
# ...

def _calculate_1(self):
# 0 1 10 99 999

result = 0
return 0
state = []
for j in self.__input_data:
for x in j.processed:
# print(x)
state.append(x)
# print(state)
for _ in range(75):
# for i in range(6):
new_state = []
for i in state:
print(i)

# print(new_state)
state = list(new_state)
result = len(state)
return result

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

def _calculate_2(self):
result = 0
state = []
for j in self.__input_data:
for x in j.processed:
# print(x)
state.append(x)
# print(state)
repeated = {}

T = 75
return calc_len("#".join(state), T + 1)
l = len(state) # noqa: E741
for progr in range(75):
# for progr in range(25):
# for progr in range(6):
l = 0 # noqa: E741
new = set()
new_state = []
for i in state:
# print(i)
new_state.extend(inner(i))
if i not in repeated:
# new.add(i)
rez = inner(i)
# print(rez,len(rez))
repeated[i] = len(rez)
# print(l)
l += repeated[i] # noqa: E741
# print(new_state)
state = list(new_state)
print(progr)
print(f"{len(new)=}, {len(repeated)=}, {l=}")

result = len(state)
return result
Loading

0 comments on commit e626b8c

Please sign in to comment.