parent
4eae307bee
commit
1c446d4a4a
3 changed files with 262 additions and 0 deletions
@ -0,0 +1,152 @@ |
||||
from __future__ import annotations |
||||
import copy |
||||
import json |
||||
from typing import Union |
||||
from itertools import combinations |
||||
|
||||
|
||||
class Regular: |
||||
|
||||
def __init__(self, value): |
||||
self.value = value |
||||
self.parent: Pair = None |
||||
|
||||
def split(self): |
||||
newLeft = Regular(self.value // 2) |
||||
newRight = Regular(self.value // 2 + self.value % 2) |
||||
newPair = Pair(newLeft, newRight) |
||||
newPair.parent = self.parent |
||||
if self == self.parent.left: |
||||
self.parent.left = newPair |
||||
if self == self.parent.right: |
||||
self.parent.right = newPair |
||||
|
||||
def __repr__(self): |
||||
return f"{self.value}" |
||||
|
||||
|
||||
class Pair: |
||||
|
||||
def __init__(self, left: Union[Pair, Regular], right: Union[Pair, Regular]): |
||||
self.left = left |
||||
self.right = right |
||||
left.parent = self |
||||
right.parent = self |
||||
self.parent: Pair = None |
||||
|
||||
@staticmethod |
||||
def neutral() -> Pair: |
||||
pair = Pair.parse([0, 0]) |
||||
pair.left = None |
||||
return pair |
||||
|
||||
@staticmethod |
||||
def parse(content) -> Union[Pair, int]: |
||||
if isinstance(content, int): |
||||
return Regular(content) |
||||
left, right = content |
||||
return Pair(Pair.parse(left), Pair.parse(right)) |
||||
|
||||
def __add__(self, other) -> Pair: |
||||
if self.left is None: |
||||
return other |
||||
pair = Pair(copy.deepcopy(self), copy.deepcopy(other)) |
||||
pair.reduce() |
||||
return pair |
||||
|
||||
def getExplodePair(self, dim=0) -> Pair: |
||||
if dim == 4: |
||||
return self |
||||
|
||||
def checkSide(side): |
||||
if isinstance(side, Pair): |
||||
pair = side.getExplodePair(dim + 1) |
||||
if isinstance(pair, Pair): |
||||
return pair |
||||
if explode := checkSide(self.left): |
||||
return explode |
||||
if explode := checkSide(self.right): |
||||
return explode |
||||
|
||||
def getSplitRegular(self) -> Regular: |
||||
leftIsRegular = isinstance(self.left, Regular) |
||||
rightIsRegular = isinstance(self.right, Regular) |
||||
if leftIsRegular: |
||||
if self.left.value >= 10: |
||||
return self.left |
||||
elif left := self.left.getSplitRegular(): |
||||
return left |
||||
if rightIsRegular: |
||||
if self.right.value >= 10: |
||||
return self.right |
||||
elif right := self.right.getSplitRegular(): |
||||
return right |
||||
return None |
||||
|
||||
def findRegularLeft(self) -> Regular: |
||||
if self.parent is None: |
||||
return None |
||||
if self == self.parent.right: |
||||
if isinstance(self.parent.left, Regular): |
||||
return self.parent.left |
||||
return self.parent.left.findLastRegular() |
||||
return self.parent.findRegularLeft() |
||||
|
||||
def findRegularRight(self) -> Regular: |
||||
if self.parent is None: |
||||
return None |
||||
if self == self.parent.left: |
||||
if isinstance(self.parent.right, Regular): |
||||
return self.parent.right |
||||
return self.parent.right.findFirstRegular() |
||||
return self.parent.findRegularRight() |
||||
|
||||
def findFirstRegular(self) -> Regular: |
||||
if isinstance(self.left, Regular): |
||||
return self.left |
||||
return self.left.findFirstRegular() |
||||
|
||||
def findLastRegular(self) -> Regular: |
||||
if isinstance(self.right, Regular): |
||||
return self.right |
||||
return self.right.findLastRegular() |
||||
|
||||
def explode(self): |
||||
regularLeft = self.findRegularLeft() |
||||
regularRight = self.findRegularRight() |
||||
if regularLeft: |
||||
regularLeft.value += self.left.value |
||||
if regularRight: |
||||
regularRight.value += self.right.value |
||||
zero = Regular(0) |
||||
zero.parent = self.parent |
||||
if self == self.parent.left: |
||||
self.parent.left = zero |
||||
if self == self.parent.right: |
||||
self.parent.right = zero |
||||
|
||||
def reduce(self): |
||||
while (toExplode := self.getExplodePair()) or (toSplit := self.getSplitRegular()): |
||||
if toExplode: |
||||
toExplode.explode() |
||||
continue |
||||
toSplit.split() |
||||
|
||||
def magnitude(self): |
||||
left = self.left.value if isinstance(self.left, Regular) else self.left.magnitude() |
||||
right = self.right.value if isinstance(self.right, Regular) else self.right.magnitude() |
||||
return left * 3 + right * 2 |
||||
|
||||
def __repr__(self): |
||||
return f"({self.left}, {self.right})" |
||||
|
||||
|
||||
def parseNumbers(fileName: str) -> list[Pair]: |
||||
with open(fileName) as file: |
||||
lines = file.read().splitlines() |
||||
return [Pair.parse(json.loads(line)) for line in lines] |
||||
|
||||
|
||||
allNumbers = parseNumbers("input") |
||||
print(sum(allNumbers, Pair.neutral()).magnitude()) |
||||
print(max([max((a + b).magnitude(), (b + a).magnitude()) for a, b in combinations(allNumbers, 2)])) |
@ -0,0 +1,100 @@ |
||||
[[[[9,5],[9,4]],[[6,5],[7,0]]],4] |
||||
[[[5,2],[[7,2],1]],[[[7,5],[0,8]],[[6,9],[7,3]]]] |
||||
[[[9,7],[0,1]],9] |
||||
[1,[[7,3],[[3,7],[3,2]]]] |
||||
[[9,[[0,8],7]],[[3,1],[[6,6],[9,0]]]] |
||||
[4,[[4,4],[[7,7],1]]] |
||||
[[[[6,2],[5,1]],[[3,3],9]],[7,[[5,7],[5,0]]]] |
||||
[[[[4,8],[4,9]],[1,[9,3]]],[1,[1,[6,1]]]] |
||||
[[[[4,7],[3,4]],[8,3]],[[3,7],[0,[1,8]]]] |
||||
[[[6,[4,8]],[4,5]],[4,[1,3]]] |
||||
[[[0,7],0],[[6,[1,8]],[9,[7,9]]]] |
||||
[[[[4,8],[3,9]],[4,5]],[1,1]] |
||||
[[[4,2],[0,[6,7]]],[[[1,8],2],[8,8]]] |
||||
[[[[1,1],7],5],[[6,[5,6]],[6,[7,5]]]] |
||||
[[[[3,2],5],[[5,3],1]],[[[0,4],[9,6]],9]] |
||||
[[6,[7,6]],9] |
||||
[[[[4,0],[0,1]],7],1] |
||||
[[[[1,3],4],6],[[1,[4,2]],[1,4]]] |
||||
[[[[6,9],[4,1]],[[6,3],[0,8]]],[[4,0],[[3,2],[2,9]]]] |
||||
[[[3,6],[[2,0],[3,2]]],[2,5]] |
||||
[[[[4,3],5],5],[[4,[4,0]],6]] |
||||
[[[[4,0],3],[[3,5],8]],[[8,[4,4]],[[9,9],[4,1]]]] |
||||
[[[2,7],6],1] |
||||
[[[[5,3],[8,4]],[0,0]],4] |
||||
[[[0,[8,1]],0],3] |
||||
[[[6,5],[8,2]],[[[6,9],[6,1]],[9,9]]] |
||||
[0,[[4,9],6]] |
||||
[[9,[[9,9],4]],[[[4,7],1],2]] |
||||
[[8,0],[[[0,7],6],[[6,4],2]]] |
||||
[[1,[[2,4],8]],1] |
||||
[[[[1,3],4],[[1,3],0]],[[[1,2],3],2]] |
||||
[[[[2,1],2],[5,[2,8]]],[2,[[6,0],2]]] |
||||
[[[8,[1,0]],[[6,7],[9,6]]],[[2,[9,7]],5]] |
||||
[[[3,[2,0]],[[3,2],[0,0]]],[[[4,6],[9,4]],[[7,8],[5,1]]]] |
||||
[[3,[[9,9],[7,2]]],[[1,3],[2,[3,2]]]] |
||||
[4,[4,[[9,5],6]]] |
||||
[[[[5,7],7],[[3,4],0]],[[9,[8,2]],[2,3]]] |
||||
[[[[2,1],[5,7]],4],[[[6,3],8],[[1,6],[5,1]]]] |
||||
[[[4,4],[[0,9],[7,8]]],[[2,[2,5]],5]] |
||||
[1,[5,[[3,7],[8,2]]]] |
||||
[[[[9,5],[8,6]],[5,5]],[[[9,2],8],[[9,3],[3,8]]]] |
||||
[0,[[9,5],[[3,7],7]]] |
||||
[[[8,[0,4]],[[2,9],6]],[[6,[8,0]],4]] |
||||
[[0,[3,5]],[[5,[0,1]],[[3,6],7]]] |
||||
[[2,[7,1]],[[[5,0],[7,7]],[[2,3],9]]] |
||||
[[5,[9,[3,9]]],[[8,[3,7]],[[7,6],[3,0]]]] |
||||
[[[4,[2,5]],5],[3,1]] |
||||
[[[[4,3],1],[[5,7],6]],[0,[3,1]]] |
||||
[[8,9],[[[0,7],5],[6,[5,7]]]] |
||||
[[6,8],[[5,8],[[8,2],[6,0]]]] |
||||
[[1,[5,6]],5] |
||||
[[[6,1],[9,[1,2]]],1] |
||||
[[5,[7,[4,8]]],[[4,[2,9]],5]] |
||||
[[[2,2],[[7,1],3]],[[[9,7],[4,6]],[1,[0,1]]]] |
||||
[[3,[6,[4,5]]],2] |
||||
[[[0,2],[[8,1],[0,6]]],[[7,[9,6]],0]] |
||||
[[[[1,0],[5,1]],[[0,6],5]],[[[1,8],8],[[0,2],5]]] |
||||
[[6,[[3,6],6]],[[[9,7],[6,4]],[[9,5],1]]] |
||||
[[[0,[5,6]],[9,0]],[[2,9],9]] |
||||
[1,[[4,[9,3]],0]] |
||||
[[1,0],[[1,9],[4,8]]] |
||||
[[[9,3],[7,0]],[[[5,1],[3,8]],9]] |
||||
[[[3,9],[[5,9],2]],[[7,2],1]] |
||||
[[1,[[3,0],[7,6]]],[7,[8,1]]] |
||||
[0,[6,[[7,1],[1,1]]]] |
||||
[[4,[[5,0],[2,1]]],[[[8,8],[8,1]],7]] |
||||
[[[[9,3],[4,3]],4],[7,5]] |
||||
[[9,[[7,4],[8,3]]],[[[1,9],7],[[1,6],[3,1]]]] |
||||
[[6,9],[5,[0,[5,1]]]] |
||||
[[[8,7],3],[[4,8],[0,7]]] |
||||
[[[[3,1],2],[[1,6],[4,3]]],[0,6]] |
||||
[[5,[[5,4],3]],[[8,8],9]] |
||||
[[5,[3,[4,5]]],[[2,[6,0]],[6,1]]] |
||||
[[[[9,5],3],6],[[8,[1,9]],[[5,2],5]]] |
||||
[[[7,5],[[3,6],4]],[6,[[5,1],[0,1]]]] |
||||
[[1,[[4,8],[1,3]]],7] |
||||
[[4,[[4,0],5]],[[[6,2],7],[[4,8],[4,9]]]] |
||||
[[[[2,3],[0,9]],[7,2]],[4,5]] |
||||
[[[[7,7],[8,0]],[7,7]],[[[6,6],[3,2]],[4,[4,3]]]] |
||||
[[[[8,7],6],[[5,5],0]],[[6,[7,3]],[[4,1],[1,7]]]] |
||||
[[[2,[2,2]],[[5,2],1]],[[9,[9,2]],6]] |
||||
[[[[1,7],6],[[8,8],5]],[6,[1,[1,7]]]] |
||||
[[[[8,6],[3,2]],[[5,2],[2,0]]],[[[8,7],2],[[5,5],2]]] |
||||
[[[8,[9,0]],[[9,5],[7,5]]],[[5,1],[[1,1],[4,6]]]] |
||||
[5,[9,[[0,2],7]]] |
||||
[8,[[0,[4,9]],[[7,4],9]]] |
||||
[[[[2,9],5],[[0,6],[6,6]]],[[0,6],[[4,2],[9,9]]]] |
||||
[7,[[[4,3],3],[[5,4],[6,0]]]] |
||||
[[0,[8,[1,1]]],5] |
||||
[[[1,8],[[4,6],[9,7]]],[[[6,6],[2,6]],[4,3]]] |
||||
[[0,[[7,5],[9,9]]],[[9,7],[6,2]]] |
||||
[[[9,[3,0]],[[1,4],0]],[[1,1],1]] |
||||
[[[0,7],[[3,0],8]],[[6,[8,0]],[[4,5],[4,0]]]] |
||||
[[[[2,9],[4,2]],[5,[9,3]]],[4,[2,[3,4]]]] |
||||
[[[1,[7,3]],[[5,7],0]],[6,[[6,5],2]]] |
||||
[4,5] |
||||
[[7,9],[6,[[6,5],[1,0]]]] |
||||
[[4,[[7,5],8]],[[4,0],[[6,6],[0,4]]]] |
||||
[[[9,[7,7]],[[4,2],7]],4] |
||||
[[0,[0,3]],5] |
@ -0,0 +1,10 @@ |
||||
[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]] |
||||
[[[5,[2,8]],4],[5,[[9,9],0]]] |
||||
[6,[[[6,2],[5,6]],[[7,6],[4,7]]]] |
||||
[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]] |
||||
[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]] |
||||
[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]] |
||||
[[[[5,4],[7,7]],8],[[8,3],8]] |
||||
[[9,3],[[9,9],[6,[4,9]]]] |
||||
[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]] |
||||
[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]] |
Loading…
Reference in new issue