-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.py
41 lines (31 loc) · 932 Bytes
/
day2.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
import re
with open("input/day2", "r") as input:
data = input.read().split("\n")
data.pop()
def extractData(inputStr):
match = re.match(r"(\d+)-(\d+) ([a-z]): (\w+)", inputStr)
min = int(match[1])
max = int(match[2])
targetChar = match[3]
pw = match[4]
return tuple((min, max, targetChar, pw))
data = [extractData(entry) for entry in data]
numberOfValidPws_pt1 = 0
numberOfValidPws_pt2 = 0
for entry in data:
min = entry[0]
max = entry[1]
targetChar = entry[2]
pw = entry[3]
count = 0
for char in pw:
if char == targetChar:
count += 1
if count > max:
break
if count >= min and count <= max:
numberOfValidPws_pt1 += 1
if (pw[min - 1] == targetChar) != (pw[max - 1] == targetChar):
numberOfValidPws_pt2 += 1
print("Part 1: " + str(numberOfValidPws_pt1))
print("Part 2: " + str(numberOfValidPws_pt2))