Exemple #1
0
    def run(self, session: Session):
        session.validate(require_token=True)

        # Already cached input
        if path.exists(session.input_file):
            print(
                f"input already exists for {session.challenge}, not fetching")
            return

        # Fetch the input with the session
        cookies = {"session": session.token}
        r = requests.get(session.challenge.input_url, cookies=cookies)

        # Cache to the file
        with open(session.input_file, "w") as f:
            f.write(r.text)
            print(f"Fetched and cached input for ${session.challenge}")
Exemple #2
0
    def run(self, session: Session):
        args = session.command_args

        if args.token:
            session.token = args.token
            print("updated token")
        if args.l:
            session.language = args.l
            print(f"updated language to {session.language}")
        if args.d or args.y or args.today or args.next:
            now = datetime.now(timezone(timedelta(hours=-5)))
            if args.today:
                if now.month != 12:
                    raise ValueError(
                        "advent of code is not currently running. try -n?")
                if now.day not in _valid_days:
                    raise ValueError("advent of code is not currently running")
                session.challenge.year = now.year
                session.challenge.day = now.day
            elif args.next:
                if now.month == 11 and now.day == 30:
                    session.challenge.year = now.year
                    session.challenge.day = 1
                else:
                    if now.month != 12:
                        raise ValueError(
                            "advent of code is not running tomorrow")
                    if now.day + 1 not in _valid_days:
                        raise ValueError(
                            "advent of code is not running tomorrow")
                    session.challenge.year = now.year
                    session.challenge.day = now.day + 1
            else:
                if args.y:
                    session.challenge.year = args.y
                    session.challenge = Challenge(args.y,
                                                  session.challenge.day)
                if args.d:
                    session.challenge.day = args.d
                    session.challenge = Challenge(session.challenge.year,
                                                  args.d)
            print(f"updated challenge to {session.challenge}")

        session.validate()
        session.cache()
        print(session)
Exemple #3
0
    def run(self, session: Session):
        session.validate(require_token=True)
        print(f"=====\nRunning {session.challenge}:")

        start_time = time.perf_counter()
        helper = language_helper(session)
        return_code, output = helper.run()
        end_time = time.perf_counter()

        if return_code != 0:
            print(f"error: {output}")
            return

        print(f"runtime: {(end_time - start_time)}")

        # If saving, overwrite the current solution
        if session.command == Command.RUN or session.command == Command.TEST:
            if output and session.command_args.save:
                print(f"saving output {output}")
                with open(session.output_file, "w") as f:
                    f.write(output)
            else:
                # When not saving, compare the solution to the current solution and report
                if path.exists(session.output_file):
                    with open(session.output_file) as f:
                        solution = f.read()
                        if solution == output:
                            print("✅ your solution appears correct!")
                        else:
                            print(
                                f"❌ solution does not match one found in {session.output_file}"
                            )
                            print(f"\texpected: {solution}")
                            print(f"\treceived: {output}")
                else:
                    print(
                        "⚠️ solution does not exist for validation. skipping..."
                    )

        # When running the submit command, return the output
        if session.command == Command.SUBMIT:
            return output