-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
executable file
·32 lines (27 loc) · 870 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
#!/usr/bin/env python3
# [Day 2: Password Philosophy](https://adventofcode.com/2020/day/2)
import re
import sys
from pathlib import Path
filename = ("test.txt" if sys.argv[1] == "-t" else sys.argv[1]) if len(sys.argv) > 1 else "input.txt"
data = Path(filename).read_text().strip()
lines = data.splitlines()
# part 1
valid = 0
for line in lines:
min, max, letter, password = re.match(r"(\d+)\-(\d+) (\w): (\w+)", line).groups()
min, max = int(min), int(max)
if min <= password.count(letter) <= max:
valid += 1
print(valid)
# part 2
valid = 0
for line in lines:
a, b, letter, password = re.match(r"(\d+)\-(\d+) (\w): (\w+)", line).groups()
a = int(a)
b = int(b)
if (password[a - 1] == letter and password[b - 1] != letter) or (
password[a - 1] != letter and password[b - 1] == letter
):
valid += 1
print(valid)