mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2025-09-02 13:20:40 +00:00
solutions
This commit is contained in:
17
solutions/1/main.py
Normal file
17
solutions/1/main.py
Normal 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
28
solutions/2/main.py
Normal 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.')
|
Reference in New Issue
Block a user