Skip to content

Commit fc78e19

Browse files
committed
nicer formatting
1 parent 7faf80d commit fc78e19

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

day02/day02.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,38 @@
22

33
import re
44

5+
56
def check_policy(policy: str, password: str, mode: int = 1) -> bool:
67
numbers, char = policy.split(" ")
78
lower_bound, upper_bound = [int(num) for num in numbers.split("-")]
89

910
if mode == 1:
1011
occ = password.count(char)
11-
return lower_bound <= occ <= upper_bound
12+
return lower_bound <= occ <= upper_bound
1213
else:
1314
# policy starts at place 1, not 0 in the string
14-
matches = [match.start()+1 for match in re.finditer(char, password)]
15+
matches = [match.start() + 1 for match in re.finditer(char, password)]
1516
return bool((lower_bound in matches) ^ (upper_bound in matches))
1617

17-
18-
1918

2019
if __name__ == "__main__":
2120
with open("input", "r") as infile:
2221
check_lines = [line.strip().split(":") for line in infile]
23-
print("Valid passwords for part1:", sum([check_policy(policy.strip(), password.strip()) for policy, password in check_lines]))
24-
print("Valid passwords for part2:", sum([check_policy(policy.strip(), password.strip(), 2) for policy, password in check_lines]))
22+
print(
23+
"Valid passwords for part1:",
24+
sum(
25+
[
26+
check_policy(policy.strip(), password.strip())
27+
for policy, password in check_lines
28+
]
29+
),
30+
)
31+
print(
32+
"Valid passwords for part2:",
33+
sum(
34+
[
35+
check_policy(policy.strip(), password.strip(), 2)
36+
for policy, password in check_lines
37+
]
38+
),
39+
)

0 commit comments

Comments
 (0)