Compare commits

...

2 commits

Author SHA1 Message Date
dece 2bfcdf0956 Day 2 2020-12-02 14:03:05 +01:00
dece 958cc852f3 Add new day script 2020-12-02 13:54:18 +01:00
3 changed files with 46 additions and 7 deletions

27
2020/day2.py Normal file
View file

@ -0,0 +1,27 @@
def main():
with open("day2.txt", "rt") as f:
lines = [line.rstrip() for line in f.readlines()]
# Part 1
valid = 0
for line in lines:
count, letter, password = line.split()
min_n, max_n = count.split("-")
letter = letter.rstrip(":")
if int(min_n) <= password.count(letter) <= int(max_n):
valid += 1
print("Valids:", valid)
# Part 2
valid = 0
for line in lines:
count, letter, password = line.split()
ofs_a, ofs_b = count.split("-")
letter = letter.rstrip(":")
a = password[int(ofs_a) - 1]
b = password[int(ofs_b) - 1]
if (a == letter or b == letter) and not (a == b == letter):
valid += 1
print("Valids:", valid)
if __name__ == "__main__":
main()

19
2020/new.py Executable file
View file

@ -0,0 +1,19 @@
#!/usr/bin/env python3
import os
from datetime import date
TEMPLATE = """
def main():
with open("{}", "rt") as f:
lines = [line.rstrip() for line in f.readlines()]
if __name__ == "__main__":
main()
"""
day = date.today().day
with open(f"day{day}.py", "wt") as f:
f.write(TEMPLATE.format(f"day{day}.txt"))
os.system(f"python ../fetch.py {day}")

View file

@ -1,7 +0,0 @@
def main():
with open("", "rt") as f:
lines = [line.rstrip() for line in f.readlines()]
if __name__ == "__main__":
main()