Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: remove() doesn't perform well. #222

Open
yhm138 opened this issue Dec 20, 2023 · 0 comments
Open

bug: remove() doesn't perform well. #222

yhm138 opened this issue Dec 20, 2023 · 0 comments

Comments

@yhm138
Copy link

yhm138 commented Dec 20, 2023

I wrote one script with readbaron to remove code comments.

from redbaron import RedBaron

def remove_comments_with_redbaron(source_py_file):
    with open(source_py_file, 'r', encoding='utf-8') as file:
        red = RedBaron(file.read())
    comments = red.find_all('comment')
    for comment in comments:
        comment.parent.remove(comment)
    return red.dumps()


def main():
    source_py_file = input("Enter the path to the .py file to clean up: ")
    new_content = remove_comments_with_redbaron(source_py_file)
    new_file_name = source_py_file.replace('.py', '_nocmts.py')
    with open(new_file_name, 'w', encoding='utf-8') as new_file:
        new_file.write(new_content)
    print(f"Cleaned file has been saved as: {new_file_name}")

if __name__ == '__main__':
    main()

But there may be indentation errors in the output file.

.py file I used:

from collections import deque
from math import lcm

lines = [x for x in open('E:\\ExplorerDownload\input_day20.txt').read().strip().split('\n')]

adj = {}
conjs = {}
ffs = {}
rx_conj = ""

for line in lines:
    module, dests = line.split(" -> ")
    dests = dests.split(", ")
    t = module[0]
    if module == "broadcaster":
        adj["broadcaster"] = dests
    else:
        label = module[1:]
        adj[label] = dests

    if "rx" in dests:
        rx_conj = label

    if t == "&":
        conjs[label] = {}

    if t == "%":
        ffs[label] = False

for label, dests in adj.items():
    for dest in dests:
        if dest in conjs:
            conjs[dest][label] = 0


low_pulses = 0
high_pulses = 0
presses = 0
rx_conj_presses = {}


def press():
    global low_pulses, high_pulses, presses
    presses += 1

    low_pulses += 1 + len(adj["broadcaster"])
    queue = deque()
    for dest in adj["broadcaster"]:
        queue.append((0, "broadcaster", dest))

    while queue:
        pulse, src, label = queue.popleft()

        if label == "rx":
            continue

        # conjunction
        to_send = 0
        if label in conjs:
            conjs[label][src] = pulse
            if any(n == 0 for n in conjs[label].values()):
                to_send = 1

        # flip-flop
        if label in ffs:
            if pulse == 1:
                continue
            ffs[label] = not ffs[label]
            if ffs[label]:
                to_send = 1

        # increment low or high pulses
        if to_send == 1:
            high_pulses += len(adj[label])
        else:
            low_pulses += len(adj[label])

        # send pulse to destination modules
        for dest in adj[label]:
            queue.append((to_send, label, dest))

        # check if any of the inputs connected to the conjunction
        # connected to "rx" are one and record the number of presses
        for label, val in conjs[rx_conj].items():
            if val == 1 and label not in rx_conj_presses:
                rx_conj_presses[label] = presses


for _ in range(1000):
    press()

print(low_pulses * high_pulses)

while len(rx_conj_presses) < 4:
    press()

print(lcm(*rx_conj_presses.values()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant