45 lines
1.2 KiB
Python
Executable File
45 lines
1.2 KiB
Python
Executable File
#! /usr/bin/env python3
|
|
import re
|
|
|
|
|
|
def part1():
|
|
print("Part1:")
|
|
total_valid = 0
|
|
with open("./input.txt", "r") as f:
|
|
for line in f:
|
|
g = re.search("([0-9]+)-([0-9]+) ([a-zA-Z]): ([a-zA-Z]+)", line)
|
|
min_count = int(g.group(1))
|
|
max_count = int(g.group(2))
|
|
letter = g.group(3)
|
|
password = g.group(4)
|
|
# print(f"min: {min_count} max: {max_count} letter: {letter} password: {password}")
|
|
count = password.count(letter)
|
|
if min_count <= count <= max_count:
|
|
total_valid += 1
|
|
|
|
print(f"Total valid {total_valid}")
|
|
|
|
|
|
def part2():
|
|
print("Part2:")
|
|
total_valid = 0
|
|
with open("./input.txt", "r") as f:
|
|
for line in f:
|
|
g = re.search("([0-9]+)-([0-9]+) ([a-zA-Z]): ([a-zA-Z]+)", line)
|
|
password = g.group(4)
|
|
try:
|
|
pos1 = password[int(g.group(1))-1]
|
|
pos2 = password[int(g.group(2))-1]
|
|
except IndexError:
|
|
continue
|
|
letter = g.group(3)
|
|
if (pos1 == letter or pos2 == letter) and pos1 != pos2:
|
|
total_valid += 1
|
|
|
|
print(f"Total valid {total_valid}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
part1()
|
|
part2()
|