-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02.py
More file actions
52 lines (42 loc) · 1.31 KB
/
02.py
File metadata and controls
52 lines (42 loc) · 1.31 KB
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
f = open('02.input', 'r')
data = [x.strip() for x in f.readlines()]
# Assumptions:
# - 1010 is not present
def part1(data):
total = 0
for x in data:
span, letter, pw = x.split()
start, end = map(int, span.split('-'))
count = pw.count(letter[0])
if start <= count <= end:
total +=1
return total
def part2(data):
total = 0
for x in data:
span, letter, pw = x.split()
start, end = map(int, span.split('-'))
position1 = pw[start-1] == letter[0]
position2 = pw[end-1] == letter[0]
if position1 != position2:
total +=1
return total
print("Solution part 1: %d" % part1(data))
print("Solution part 2: %d" % part2(data))
# Alternate solutions
def part1functional(data):
def validate(line):
span, letter, pw = line.split()
start, end = map(int, span.split('-'))
count = pw.count(letter[0])
return start <= count <= end
valid_passwords = filter(validate, data)
return len(valid_passwords)
import re
def part1regex(data):
def validate(line):
start, end, ch, pw = re.search(r'(\d+)-(\d+) (.): (\w+)', line).groups()
return int(start) <= pw.count(ch) <= int(end)
valid_passwords = filter(validate, data)
return len(valid_passwords)
print(part1regex(data))