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,37 +1,30 @@
|
||||||
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
|
corrupted = None
|
||||||
comp_scores = []
|
state = []
|
||||||
for line in lines:
|
for char in line:
|
||||||
corrupted = None
|
if char in CHARMAP:
|
||||||
state = []
|
if state and state[-1] == CHARMAP[char]:
|
||||||
for char in line:
|
state.pop()
|
||||||
if char in CHARMAP:
|
|
||||||
if state and state[-1] == CHARMAP[char]:
|
|
||||||
state.pop()
|
|
||||||
else:
|
|
||||||
corrupted = char
|
|
||||||
break
|
|
||||||
else:
|
else:
|
||||||
state.append(char)
|
corrupted = char
|
||||||
if corrupted:
|
break
|
||||||
error_score += SCOREMAP[corrupted]
|
|
||||||
else:
|
else:
|
||||||
cs = 0
|
state.append(char)
|
||||||
for char in reversed(state):
|
if corrupted:
|
||||||
cs = cs * 5 + SCOREMAP2[char]
|
error_score += SCOREMAP[corrupted]
|
||||||
comp_scores.append(cs)
|
else:
|
||||||
print(error_score)
|
cs = 0
|
||||||
print(list(sorted(comp_scores))[len(comp_scores) // 2])
|
for char in reversed(state):
|
||||||
|
cs = cs * 5 + SCOREMAP2[char]
|
||||||
|
comp_scores.append(cs)
|
||||||
if __name__ == "__main__":
|
print(error_score)
|
||||||
main()
|
print(list(sorted(comp_scores))[len(comp_scores) // 2])
|
||||||
|
|
74
prepare.py
74
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()
|
if lang == "rs":
|
||||||
return response.text
|
bin_path = year_dir / "src" / "bin"
|
||||||
|
bin_path.mkdir(exist_ok=True)
|
||||||
def get_url(day, year):
|
script_path = bin_path / f"day{day}.{lang}"
|
||||||
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)
|
|
||||||
else:
|
else:
|
||||||
print(f"No template for {lang}.")
|
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)
|
||||||
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