I really wanted this to work without iteration, but I was getting nowhere.
#!/usr/bin/python
import timeit
from pandas import read_table
df = read_table('input_day2.txt', sep='-| |: ', header=None, engine='python')
# Part 1
starttime = timeit.default_timer()
my_check = 0
for index, row in df.iterrows():
my_check = my_check + (row[3].count(row[2]) >= row[0] and row[3].count(row[2]) <= row[1])
print("Part 1 - Number of valid passwords:", my_check)
print("Time taken: ", timeit.default_timer() - starttime)
# Part 2
starttime = timeit.default_timer()
my_check = 0
for index, row in df.iterrows():
my_check = my_check + ((row[3][row[0]-1] == row[2]) is not (row[3][row[1]-1] == row[2]))
print("Part 2 - Number of valid passwords:", my_check)
print("Time taken: ", timeit.default_timer() - starttime)
:: tiswas@laptop2:/media/storage/Documents/Technical/Git/AoC2020 ::
$ ./day2_password_check.py
Part 1 - Number of valid passwords: 500
Time taken: 0.13249829085543752
Part 2 - Number of valid passwords: 313
Time taken: 0.1401983331888914
Stopped twatting about and finished day 2.
I really wanted this to work without iteration, but I was getting nowhere.