day 4 2019 part 1 & part 2

This commit is contained in:
Xevion
2019-12-04 00:29:23 -06:00
parent 10b70059a4
commit 99c914e2d8
2 changed files with 28 additions and 0 deletions

1
2019/day-4/input Normal file
View File

@@ -0,0 +1 @@
359282-820401

27
2019/day-4/python/main.py Normal file
View File

@@ -0,0 +1,27 @@
import os
import sys
import re
PATH = os.path.join(sys.path[0], '..', 'input')
DATA = list(map(int, open(PATH, 'r').read().split('-')))
def criteria(n):
digits = list(map(int, list(str(n))))
repeats = False
prev = digits[0]
for digit in digits[1:]:
if prev == digit:
repeats = True
if prev > digit:
return False
prev = digit
return repeats
def rerepeats(n):
return 2 in [len(m[0]) for m in re.finditer(r'(\d)\1+', str(n))]
passwords = [x for x in range(DATA[0], DATA[1]) if criteria(x)]
print(len(passwords))
passwords = [x for x in passwords if rerepeats(x)]
print(len(passwords))