From efb6a0fd4f68648a883f8440669ed68e6c67c4bb Mon Sep 17 00:00:00 2001 From: Stegallo Date: Fri, 13 Dec 2024 17:02:34 -0800 Subject: [PATCH] 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):