-
Notifications
You must be signed in to change notification settings - Fork 0
/
advent-02.py
49 lines (36 loc) · 1.17 KB
/
advent-02.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
"""
URL for challenge: https://adventofcode.com/2020/day/2
"""
def part1():
f = open("advent-02-input.txt")
num_valid_pwds = 0
for policy_pwd in f.readlines():
limits, letter, pwd = policy_pwd.split(sep=' ')
lower_lim, upper_lim = [int(li) for li in limits.split(sep='-')]
letter_count = pwd.count(letter[0])
if lower_lim <= letter_count <= upper_lim:
num_valid_pwds += 1
return num_valid_pwds
def part2():
f = open("advent-02-input.txt")
num_valid_pwds = 0
for policy_pwd in f.readlines():
indices, letter, pwd = policy_pwd.split(sep=' ')
lower_idx, upper_idx = [int(idx) for idx in indices.split(sep='-')]
letter = letter[0]
first_case = pwd[lower_idx-1] == letter
second_case = pwd[upper_idx-1] == letter
# Use XOR gate
if first_case ^ second_case:
num_valid_pwds += 1
return num_valid_pwds
def run():
chall = int(input("Please enter either 1 or 2 for the challenges: "))
if chall == 1:
print(part1())
elif chall == 2:
print(part2())
else:
print("You need to enter either 1 or 2")
exit(1)
run()