solutions

This commit is contained in:
cupcakearmy 2020-12-02 13:07:57 +01:00
commit 88857ca3ce
3 changed files with 174 additions and 0 deletions

129
.gitignore vendored Normal file
View File

@ -0,0 +1,129 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/

17
solutions/1/main.py Normal file
View File

@ -0,0 +1,17 @@
from typing import List
from itertools import product
from os.path import join, dirname
target = 2020
data = join(dirname(__file__), 'data.txt')
with open(data) as f:
numbers: List[int] = list(map(int, f.readlines()))
for a, b in product(numbers, numbers):
if a + b == target:
print(f'The numbers: {a} and {b}.\tSolution: {a*b}')
break
for a, b, c in product(numbers, numbers, numbers):
if a + b + c == target:
print(f'The numbers: {a}, {b} and {c}.\tSolution: {a*b*c}')
break

28
solutions/2/main.py Normal file
View File

@ -0,0 +1,28 @@
from typing import List
from os.path import join, dirname
def checkRow(row: str, alternative=False) -> bool:
rule, password = map(lambda s: s.strip(), row.split(':'))
amount, char = rule.split(' ')
minimum, maximum = map(int, amount.split('-'))
if alternative:
return (password[minimum - 1] == char) ^ (password[maximum - 1] != char)
else:
occurrences = password.count(char)
return minimum <= occurrences <= maximum
data = join(dirname(__file__), 'data.txt')
with open(data) as f:
valid = 0
valid_alt = 0
rows = list(f.read().strip().split('\n'))
for row in rows:
if(checkRow(row)):
valid += 1
if(checkRow(row, alternative=True)):
valid_alt += 1
print(f'Found {valid} valid passwords.')
print('Policy changed...')
print(f'Found {valid_alt} valid passwords.')