Shorter Python template (also 2018 day 5 to test)
This commit is contained in:
parent
24ee5520bf
commit
24886aee85
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
day*.txt
|
day*.txt
|
||||||
|
input*.txt
|
||||||
venv/
|
venv/
|
||||||
|
|
23
2018/day5.py
Normal file
23
2018/day5.py
Normal file
|
@ -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,16 +1,13 @@
|
||||||
import sys
|
with open("day10.txt", "rt") as f:
|
||||||
|
lines = [line.rstrip() for line in f]
|
||||||
|
|
||||||
CHARMAP = {"]": "[", ")": "(", "}": "{", ">": "<"}
|
CHARMAP = {"]": "[", ")": "(", "}": "{", ">": "<"}
|
||||||
SCOREMAP = {")": 3, "]": 57, "}": 1197, ">": 25137}
|
SCOREMAP = {")": 3, "]": 57, "}": 1197, ">": 25137}
|
||||||
SCOREMAP2 = {"(": 1, "[": 2, "{": 3, "<": 4}
|
SCOREMAP2 = {"(": 1, "[": 2, "{": 3, "<": 4}
|
||||||
|
|
||||||
|
error_score = 0
|
||||||
def main():
|
comp_scores = []
|
||||||
lines = [line.rstrip() for line in sys.stdin]
|
for line in lines:
|
||||||
error_score = 0
|
|
||||||
comp_scores = []
|
|
||||||
for line in lines:
|
|
||||||
corrupted = None
|
corrupted = None
|
||||||
state = []
|
state = []
|
||||||
for char in line:
|
for char in line:
|
||||||
|
@ -29,9 +26,5 @@ def main():
|
||||||
for char in reversed(state):
|
for char in reversed(state):
|
||||||
cs = cs * 5 + SCOREMAP2[char]
|
cs = cs * 5 + SCOREMAP2[char]
|
||||||
comp_scores.append(cs)
|
comp_scores.append(cs)
|
||||||
print(error_score)
|
print(error_score)
|
||||||
print(list(sorted(comp_scores))[len(comp_scores) // 2])
|
print(list(sorted(comp_scores))[len(comp_scores) // 2])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
|
|
62
prepare.py
62
prepare.py
|
@ -1,7 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
import shutil
|
|
||||||
import webbrowser
|
import webbrowser
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
@ -13,47 +12,40 @@ URL = "https://adventofcode.com/{}/day/{}"
|
||||||
SESSION_ID = os.environ["AOC_SESSION"]
|
SESSION_ID = os.environ["AOC_SESSION"]
|
||||||
|
|
||||||
|
|
||||||
def main():
|
now = datetime.now()
|
||||||
now = datetime.now()
|
parser = argparse.ArgumentParser()
|
||||||
parser = argparse.ArgumentParser()
|
parser.add_argument("day", type=int, default=now.day)
|
||||||
parser.add_argument("day", type=int, default=now.day)
|
parser.add_argument("year", type=int, default=now.year)
|
||||||
parser.add_argument("year", type=int, default=now.year)
|
parser.add_argument("--lang", default="py")
|
||||||
parser.add_argument("--lang", default="py")
|
args = parser.parse_args()
|
||||||
args = parser.parse_args()
|
day, year, lang = args.day, args.year, args.lang
|
||||||
|
|
||||||
day_url = get_url(args.day, args.year)
|
day_url = URL.format(year, day)
|
||||||
input_url = day_url + "/input"
|
input_url = day_url + "/input"
|
||||||
input_text = fetch(input_url)
|
response = requests.get(input_url, cookies={"session": SESSION_ID})
|
||||||
create_files(input_text, args.day, args.year, args.lang)
|
response.raise_for_status()
|
||||||
webbrowser.open_new_tab(day_url)
|
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)
|
||||||
|
|
||||||
def fetch(input_url):
|
template_path = root_dir / f"template.{lang}"
|
||||||
response = requests.get(input_url, cookies={"session": SESSION_ID})
|
if template_path.exists():
|
||||||
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":
|
if lang == "rs":
|
||||||
bin_path = year_dir / "src" / "bin"
|
bin_path = year_dir / "src" / "bin"
|
||||||
bin_path.mkdir(exist_ok=True)
|
bin_path.mkdir(exist_ok=True)
|
||||||
script_path = bin_path / f"day{day}.{lang}"
|
script_path = bin_path / f"day{day}.{lang}"
|
||||||
else:
|
else:
|
||||||
script_path = year_dir / f"day{day}.{lang}"
|
script_path = year_dir / f"day{day}.{lang}"
|
||||||
shutil.copyfile(template_path, script_path)
|
with open(template_path, "rt") as template_file:
|
||||||
else:
|
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}.")
|
print(f"No template for {lang}.")
|
||||||
|
|
||||||
|
webbrowser.open_new_tab(day_url)
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
|
|
|
@ -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…
Reference in a new issue