adventofcode2020/2/2.py

33 lines
725 B
Python
Executable File

#!/usr/bin/env python3
from random import randrange
import sys
L = []
with open(sys.argv[1], 'r') as f:
L = f.readlines()
L = [x.strip().split(' ') for x in L]
# part 1
valid = 0
for i in L:
minmax = i[0].split('-')
minmax = [int(x) for x in i[0].split('-')]
char = i[1].strip(':')
count = i[2].count(char)
if count >= minmax[0] and count <= minmax[1]:
valid += 1
print(valid)
# part 2
valid = 0
for i in L:
minmax = i[0].split('-')
minmax = [int(x) for x in i[0].split('-')]
char = i[1].strip(':')
if (i[2][minmax[0] - 1] == char and i[2][minmax[1] - 1] != char) or \
(i[2][minmax[0] - 1] != char and i[2][minmax[1] - 1] == char):
valid += 1
print(valid)