Skip to content

Commit 2318e1a

Browse files
committed
added solution for day 2 in python
1 parent 06882fc commit 2318e1a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import re
2+
lines = []
3+
with open("PATH") as f:
4+
lines = f.readlines()
5+
6+
valid_1 = 0
7+
valid_2 = 0
8+
9+
pattern = '([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)'
10+
11+
for i in lines:
12+
match = re.search(pattern, i)
13+
min = int(match.group(1))
14+
max = int(match.group(2))
15+
char = match.group(3)
16+
pw = match.group(4)
17+
18+
count = pw.count(char)
19+
20+
if count >= min and count <= max:
21+
valid_1 += 1
22+
23+
# part 2
24+
25+
if pw[min - 1] == char and not pw[max - 1] == char:
26+
valid_2 += 1
27+
if pw[max - 1] == char and not pw[min - 1] == char:
28+
valid_2 += 1
29+
30+
print(valid_1)
31+
print(valid_2)

0 commit comments

Comments
 (0)