day20 - finished (is there a way to speed up p2?)
This commit is contained in:
parent
2b3a465c5a
commit
688d7d1e34
58
day20.py
58
day20.py
@ -8,10 +8,9 @@ class Node:
|
||||
self.value = value
|
||||
self.left = None
|
||||
self.right = None
|
||||
self.was_moved = False
|
||||
|
||||
def __str__(self):
|
||||
return f"Node(id={self.node_id}; value={self.value}; moved={self.was_moved}; left={self.left.node_id}; right={self.right.node_id})"
|
||||
return f"Node(id={self.node_id}; value={self.value}; left={self.left.node_id}; right={self.right.node_id})"
|
||||
|
||||
def __len__(self):
|
||||
ptr = self
|
||||
@ -23,26 +22,14 @@ class Node:
|
||||
return length + 1
|
||||
|
||||
|
||||
def llistprint(llist: Node) -> None:
|
||||
ptr = llist
|
||||
print("[", end="")
|
||||
while ptr.right != llist:
|
||||
print(f"{ptr.value}, ", end="")
|
||||
ptr = ptr.right
|
||||
print(f"{ptr.value}]")
|
||||
|
||||
|
||||
def mix(head: Node) -> Node:
|
||||
ptr = head
|
||||
list_len = len(head)
|
||||
for i in range(list_len):
|
||||
while ptr.node_id != i:
|
||||
ptr = ptr.right
|
||||
cur_val = ptr.value
|
||||
cur_ptr = ptr
|
||||
cur_ptr.was_moved = True
|
||||
|
||||
ptr = ptr.right
|
||||
while ptr.was_moved and i < list_len - 1:
|
||||
ptr = ptr.right
|
||||
|
||||
if cur_val == 0:
|
||||
continue
|
||||
@ -55,9 +42,9 @@ def mix(head: Node) -> Node:
|
||||
|
||||
target_ptr = cur_ptr
|
||||
if cur_val > 0:
|
||||
cur_val = cur_val % list_len + cur_val // list_len
|
||||
cur_val = cur_val % list_len + (cur_val // list_len) % (list_len - 1)
|
||||
else:
|
||||
cur_val = -(abs(cur_val) % list_len + abs(cur_val) // list_len)
|
||||
cur_val = -(abs(cur_val) % list_len + (abs(cur_val) // list_len) % (list_len - 1))
|
||||
|
||||
for _ in range(abs(cur_val)):
|
||||
if cur_val < 0:
|
||||
@ -78,6 +65,20 @@ def mix(head: Node) -> Node:
|
||||
|
||||
return head
|
||||
|
||||
|
||||
def coord_sum(ptr: Node) -> int:
|
||||
while ptr.value != 0:
|
||||
ptr = ptr.right
|
||||
|
||||
val_sum = 0
|
||||
for _ in range(3):
|
||||
for _ in range(1000):
|
||||
ptr = ptr.right
|
||||
val_sum += ptr.value
|
||||
|
||||
return val_sum
|
||||
|
||||
|
||||
class Day(AOCDay):
|
||||
inputs = [
|
||||
[
|
||||
@ -87,7 +88,7 @@ class Day(AOCDay):
|
||||
],
|
||||
[
|
||||
(1623178306, "input20_test"),
|
||||
(None, "input20"),
|
||||
(4979911042808, "input20"),
|
||||
]
|
||||
]
|
||||
|
||||
@ -112,30 +113,17 @@ class Day(AOCDay):
|
||||
def part1(self) -> Any:
|
||||
ptr = self.get_inp_llist()
|
||||
ptr = mix(ptr)
|
||||
return coord_sum(ptr)
|
||||
|
||||
while ptr.value != 0:
|
||||
ptr = ptr.right
|
||||
|
||||
val_sum = 0
|
||||
for _ in range(3):
|
||||
for _ in range(1000):
|
||||
ptr = ptr.right
|
||||
val_sum += ptr.value
|
||||
|
||||
return val_sum
|
||||
|
||||
def part2(self) -> Any:
|
||||
key = 811589153
|
||||
ptr = self.get_inp_llist(key)
|
||||
for _ in range(10):
|
||||
llistprint(ptr)
|
||||
ptr = mix(ptr)
|
||||
|
||||
llistprint(ptr)
|
||||
|
||||
return ""
|
||||
return coord_sum(ptr)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
day = Day(2022, 20)
|
||||
day.run(verbose=True)
|
||||
day.run(verbose=True)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user