From 24886aee85d971aec97a13f426b2309abba1b8ff Mon Sep 17 00:00:00 2001 From: dece Date: Fri, 10 Dec 2021 17:24:01 +0100 Subject: [PATCH] Shorter Python template (also 2018 day 5 to test) --- .gitignore | 1 + 2018/day5.py | 23 +++++++++++++++ 2021/day10.py | 53 +++++++++++++++------------------- prepare.py | 80 +++++++++++++++++++++++---------------------------- template.py | 9 ++---- 5 files changed, 85 insertions(+), 81 deletions(-) create mode 100644 2018/day5.py diff --git a/.gitignore b/.gitignore index 2a20608..a286d05 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ day*.txt +input*.txt venv/ diff --git a/2018/day5.py b/2018/day5.py new file mode 100644 index 0000000..42c9cd7 --- /dev/null +++ b/2018/day5.py @@ -0,0 +1,23 @@ +import string + +with open("input5.txt") as f: + line = f.read().rstrip() + + +def react(line): + while True: + previous_length = len(line) + for letter in string.ascii_lowercase: + bi1 = letter + chr(ord(letter) ^ 0x20) + bi2 = bi1[::-1] + line = line.replace(bi1, "") + line = line.replace(bi2, "") + if len(line) == previous_length: + return line + + +print(len(react(line))) +print(min( + len(react(line.replace(letter, "").replace(chr(ord(letter) ^ 0x20), ""))) + for letter in string.ascii_letters +)) diff --git a/2021/day10.py b/2021/day10.py index 21beb2b..4607e56 100644 --- a/2021/day10.py +++ b/2021/day10.py @@ -1,37 +1,30 @@ -import sys - +with open("day10.txt", "rt") as f: + lines = [line.rstrip() for line in f] CHARMAP = {"]": "[", ")": "(", "}": "{", ">": "<"} SCOREMAP = {")": 3, "]": 57, "}": 1197, ">": 25137} SCOREMAP2 = {"(": 1, "[": 2, "{": 3, "<": 4} - -def main(): - lines = [line.rstrip() for line in sys.stdin] - error_score = 0 - comp_scores = [] - for line in lines: - corrupted = None - state = [] - for char in line: - if char in CHARMAP: - if state and state[-1] == CHARMAP[char]: - state.pop() - else: - corrupted = char - break +error_score = 0 +comp_scores = [] +for line in lines: + corrupted = None + state = [] + for char in line: + if char in CHARMAP: + if state and state[-1] == CHARMAP[char]: + state.pop() else: - state.append(char) - if corrupted: - error_score += SCOREMAP[corrupted] + corrupted = char + break else: - cs = 0 - for char in reversed(state): - cs = cs * 5 + SCOREMAP2[char] - comp_scores.append(cs) - print(error_score) - print(list(sorted(comp_scores))[len(comp_scores) // 2]) - - -if __name__ == "__main__": - main() + state.append(char) + if corrupted: + error_score += SCOREMAP[corrupted] + else: + cs = 0 + for char in reversed(state): + cs = cs * 5 + SCOREMAP2[char] + comp_scores.append(cs) +print(error_score) +print(list(sorted(comp_scores))[len(comp_scores) // 2]) diff --git a/prepare.py b/prepare.py index 43abfe9..6cd061c 100644 --- a/prepare.py +++ b/prepare.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 import argparse import os -import shutil import webbrowser from datetime import datetime from pathlib import Path @@ -13,47 +12,40 @@ URL = "https://adventofcode.com/{}/day/{}" SESSION_ID = os.environ["AOC_SESSION"] -def main(): - now = datetime.now() - parser = argparse.ArgumentParser() - parser.add_argument("day", type=int, default=now.day) - parser.add_argument("year", type=int, default=now.year) - parser.add_argument("--lang", default="py") - args = parser.parse_args() - - day_url = get_url(args.day, args.year) - input_url = day_url + "/input" - input_text = fetch(input_url) - create_files(input_text, args.day, args.year, args.lang) - webbrowser.open_new_tab(day_url) - -def fetch(input_url): - response = requests.get(input_url, cookies={"session": SESSION_ID}) - response.raise_for_status() - return response.text - -def get_url(day, year): - return URL.format(year, day) - -def create_files(text, day, year, lang): - root_dir = Path(__file__).parent.resolve() - year_dir = root_dir / str(year) - year_dir.mkdir(exist_ok=True) - input_path = year_dir / f"day{day}.txt" - with open(input_path, "wt") as output_file: - output_file.write(text) - template_path = root_dir / f"template.{lang}" - if template_path.exists(): - if lang == "rs": - bin_path = year_dir / "src" / "bin" - bin_path.mkdir(exist_ok=True) - script_path = bin_path / f"day{day}.{lang}" - else: - script_path = year_dir / f"day{day}.{lang}" - shutil.copyfile(template_path, script_path) +now = datetime.now() +parser = argparse.ArgumentParser() +parser.add_argument("day", type=int, default=now.day) +parser.add_argument("year", type=int, default=now.year) +parser.add_argument("--lang", default="py") +args = parser.parse_args() +day, year, lang = args.day, args.year, args.lang + +day_url = URL.format(year, day) +input_url = day_url + "/input" +response = requests.get(input_url, cookies={"session": SESSION_ID}) +response.raise_for_status() +input_text = response.text +root_dir = Path(__file__).parent.resolve() +year_dir = root_dir / str(year) +year_dir.mkdir(exist_ok=True) +input_path = year_dir / f"input{day}.txt" +with open(input_path, "wt") as output_file: + output_file.write(input_text) + +template_path = root_dir / f"template.{lang}" +if template_path.exists(): + if lang == "rs": + bin_path = year_dir / "src" / "bin" + bin_path.mkdir(exist_ok=True) + script_path = bin_path / f"day{day}.{lang}" else: - print(f"No template for {lang}.") - - -if __name__ == "__main__": - main() + script_path = year_dir / f"day{day}.{lang}" + with open(template_path, "rt") as template_file: + template = template_file.read() + template = template.format(day=day) + with open(script_path, "wt") as script_file: + script_file.write(template) +else: + print(f"No template for {lang}.") + +webbrowser.open_new_tab(day_url) diff --git a/template.py b/template.py index 8ee30f9..d9e95ca 100644 --- a/template.py +++ b/template.py @@ -1,9 +1,4 @@ -import sys +with open("input{day}.txt") as f: + lines = [line.rstrip() for line in f] -def main(): - lines = [line.rstrip() for line in sys.stdin] - - -if __name__ == "__main__": - main()