-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.py
49 lines (40 loc) · 1023 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
42
43
44
45
46
47
48
49
f = open('input2.txt','r')
txt = f.read()
txt = txt.split('\n')
puzzle_input = txt
def find_pattern(s):
hyphen_ind = s.find('-')
first_num = int(s[0:hyphen_ind])
space_ind = s.find(' ')
colon_ind = s.find(':')
ch = s[space_ind+1:space_ind+2]
second_num = int(s[hyphen_ind+1:space_ind])
passwd = s[colon_ind+2:]
return (first_num, second_num, ch, passwd)
def is_valid(s, pat):
cnt = s.count(pat[2])
if cnt >= pat[0] and cnt <= pat[1]:
return True
else:
return False
def is_valid2(s, pat):
cnt=0
if s[pat[0]-1]==pat[2]:
cnt+=1
if s[pat[1]-1]==pat[2]:
cnt+=1
if cnt==1:
return True
else:
return False
cnt=0
for s in puzzle_input:
pat = find_pattern(s)
cnt+=int(is_valid(pat[-1], pat))
print(f'part 1 results={cnt}')
# part 2:
cnt=0
for s in puzzle_input:
pat = find_pattern(s)
cnt+=int(is_valid2(pat[-1], pat))
print(f'part 2 results={cnt}')