From 6fa734ac298678beab21075a4db7cb90ab4d95a9 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sun, 8 Dec 2024 17:37:41 -0800 Subject: [PATCH 01/15] maybe disable pydantic --- common/grid.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/common/grid.py b/common/grid.py index a527b2d..0bab731 100644 --- a/common/grid.py +++ b/common/grid.py @@ -1,6 +1,8 @@ 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)} From c5e9708160062a47a05ace385850200964e85972 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sun, 8 Dec 2024 20:58:01 -0800 Subject: [PATCH 02/15] day4 --- common/grid.py | 17 ++++++++ y_2024/day0.py | 3 ++ y_2024/day4.py | 107 ++++++++++++++++++++++++++----------------------- y_2024/day9.py | 41 +++++++++++++++++++ 4 files changed, 118 insertions(+), 50 deletions(-) create mode 100644 y_2024/day9.py diff --git a/common/grid.py b/common/grid.py index 0bab731..889975f 100644 --- a/common/grid.py +++ b/common/grid.py @@ -6,6 +6,16 @@ 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 @@ -55,6 +65,13 @@ def from_symbol(symbol: str): symbol, ) + @staticmethod + def from_symbol8(symbol: str): + return Direction( + *DIRS_8[symbol], + symbol, + ) + @dataclass class Cursor: diff --git a/y_2024/day0.py b/y_2024/day0.py index 5dc41bf..fe050c5 100644 --- a/y_2024/day0.py +++ b/y_2024/day0.py @@ -3,6 +3,7 @@ from pydantic.dataclasses import dataclass from common.aoc import AoCDay +from common.grid import Grid @dataclass @@ -23,6 +24,8 @@ 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] diff --git a/y_2024/day4.py b/y_2024/day4.py index 9ea7cdf..cc8a046 100644 --- a/y_2024/day4.py +++ b/y_2024/day4.py @@ -1,4 +1,5 @@ from common.aoc import AoCDay +from common.grid import Grid, DIRS_8, Cursor, Direction class Day(AoCDay): @@ -6,64 +7,70 @@ def __init__(self, test=0): super().__init__(__name__, test) def _preprocess_input(self): - self.__input_data = [[i for i in chunk] for chunk in self._input_data] - self.grid = {} - for y in self.__input_data: - for c, x in enumerate(y): - for i, k in enumerate(x): - self.grid[(c, i)] = k + self.grid = Grid.from_input(self._input_data) + self.grid.display() def _calculate_1(self): result = 0 - for y in self.__input_data: - for c, x in enumerate(y): - for i, k in enumerate(x): - if k != "X": - continue - left = (c, i - 1) - r = (c, i + 1) - u = (c - 1, i) - d = (c + 1, i) - lu = (c - 1, i - 1) - ru = (c - 1, i + 1) - ld = (c + 1, i - 1) - rd = (c + 1, i + 1) - if self.grid.get(left) == "M": - if self.grid.get((c, i - 2)) == "A": - if self.grid.get((c, i - 3)) == "S": - result += 1 - if self.grid.get(r) == "M": - if self.grid.get((c, i + 2)) == "A": - if self.grid.get((c, i + 3)) == "S": - result += 1 - if self.grid.get(u) == "M": - if self.grid.get((c - 2, i)) == "A": - if self.grid.get((c - 3, i)) == "S": - result += 1 - if self.grid.get(d) == "M": - if self.grid.get((c + 2, i)) == "A": - if self.grid.get((c + 3, i)) == "S": - result += 1 - if self.grid.get(lu) == "M": - if self.grid.get((c - 2, i - 2)) == "A": - if self.grid.get((c - 3, i - 3)) == "S": - result += 1 - if self.grid.get(ru) == "M": - if self.grid.get((c - 2, i + 2)) == "A": - if self.grid.get((c - 3, i + 3)) == "S": - result += 1 - if self.grid.get(ld) == "M": - if self.grid.get((c + 2, i - 2)) == "A": - if self.grid.get((c + 3, i - 3)) == "S": - result += 1 - if self.grid.get(rd) == "M": - if self.grid.get((c + 2, i + 2)) == "A": - if self.grid.get((c + 3, i + 3)) == "S": + for value in self.grid.values: + if value != "X": + continue + for pos in self.grid.values[value]: + for dir in DIRS_8: + cur = Cursor(pos, Direction.from_symbol8(dir)) + if self.grid.grid.get(cur.ahead()) == "M": + cur1 = Cursor(cur.ahead(), Direction.from_symbol8(dir)) + if self.grid.grid.get(cur1.ahead()) == "A": + cur2 = Cursor(cur1.ahead(), Direction.from_symbol8(dir)) + if self.grid.grid.get(cur2.ahead()) == "S": result += 1 + return result def _calculate_2(self): result = 0 + dirs_x = {k: v for k, v in DIRS_8.items() if k in ["1", "7", "L", "J"]} + # print(dirs_x) + for value in self.grid.values: + if value != "X": + continue + for pos in self.grid.values[value]: + ones = {} + for dir in dirs_x: + cur = Cursor(pos, Direction.from_symbol8(dir)) + ones[cur.dir.icon] = self.grid.grid.get(cur.ahead()) + print(ones) + if ( + ones["1"] == "M" + and ones["L"] == "S" + and ones["7"] == "M" + and ones["J"] == "S" + ): + result += 1 + if ( + ones["1"] == "S" + and ones["L"] == "M" + and ones["7"] == "S" + and ones["J"] == "M" + ): + result += 1 + + if ( + ones["1"] == "M" + and ones["L"] == "S" + and ones["7"] == "S" + and ones["J"] == "M" + ): + result += 1 + if ( + ones["1"] == "S" + and ones["L"] == "M" + and ones["7"] == "M" + and ones["J"] == "S" + ): + result += 1 + result = 0 + return result for y in self.__input_data: for c, x in enumerate(y): for i, k in enumerate(x): diff --git a/y_2024/day9.py b/y_2024/day9.py new file mode 100644 index 0000000..fe050c5 --- /dev/null +++ b/y_2024/day9.py @@ -0,0 +1,41 @@ +from typing import Optional + +from pydantic.dataclasses import dataclass + +from common.aoc import AoCDay +from common.grid import Grid + + +@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.__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 + for x in self.__input_data: + print(f"{x}") + ... + return result + + def _calculate_2(self): + result = 0 + return result From 83c60fa873d698ff59583689dff953b2c4ec2f11 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Tue, 10 Dec 2024 20:50:08 -0800 Subject: [PATCH 03/15] day 9 --- y_2024/day9.py | 144 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 136 insertions(+), 8 deletions(-) diff --git a/y_2024/day9.py b/y_2024/day9.py index fe050c5..7d1d614 100644 --- a/y_2024/day9.py +++ b/y_2024/day9.py @@ -1,9 +1,8 @@ from typing import Optional from pydantic.dataclasses import dataclass - +from collections import deque from common.aoc import AoCDay -from common.grid import Grid @dataclass @@ -15,6 +14,11 @@ def __post_init__(self) -> None: self.processed = "" # self.original +@dataclass +class Elem: + x: str + + class Day(AoCDay): def __init__(self, test=0): super().__init__(__name__, test) @@ -22,20 +26,144 @@ def __init__(self, test=0): 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() + # 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 - for x in self.__input_data: - print(f"{x}") + return 0 + r = [] + for c, x in enumerate(self.__input_data[0].original): + # print(f"{c//2=},{c%2=},{c=}{x=}") + m = c // 2 if c % 2 == 0 else "." + # print(f"{m=}, {x=}") + for i in range(int(x)): + r.append(Elem(str(m))) + # r.append(v) + # print(f"{v=}") ... + # print(r) + # string = list(''.join(r)) + print("here") + len_file = len([i for i in r if i.x != "."]) + # print(f"{string=}, {len_file=}") + print(f"{len_file=}") + print(f"here, {len_file=}") + c = 0 + # d=0 + for i in range(len(r) - 1): + x = r[len(r) - 1 - i].x + try: + while r[c].x != ".": + c += 1 + r[c].x = x + r[len(r) - 1 - i] = Elem("#") + except Exception: + break + result = 0 + for c, i in enumerate(r): + if i.x != "#": + result += c * int(i.x) + return result def _calculate_2(self): result = 0 + r = [] + for c, x in enumerate(self.__input_data[0].original): + # print(f"{c//2=},{c%2=},{c=}{x=}") + m = c // 2 if c % 2 == 0 else "." + # print(f"{m=}, {x=}") + for i in range(int(x)): + r.append(Elem(str(m))) + # r.append(v) + # print(f"{v=}") + ... + # print(r) + # string = list(''.join(r)) + print("here") + len_file = len([i for i in r if i.x != "."]) + # print(f"{string=}, {len_file=}") + print(f"{len_file=}") + print(f"here, {len_file=}") + # chunks + chunks = [] + chunks2 = [] + c = [] + c2 = [] + x = r[-1] + c.append(x) + c2.append(len(r) - 1) + for i in range(1, len(r)): + # print(i) + if r[-1 - i].x == x.x: + # print(r[-1-i].x,x.x) + c.append(r[len(r) - 1 - i]) + c2.append(len(r) - 1 - i) + else: + # print(r[-1-i].x,x.x) + if c[0].x != ".": + chunks.append(c) + chunks2.append(c2[::-1]) + c = [] + c2 = [] + x = r[-1 - i] + c.append(x) + c2.append(len(r) - 1 - i) + # print(chunks) + + # for c,i in enumerate(chunks): + # print(i,chunks2[c]) + # print('now') + + # free slots + slots = [] + slo = deque() + for i in range(len(r)): + if r[i].x == ".": + slo.append(i) + else: + if len(slo) > 0: + slots.append(slo) + slo = deque() + # for i in slots: + # print(i) + # return + c = 0 + # d=0 + print(len(r), r[-60:]) + print("tada\n\n\n") + for c, i in enumerate(chunks): + # # x = chunks[len(chunks)-1-i] + # print(i, len(i)) + for j in range(len(slots)): + # print(j,len(j)) + + if len(i) <= len(slots[j]): + if slots[j][0] > chunks2[c][0]: + break + # print("swap") + # print(chunks2[c]) + + for m in chunks2[c]: + # print(m) + r[m] = Elem("#") + n = slots[j].popleft() + # print(r[n],i[0], len(i)) + # if r[n].x!='.': + # breakpoint() + r[n] = i[0] + break + print(len(r), r[-60:]) + print("".join([i.x for i in r])) + result = 0 + for c, i in enumerate(r): + # print(i) + if i.x not in ["#", "."]: + result += c * int(i.x) + # 8504654861152 too high return result From ae2788142f40d73a8ec89c211e9bc077c867e5a7 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Tue, 10 Dec 2024 20:51:05 -0800 Subject: [PATCH 04/15] day 10 --- y_2024/day10.py | 119 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 y_2024/day10.py diff --git a/y_2024/day10.py b/y_2024/day10.py new file mode 100644 index 0000000..c3b0cef --- /dev/null +++ b/y_2024/day10.py @@ -0,0 +1,119 @@ +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.__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)) + + 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()) + # 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 ( + 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()) + + # print(f"{len(visited)=}") + result += visited + # break + return result From 358ed740041c2b5115d25e4768706af30fc15d65 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Wed, 11 Dec 2024 19:26:41 -0800 Subject: [PATCH 05/15] day 11 --- y_2024/day11.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 y_2024/day11.py diff --git a/y_2024/day11.py b/y_2024/day11.py new file mode 100644 index 0000000..fe050c5 --- /dev/null +++ b/y_2024/day11.py @@ -0,0 +1,41 @@ +from typing import Optional + +from pydantic.dataclasses import dataclass + +from common.aoc import AoCDay +from common.grid import Grid + + +@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.__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 + for x in self.__input_data: + print(f"{x}") + ... + return result + + def _calculate_2(self): + result = 0 + return result From febf7755d632a7afbcb93186a6101afd7e8082ba Mon Sep 17 00:00:00 2001 From: Stegallo Date: Wed, 11 Dec 2024 19:29:01 -0800 Subject: [PATCH 06/15] day 11 --- y_2024/day11.py | 114 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 103 insertions(+), 11 deletions(-) diff --git a/y_2024/day11.py b/y_2024/day11.py index fe050c5..999d387 100644 --- a/y_2024/day11.py +++ b/y_2024/day11.py @@ -3,16 +3,54 @@ from pydantic.dataclasses import dataclass from common.aoc import AoCDay -from common.grid import Grid +import functools @dataclass class Row: original: str - processed: Optional[str] = None + processed: Optional[list[str]] = None def __post_init__(self) -> None: - self.processed = "" # self.original + self.processed = self.original.split(" ") + + +@functools.lru_cache(maxsize=128000000, typed=False) +def inner(i): + # print(type(i)) + res = [] + # return 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: + # 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 class Day(AoCDay): @@ -21,21 +59,75 @@ def __init__(self, test=0): 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() + # 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 - for x in self.__input_data: - print(f"{x}") - ... + 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 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 From 49c9309d3ee981d399397a308be93811ded53bb2 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Wed, 11 Dec 2024 19:30:49 -0800 Subject: [PATCH 07/15] day 12 --- y_2024/day0.py | 3 ++- y_2024/day12.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 y_2024/day12.py diff --git a/y_2024/day0.py b/y_2024/day0.py index fe050c5..98a9c43 100644 --- a/y_2024/day0.py +++ b/y_2024/day0.py @@ -28,11 +28,12 @@ def _preprocess_input(self): 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 diff --git a/y_2024/day12.py b/y_2024/day12.py new file mode 100644 index 0000000..98a9c43 --- /dev/null +++ b/y_2024/day12.py @@ -0,0 +1,42 @@ +from typing import Optional + +from pydantic.dataclasses import dataclass + +from common.aoc import AoCDay +from common.grid import Grid + + +@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.__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: + print(f"{x}") + + def _calculate_1(self): + result = 0 + for x in self.__input_data: + ... + return result + + def _calculate_2(self): + result = 0 + return result From b491a5ad1236d407d41238a6a9a1d883bc20d64c Mon Sep 17 00:00:00 2001 From: Stegallo Date: Thu, 12 Dec 2024 20:59:23 -0800 Subject: [PATCH 08/15] day 13 --- y_2024/day13.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 y_2024/day13.py diff --git a/y_2024/day13.py b/y_2024/day13.py new file mode 100644 index 0000000..98a9c43 --- /dev/null +++ b/y_2024/day13.py @@ -0,0 +1,42 @@ +from typing import Optional + +from pydantic.dataclasses import dataclass + +from common.aoc import AoCDay +from common.grid import Grid + + +@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.__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: + print(f"{x}") + + def _calculate_1(self): + result = 0 + for x in self.__input_data: + ... + return result + + def _calculate_2(self): + result = 0 + return result From efb6a0fd4f68648a883f8440669ed68e6c67c4bb Mon Sep 17 00:00:00 2001 From: Stegallo Date: Fri, 13 Dec 2024 17:02:34 -0800 Subject: [PATCH 09/15] day 13 --- setup.cfg | 1 + y_2024/day13.py | 126 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 114 insertions(+), 13 deletions(-) diff --git a/setup.cfg b/setup.cfg index 941a1b7..7edfc2c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -4,3 +4,4 @@ ignore = E203,W503,W605,E741 [mypy] plugins = pydantic.mypy +ignore_missing_imports = True diff --git a/y_2024/day13.py b/y_2024/day13.py index 98a9c43..4b26037 100644 --- a/y_2024/day13.py +++ b/y_2024/day13.py @@ -1,18 +1,82 @@ from typing import Optional - +import numpy as np from pydantic.dataclasses import dataclass from common.aoc import AoCDay -from common.grid import Grid @dataclass class Row: original: str - processed: Optional[str] = None + processed: Optional[list[str]] = None def __post_init__(self) -> None: - self.processed = "" # self.original + self.processed = self.original.split(": ") + + +@dataclass +class Button: + name: str + x_incr: int + y_incr: int + + +@dataclass +class Prize: + x: int + y: int + + +@dataclass +class Machine: + ba: Button + bb: Button + pr: Prize + + def find_prize(self, cost=0, x=0, y=0, a=0, b=0): + M = np.array( + [[self.ba.x_incr, self.bb.x_incr], [self.ba.y_incr, self.bb.y_incr]], + ) + Minv = np.linalg.inv(M) + pr = np.array([self.pr.x + 10000000000000, self.pr.y + 10000000000000]) + x = np.matmul(Minv, pr) + print(x[0], x[1]) + a, b = round(x[0]), round(x[1]) + print(a, b) + # print(a*self.ba.x_incr+b*self.bb.x_incr) + # print(a*self.ba.y_incr+b*self.bb.y_incr) + # + + if ( + a * self.ba.x_incr + b * self.bb.x_incr != self.pr.x + 10000000000000 + or a * self.ba.y_incr + b * self.bb.y_incr != self.pr.y + 10000000000000 + ): + return 0 + print(a, b) + # if a>100 or b > 100: + # + # return 0 + print() + return a * 3 + b + # breakpoint() + # print(cost, x ,y, self.pr.x, self.pr.y, a, b) + # breakpoint() + # if x == self.pr.x and y == self.pr.y: + # return cost + # + # if x > self.pr.x or y > self.pr.y: + # return None + # # + # press_a = self.find_prize(cost+3,x+self.ba.x_incr,y+self.ba.y_incr, a+1, b) + # press_b = self.find_prize(cost+1,x+self.bb.x_incr,y+self.bb.y_incr, a, b+1) + # if press_a or press_b: + # return press_a or press_b + # # + # return None + # print(self.pr.x/self.ba.x_incr) + # print(self.pr.y/self.ba.y_incr) + # print(self.pr.x/self.bb.x_incr) + # print(self.pr.y/self.bb.y_incr) class Day(AoCDay): @@ -21,20 +85,56 @@ def __init__(self, test=0): 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"{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.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}") + self.machines = [] + for j in self._input_data: + local_machine = [] + for c, i in enumerate(j): + r = Row(i) + if c == 0: + a = Button( + r.processed[0].replace("Button ", ""), + int(r.processed[1].split(", ")[0].replace("X", "")), + int(r.processed[1].split(", ")[1].replace("Y", "")), + ) + # breakpoint() + if c == 1: + b = Button( + r.processed[0].replace("Button ", ""), + int(r.processed[1].split(", ")[0].replace("X", "")), + int(r.processed[1].split(", ")[1].replace("Y", "")), + ) + if c == 2: + p = Prize( + r.processed[1].split(", ")[0].replace("X=", ""), + r.processed[1].split(", ")[1].replace("Y=", ""), + ) + local_machine = Machine(a, b, p) + self.machines.append(local_machine) + # self.__input_data = [Row(i) for j in self._input_data for i in j] + # for x in self.machines: + # # for y in x: + # print(f"{x}") + # print() - def _calculate_1(self): + def _calculate_1(self): # 15731 low result = 0 - for x in self.__input_data: - ... + # return result + for c, x in enumerate(self.machines): + # a,b = x.find_prize() + # resulta += a + # resultb += b + print(f"{c=}, {x.find_prize()}") + result += x.find_prize() + # break + # print(f"{x}") + # print(resulta) + # print(resultb) return result def _calculate_2(self): From 12557e4c31e9176e119515df542109794202dd38 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 14 Dec 2024 15:11:22 -0800 Subject: [PATCH 10/15] day4 test --- y_2024/day4.py | 48 +++--------------------------------------------- 1 file changed, 3 insertions(+), 45 deletions(-) diff --git a/y_2024/day4.py b/y_2024/day4.py index cc8a046..b996c87 100644 --- a/y_2024/day4.py +++ b/y_2024/day4.py @@ -32,14 +32,14 @@ def _calculate_2(self): dirs_x = {k: v for k, v in DIRS_8.items() if k in ["1", "7", "L", "J"]} # print(dirs_x) for value in self.grid.values: - if value != "X": + if value != "A": continue for pos in self.grid.values[value]: ones = {} for dir in dirs_x: cur = Cursor(pos, Direction.from_symbol8(dir)) ones[cur.dir.icon] = self.grid.grid.get(cur.ahead()) - print(ones) + # print(ones) if ( ones["1"] == "M" and ones["L"] == "S" @@ -69,47 +69,5 @@ def _calculate_2(self): and ones["J"] == "S" ): result += 1 - result = 0 - return result - for y in self.__input_data: - for c, x in enumerate(y): - for i, k in enumerate(x): - if k != "A": - continue - lu = (c - 1, i - 1) - ru = (c - 1, i + 1) - ld = (c + 1, i - 1) - rd = (c + 1, i + 1) - - if ( - self.grid.get(lu) == "M" - and self.grid.get(rd) == "S" - and self.grid.get(ru) == "M" - and self.grid.get(ld) == "S" - ): - result += 1 - - if ( - self.grid.get(lu) == "S" - and self.grid.get(rd) == "M" - and self.grid.get(ru) == "S" - and self.grid.get(ld) == "M" - ): - result += 1 - - if ( - self.grid.get(lu) == "M" - and self.grid.get(rd) == "S" - and self.grid.get(ru) == "S" - and self.grid.get(ld) == "M" - ): - result += 1 - - if ( - self.grid.get(lu) == "S" - and self.grid.get(rd) == "M" - and self.grid.get(ru) == "M" - and self.grid.get(ld) == "S" - ): - result += 1 + # result = 0 return result From 12e241cc5a1b1e460d3a9a2ab0395b420d81eb50 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 14 Dec 2024 15:25:24 -0800 Subject: [PATCH 11/15] day9 --- tests/y_2024/test_2024_day10.py | 39 +++++++++++++++++++ tests/y_2024/test_2024_day11.py | 39 +++++++++++++++++++ tests/y_2024/test_2024_day12.py | 39 +++++++++++++++++++ tests/y_2024/test_2024_day9.py | 27 ++++++++++++++ y_2024/day9.py | 66 +++++---------------------------- 5 files changed, 154 insertions(+), 56 deletions(-) create mode 100644 tests/y_2024/test_2024_day10.py create mode 100644 tests/y_2024/test_2024_day11.py create mode 100644 tests/y_2024/test_2024_day12.py create mode 100644 tests/y_2024/test_2024_day9.py diff --git a/tests/y_2024/test_2024_day10.py b/tests/y_2024/test_2024_day10.py new file mode 100644 index 0000000..63a9ee0 --- /dev/null +++ b/tests/y_2024/test_2024_day10.py @@ -0,0 +1,39 @@ +# 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="""............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ +""", # noqa: E501 + ), +): + day = Day() + + +def test__preprocess_input(): + assert True + + +def test_calculate_1(): + r = day._calculate_1() + assert r == 14 + + +def test_calculate_2(): + r = day._calculate_2() + assert r == 34 diff --git a/tests/y_2024/test_2024_day11.py b/tests/y_2024/test_2024_day11.py new file mode 100644 index 0000000..ed18184 --- /dev/null +++ b/tests/y_2024/test_2024_day11.py @@ -0,0 +1,39 @@ +# 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="""............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ +""", # noqa: E501 + ), +): + day = Day() + + +def test__preprocess_input(): + assert True + + +def test_calculate_1(): + r = day._calculate_1() + assert r == 14 + + +def test_calculate_2(): + r = day._calculate_2() + assert r == 34 diff --git a/tests/y_2024/test_2024_day12.py b/tests/y_2024/test_2024_day12.py new file mode 100644 index 0000000..5c63e49 --- /dev/null +++ b/tests/y_2024/test_2024_day12.py @@ -0,0 +1,39 @@ +# from __future__ import annotations + +from unittest.mock import mock_open, patch + +from y_2024.day12 import Day + +with patch( + "builtins.open", + mock_open( + read_data="""............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ +""", # noqa: E501 + ), +): + day = Day() + + +def test__preprocess_input(): + assert True + + +def test_calculate_1(): + r = day._calculate_1() + assert r == 14 + + +def test_calculate_2(): + r = day._calculate_2() + assert r == 34 diff --git a/tests/y_2024/test_2024_day9.py b/tests/y_2024/test_2024_day9.py new file mode 100644 index 0000000..ee29026 --- /dev/null +++ b/tests/y_2024/test_2024_day9.py @@ -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 diff --git a/y_2024/day9.py b/y_2024/day9.py index 7d1d614..df36f2e 100644 --- a/y_2024/day9.py +++ b/y_2024/day9.py @@ -35,26 +35,15 @@ def _preprocess_input(self): def _calculate_1(self): result = 0 - return 0 r = [] for c, x in enumerate(self.__input_data[0].original): - # print(f"{c//2=},{c%2=},{c=}{x=}") m = c // 2 if c % 2 == 0 else "." - # print(f"{m=}, {x=}") + for i in range(int(x)): r.append(Elem(str(m))) - # r.append(v) - # print(f"{v=}") - ... - # print(r) - # string = list(''.join(r)) - print("here") - len_file = len([i for i in r if i.x != "."]) - # print(f"{string=}, {len_file=}") - print(f"{len_file=}") - print(f"here, {len_file=}") + c = 0 - # d=0 + for i in range(len(r) - 1): x = r[len(r) - 1 - i].x try: @@ -75,22 +64,11 @@ def _calculate_2(self): result = 0 r = [] for c, x in enumerate(self.__input_data[0].original): - # print(f"{c//2=},{c%2=},{c=}{x=}") m = c // 2 if c % 2 == 0 else "." - # print(f"{m=}, {x=}") + for i in range(int(x)): r.append(Elem(str(m))) - # r.append(v) - # print(f"{v=}") - ... - # print(r) - # string = list(''.join(r)) - print("here") - len_file = len([i for i in r if i.x != "."]) - # print(f"{string=}, {len_file=}") - print(f"{len_file=}") - print(f"here, {len_file=}") - # chunks + chunks = [] chunks2 = [] c = [] @@ -99,13 +77,10 @@ def _calculate_2(self): c.append(x) c2.append(len(r) - 1) for i in range(1, len(r)): - # print(i) if r[-1 - i].x == x.x: - # print(r[-1-i].x,x.x) c.append(r[len(r) - 1 - i]) c2.append(len(r) - 1 - i) else: - # print(r[-1-i].x,x.x) if c[0].x != ".": chunks.append(c) chunks2.append(c2[::-1]) @@ -114,13 +89,7 @@ def _calculate_2(self): x = r[-1 - i] c.append(x) c2.append(len(r) - 1 - i) - # print(chunks) - - # for c,i in enumerate(chunks): - # print(i,chunks2[c]) - # print('now') - # free slots slots = [] slo = deque() for i in range(len(r)): @@ -130,40 +99,25 @@ def _calculate_2(self): if len(slo) > 0: slots.append(slo) slo = deque() - # for i in slots: - # print(i) - # return + c = 0 - # d=0 - print(len(r), r[-60:]) - print("tada\n\n\n") + for c, i in enumerate(chunks): - # # x = chunks[len(chunks)-1-i] - # print(i, len(i)) for j in range(len(slots)): - # print(j,len(j)) - if len(i) <= len(slots[j]): if slots[j][0] > chunks2[c][0]: break - # print("swap") - # print(chunks2[c]) for m in chunks2[c]: - # print(m) r[m] = Elem("#") n = slots[j].popleft() - # print(r[n],i[0], len(i)) - # if r[n].x!='.': - # breakpoint() + r[n] = i[0] break - print(len(r), r[-60:]) - print("".join([i.x for i in r])) + result = 0 for c, i in enumerate(r): - # print(i) if i.x not in ["#", "."]: result += c * int(i.x) - # 8504654861152 too high + return result From 4b2433961ea486cc9a836d3cd40415f75b3c988a Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 14 Dec 2024 15:29:09 -0800 Subject: [PATCH 12/15] day 10 --- tests/y_2024/test_2024_day10.py | 24 +++++++++------------ y_2024/day10.py | 37 ++++++++------------------------- 2 files changed, 19 insertions(+), 42 deletions(-) diff --git a/tests/y_2024/test_2024_day10.py b/tests/y_2024/test_2024_day10.py index 63a9ee0..b95f618 100644 --- a/tests/y_2024/test_2024_day10.py +++ b/tests/y_2024/test_2024_day10.py @@ -7,18 +7,14 @@ with patch( "builtins.open", mock_open( - read_data="""............ -........0... -.....0...... -.......0.... -....0....... -......A..... -............ -............ -........A... -.........A.. -............ -............ + read_data="""89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 """, # noqa: E501 ), ): @@ -31,9 +27,9 @@ def test__preprocess_input(): def test_calculate_1(): r = day._calculate_1() - assert r == 14 + assert r == 36 def test_calculate_2(): r = day._calculate_2() - assert r == 34 + assert r == 81 diff --git a/y_2024/day10.py b/y_2024/day10.py index c3b0cef..143b420 100644 --- a/y_2024/day10.py +++ b/y_2024/day10.py @@ -20,28 +20,19 @@ 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())) @@ -49,14 +40,14 @@ def _calculate_1(self): == 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)) @@ -67,26 +58,19 @@ 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())) @@ -94,15 +78,13 @@ def _calculate_2(self): == 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 ( @@ -113,7 +95,6 @@ def _calculate_2(self): ): frontier.append(cur.ahead()) - # print(f"{len(visited)=}") result += visited - # break + return result From c48c3cb37cdf225cd53fbc1b4c2b1b63bc78cc43 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 14 Dec 2024 15:42:41 -0800 Subject: [PATCH 13/15] day 11 --- tests/y_2024/test_2024_day11.py | 18 ++------- tests/y_2024/test_2024_day12.py | 39 ------------------ y_2024/day11.py | 71 +++------------------------------ 3 files changed, 9 insertions(+), 119 deletions(-) delete mode 100644 tests/y_2024/test_2024_day12.py diff --git a/tests/y_2024/test_2024_day11.py b/tests/y_2024/test_2024_day11.py index ed18184..6a2b796 100644 --- a/tests/y_2024/test_2024_day11.py +++ b/tests/y_2024/test_2024_day11.py @@ -7,19 +7,7 @@ with patch( "builtins.open", mock_open( - read_data="""............ -........0... -.....0...... -.......0.... -....0....... -......A..... -............ -............ -........A... -.........A.. -............ -............ -""", # noqa: E501 + read_data="""125 17""", # noqa: E501 ), ): day = Day() @@ -31,9 +19,9 @@ def test__preprocess_input(): def test_calculate_1(): r = day._calculate_1() - assert r == 14 + assert r == 55312 def test_calculate_2(): r = day._calculate_2() - assert r == 34 + assert r == 65601038650482 diff --git a/tests/y_2024/test_2024_day12.py b/tests/y_2024/test_2024_day12.py deleted file mode 100644 index 5c63e49..0000000 --- a/tests/y_2024/test_2024_day12.py +++ /dev/null @@ -1,39 +0,0 @@ -# from __future__ import annotations - -from unittest.mock import mock_open, patch - -from y_2024.day12 import Day - -with patch( - "builtins.open", - mock_open( - read_data="""............ -........0... -.....0...... -.......0.... -....0....... -......A..... -............ -............ -........A... -.........A.. -............ -............ -""", # noqa: E501 - ), -): - day = Day() - - -def test__preprocess_input(): - assert True - - -def test_calculate_1(): - r = day._calculate_1() - assert r == 14 - - -def test_calculate_2(): - r = day._calculate_2() - assert r == 34 diff --git a/y_2024/day11.py b/y_2024/day11.py index 999d387..fd1b59d 100644 --- a/y_2024/day11.py +++ b/y_2024/day11.py @@ -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 @@ -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 @@ -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 From 7873016a1f782fc85808bce98a65e8b0480567fe Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 14 Dec 2024 15:57:52 -0800 Subject: [PATCH 14/15] day 13 --- tests/y_2024/test_2024_day13.py | 42 ++++++++++++++++++++ y_2024/day13.py | 70 +++++++-------------------------- 2 files changed, 56 insertions(+), 56 deletions(-) create mode 100644 tests/y_2024/test_2024_day13.py diff --git a/tests/y_2024/test_2024_day13.py b/tests/y_2024/test_2024_day13.py new file mode 100644 index 0000000..bfbcc8b --- /dev/null +++ b/tests/y_2024/test_2024_day13.py @@ -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 diff --git a/y_2024/day13.py b/y_2024/day13.py index 4b26037..c9abf94 100644 --- a/y_2024/day13.py +++ b/y_2024/day13.py @@ -33,50 +33,23 @@ class Machine: bb: Button pr: Prize - def find_prize(self, cost=0, x=0, y=0, a=0, b=0): + def find_prize(self, cost=0, x=0, y=0, a=0, b=0, pr_mul=0): M = np.array( [[self.ba.x_incr, self.bb.x_incr], [self.ba.y_incr, self.bb.y_incr]], ) Minv = np.linalg.inv(M) - pr = np.array([self.pr.x + 10000000000000, self.pr.y + 10000000000000]) + pr = np.array([self.pr.x + pr_mul, self.pr.y + pr_mul]) x = np.matmul(Minv, pr) - print(x[0], x[1]) + a, b = round(x[0]), round(x[1]) - print(a, b) - # print(a*self.ba.x_incr+b*self.bb.x_incr) - # print(a*self.ba.y_incr+b*self.bb.y_incr) - # if ( - a * self.ba.x_incr + b * self.bb.x_incr != self.pr.x + 10000000000000 - or a * self.ba.y_incr + b * self.bb.y_incr != self.pr.y + 10000000000000 + a * self.ba.x_incr + b * self.bb.x_incr != self.pr.x + pr_mul + or a * self.ba.y_incr + b * self.bb.y_incr != self.pr.y + pr_mul ): return 0 - print(a, b) - # if a>100 or b > 100: - # - # return 0 - print() + return a * 3 + b - # breakpoint() - # print(cost, x ,y, self.pr.x, self.pr.y, a, b) - # breakpoint() - # if x == self.pr.x and y == self.pr.y: - # return cost - # - # if x > self.pr.x or y > self.pr.y: - # return None - # # - # press_a = self.find_prize(cost+3,x+self.ba.x_incr,y+self.ba.y_incr, a+1, b) - # press_b = self.find_prize(cost+1,x+self.bb.x_incr,y+self.bb.y_incr, a, b+1) - # if press_a or press_b: - # return press_a or press_b - # # - # return None - # print(self.pr.x/self.ba.x_incr) - # print(self.pr.y/self.ba.y_incr) - # print(self.pr.x/self.bb.x_incr) - # print(self.pr.y/self.bb.y_incr) class Day(AoCDay): @@ -84,13 +57,6 @@ 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.machines = [] for j in self._input_data: local_machine = [] @@ -102,7 +68,7 @@ def _preprocess_input(self): int(r.processed[1].split(", ")[0].replace("X", "")), int(r.processed[1].split(", ")[1].replace("Y", "")), ) - # breakpoint() + if c == 1: b = Button( r.processed[0].replace("Button ", ""), @@ -116,27 +82,19 @@ def _preprocess_input(self): ) local_machine = Machine(a, b, p) self.machines.append(local_machine) - # self.__input_data = [Row(i) for j in self._input_data for i in j] - # for x in self.machines: - # # for y in x: - # print(f"{x}") - # print() - def _calculate_1(self): # 15731 low + def _calculate_1(self): result = 0 - # return result + for c, x in enumerate(self.machines): - # a,b = x.find_prize() - # resulta += a - # resultb += b - print(f"{c=}, {x.find_prize()}") result += x.find_prize() - # break - # print(f"{x}") - # print(resulta) - # print(resultb) + return result def _calculate_2(self): result = 0 + + for c, x in enumerate(self.machines): + result += x.find_prize(pr_mul=10000000000000) + return result From 2eed55a21ea73e2487cf51f82b9d2a50806d1df4 Mon Sep 17 00:00:00 2001 From: Stegallo Date: Sat, 14 Dec 2024 16:00:35 -0800 Subject: [PATCH 15/15] add numpy --- requirements/base.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements/base.txt b/requirements/base.txt index 903705e..ddc5f06 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -1,2 +1,3 @@ +numpy pydantic requests