-
Notifications
You must be signed in to change notification settings - Fork 1
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 #257 from Stegallo/2024
2024
- Loading branch information
Showing
14 changed files
with
651 additions
and
91 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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
numpy | ||
pydantic | ||
requests |
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ ignore = E203,W503,W605,E741 | |
|
||
[mypy] | ||
plugins = pydantic.mypy | ||
ignore_missing_imports = True |
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,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 |
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 @@ | ||
# 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 |
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 __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 |
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 @@ | ||
# 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 |
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
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,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 |
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,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) |
Oops, something went wrong.