day 2
This commit is contained in:
parent
decaf69ba5
commit
2a6451876e
35
02/02.py
Normal file
35
02/02.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
def read_file():
|
||||
with open("input.txt","r") as f:
|
||||
return f.read().split('\n')
|
||||
|
||||
def part1(passwords):
|
||||
valid = 0
|
||||
for entry in passwords:
|
||||
[policy, password] = entry.split(': ')
|
||||
[counts, character] = policy.split(' ')
|
||||
[minimum, maximum] = counts.split('-')
|
||||
if int(minimum) <= password.count(character) <= int(maximum):
|
||||
valid += 1
|
||||
return valid
|
||||
|
||||
def part2(passwords):
|
||||
valid = 0
|
||||
for entry in passwords:
|
||||
[policy, password] = entry.split(': ')
|
||||
[positions, character] = policy.split(' ')
|
||||
[p1, p2] = positions.split('-')
|
||||
p1 = int(p1) - 1 # convert 1-index to 0-index
|
||||
p2 = int(p2) - 1 # convert 1-index to 0-index
|
||||
first_match = password[p1] == character
|
||||
second_match = password[p2] == character
|
||||
if first_match + second_match == 1: # exclusive or
|
||||
valid += 1
|
||||
return valid
|
||||
|
||||
def main():
|
||||
passwords = read_file()
|
||||
print(f"Part 1: {part1(passwords)}")
|
||||
print(f"Part 2: {part2(passwords)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
1000
02/input.txt
Normal file
1000
02/input.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue