-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_4.py
67 lines (51 loc) · 1.63 KB
/
day_4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import re
print("Advent Of Code - Day 4")
PUZZLEINPUT = "284639-748759"
lower_bound,upper_bound = PUZZLEINPUT.split('-')
valid_passwords = 0
valid_passwords_2 = 0
def same_digits(password):
if re.search(r"(.)\1", password):
return True
else:
return False
def decreasing_digit(password):
password = list(str(password))
if password == sorted(password):
return True
return False
for i in range(int(lower_bound),int(upper_bound)):
rule1 = same_digits(str(i))
rule2 = decreasing_digit(str(i))
if rule1 and rule2:
valid_passwords += 1
print(f'Part 1: {valid_passwords}')
# def same_digits_group(password):
# matchs = re.match(r"(.)\1",pw)
# if match:
# if (len(match.group())) is 2:
# return True
# else:
# return False
# else:
# return False
# def same_digits_group(password):
# matches = re.findall(r"(([0-9])\2)",password)
# for match in matches:
# print(f'match is {match} and length is {len(matches[0])} for pw {pw}')
# if len(matches[0]) == 2:
# return True
# else:
# return False
def same_digits_group(password):
password = list(password)
for i in range(0,5):
if password[i] == password[i+1] and (i == 0 or password[i] != password[i-1]) and (i == 4 or password[i] != password[i+2]):
return True
return False
for i in range(int(lower_bound),int(upper_bound)):
rule1 = same_digits_group(str(i))
rule2 = decreasing_digit(str(i))
if rule1 and rule2:
valid_passwords_2 += 1
print(f'Part 2: {valid_passwords_2}')