From c6f61b88a9e92085eb2cc224d738e7ea24510560 Mon Sep 17 00:00:00 2001 From: Corentin Dancette Date: Tue, 29 Dec 2020 00:20:06 +0100 Subject: [PATCH 1/2] coco day 23 --- day-23/input/coco.txt | 1 + day-23/part-1/coco.py | 40 ++++++++++++++++++++++++++ day-23/part-2/coco.py | 66 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 day-23/input/coco.txt create mode 100644 day-23/part-1/coco.py create mode 100644 day-23/part-2/coco.py diff --git a/day-23/input/coco.txt b/day-23/input/coco.txt new file mode 100644 index 00000000..6215abc0 --- /dev/null +++ b/day-23/input/coco.txt @@ -0,0 +1 @@ +326519478 \ No newline at end of file diff --git a/day-23/part-1/coco.py b/day-23/part-1/coco.py new file mode 100644 index 00000000..6b62ff7f --- /dev/null +++ b/day-23/part-1/coco.py @@ -0,0 +1,40 @@ +from tool.runners.python import SubmissionPy + + +class CocoSubmission(SubmissionPy): + def run(self, s): + """ + :param s: input in string format + :return: solution flag + """ + # Your code goes here + cups = [int(x) for x in list(s.strip())] + # print("cups", cups) + N = len(cups) + position = 0 + for k in range(100): + print(f"move {k+1}") + print(" ".join(map(str, cups))) + item = cups[position] + removed = [cups[(position + k) % N] for k in range(1, 4)] + remaining = [cups[(position + k) % N] for k in range(4, len(cups) + 1)] + destination_value = (item - 2) % N + 1 + while destination_value in removed: + destination_value = (destination_value - 2) % N + 1 + index = remaining.index(destination_value) + cups = remaining[:index+1] + removed + remaining[index+1:] + position = (cups.index(item) + 1) % N + + index_one = cups.index(1) + return "".join(str(cups[k % N]) for k in range(index_one + 1, index_one + len(cups))) + +def test_coco(): + """ + Run `python -m pytest ./day-23/part-1/coco.py` to test the submission. + """ + assert ( + CocoSubmission().run( + """389125467""".strip() + ) + == "67384529" + ) diff --git a/day-23/part-2/coco.py b/day-23/part-2/coco.py new file mode 100644 index 00000000..e8f67ee8 --- /dev/null +++ b/day-23/part-2/coco.py @@ -0,0 +1,66 @@ +from tool.runners.python import SubmissionPy +from tqdm import tqdm + + +class LinkedListNode: + def __init__(self, value) -> None: + self.value = value + self.next = None + + +class CocoSubmission(SubmissionPy): + + def run(self, s): + """ + :param s: input in string format + :return: solution flag + """ + # Your code goes here + inputs = [int(x) for x in list(s.strip())] + cups = list(range(1, int(1e6+1))) + cups[:len(inputs)] = inputs + + N = len(cups) + + # init linked list + nodes = [LinkedListNode(value=cups[0])] + for i in range(1, len(cups)): + node = LinkedListNode(value=cups[i]) + nodes.append(node) + nodes[i-1].next = node + nodes[-1].next = nodes[0] # loop over beginning + + value_to_node = { + node.value: node for node in nodes + } + + node = nodes[0] + for _ in tqdm(range(int(1e7))): + next_three = [node.next, node.next.next, node.next.next.next] + node.next = node.next.next.next.next # 4th + + removed_values = {n.value for n in next_three} + destination_value = (node.value - 2) % N + 1 + while destination_value in removed_values: + destination_value = (destination_value - 2) % N + 1 + + node_dest = value_to_node[destination_value] + next_tmp = node_dest.next + node_dest.next = next_three[0] + next_three[2].next = next_tmp + node = node.next + + node_one = [n for n in nodes if n.value == 1][0] + return node_one.next.value * node_one.next.next.value + + +def test_coco(): + """ + Run `python -m pytest ./day-23/part-1/coco.py` to test the submission. + """ + assert ( + CocoSubmission().run( + """389125467""".strip() + ) + == 149245887792 + ) From cc4d4a6b0e59f399be58803f937b325d1d522574 Mon Sep 17 00:00:00 2001 From: Corentin Dancette Date: Tue, 29 Dec 2020 00:24:18 +0100 Subject: [PATCH 2/2] clean --- day-23/part-1/coco.py | 3 --- day-23/part-2/coco.py | 4 +--- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/day-23/part-1/coco.py b/day-23/part-1/coco.py index 6b62ff7f..9da44669 100644 --- a/day-23/part-1/coco.py +++ b/day-23/part-1/coco.py @@ -9,12 +9,9 @@ def run(self, s): """ # Your code goes here cups = [int(x) for x in list(s.strip())] - # print("cups", cups) N = len(cups) position = 0 for k in range(100): - print(f"move {k+1}") - print(" ".join(map(str, cups))) item = cups[position] removed = [cups[(position + k) % N] for k in range(1, 4)] remaining = [cups[(position + k) % N] for k in range(4, len(cups) + 1)] diff --git a/day-23/part-2/coco.py b/day-23/part-2/coco.py index e8f67ee8..390ea0ae 100644 --- a/day-23/part-2/coco.py +++ b/day-23/part-2/coco.py @@ -1,6 +1,4 @@ from tool.runners.python import SubmissionPy -from tqdm import tqdm - class LinkedListNode: def __init__(self, value) -> None: @@ -35,7 +33,7 @@ def run(self, s): } node = nodes[0] - for _ in tqdm(range(int(1e7))): + for _ in (range(int(1e7))): next_three = [node.next, node.next.next, node.next.next.next] node.next = node.next.next.next.next # 4th