diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-11-26 23:51:59 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-11-26 23:51:59 +0000 |
commit | deb9cd82db7f5b6a44f613feb0959110ce70c2e6 (patch) | |
tree | de0038e31cb65a8106fe30f4effdbcf883759683 /7 | |
parent | 0e8e49af605d3160645e773fdec53b7cf60cc068 (diff) | |
download | aoc2015-deb9cd82db7f5b6a44f613feb0959110ce70c2e6.tar.gz aoc2015-deb9cd82db7f5b6a44f613feb0959110ce70c2e6.tar.xz aoc2015-deb9cd82db7f5b6a44f613feb0959110ce70c2e6.zip |
solutions
Diffstat (limited to '7')
-rw-r--r-- | 7/solution.py | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/7/solution.py b/7/solution.py index 77a1391..6eee284 100644 --- a/7/solution.py +++ b/7/solution.py @@ -15,34 +15,44 @@ class U16: return U16(self.value << other.value) def __str__(self): return str(self.value) + def __repr__(self): + return f'{self.value}' class Node: - _value: U16 = None + _value: U16 | None = None @property def value(self) -> U16: - if not self._value: + if self._value is None: self._value = self.eval() return self._value - def eval(self, net: dict[Node]) -> U16: - return self.eval() - @absrtactmethod + @abstractmethod def eval(self) -> U16: pass + @abstractmethod + def __repr__(self) -> str: + pass class Reference(Node): - def __init__(self, name: str): + def __init__(self, net: dict[Node], name: str): + self.net = net self.name = name - def eval(self, net: dict[Node]) -> U16: - return net[self.name].value + def eval(self) -> U16: + return self.net[self.name].value + def __repr__(self) -> str: + return f'Reference({self.name})' class Constant(Node): def __init__(self, value): self._value = value + def __repr__(self) -> str: + return f'Constant({self.value})' class BinaryOp(Node): def __init__(self, left, right): self.left = left self.right = right + def __repr__(self) -> str: + return f'{self.__class__.__name__}({self.left}, {self.right})' class And(BinaryOp): def eval(self) -> U16: @@ -65,6 +75,8 @@ class Not(Node): self.node = node def eval(self): return ~self.node.value + def __repr__(self) -> str: + return 'Not({self.node})' def parse_expr(net: dict[Node], expr: str) -> Node: parts = expr.split(' ') @@ -112,4 +124,4 @@ if __name__ == '__main__': with open('input') as f: inp = [line.strip() for line in f] print(part1(inp)) - print(part2(inp))
\ No newline at end of file + print(part2(inp)) |