Shorter Python template (also 2018 day 5 to test)

master
dece 2 years ago
parent 24ee5520bf
commit 24886aee85

1
.gitignore vendored

@ -1,2 +1,3 @@
day*.txt
input*.txt
venv/

@ -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
))

@ -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])

@ -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)

@ -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()

Loading…
Cancel
Save