aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Brattlof <hello@bryanbrattlof.com>2020-12-11 10:28:08 -0500
committerBryan Brattlof <hello@bryanbrattlof.com>2020-12-11 10:28:08 -0500
commit9f0b88800f2f8136d53a5ad0cfc8492e51955780 (patch)
tree41bfd4488d7104996e5f0ac572a31db2599431d8
parent00b7e91794d5c59bbf99b3c69091f51af921278a (diff)
downloadadvent-of-code-9f0b88800f2f8136d53a5ad0cfc8492e51955780.tar.gz
advent-of-code-9f0b88800f2f8136d53a5ad0cfc8492e51955780.tar.bz2
day 2.2 of Advent of Code 2020
-rw-r--r--2020/day02/password-philosophy.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/2020/day02/password-philosophy.py b/2020/day02/password-philosophy.py
index d5147f2..13222e5 100644
--- a/2020/day02/password-philosophy.py
+++ b/2020/day02/password-philosophy.py
@@ -18,6 +18,13 @@ class Password(NamedTuple):
total = self.passwd.count(self.char)
return self.lo <= total <= self.hi
+ def is_valid2(self) -> bool:
+ """is password valid for correct company"""
+ is_lo = self.passwd[self.lo - 1] == self.char
+ is_hi = self.passwd[self.hi - 1] == self.char
+
+ return is_lo != is_hi
+
@staticmethod
def parse(line: str) -> Password:
"""parse test line into Password object"""
@@ -26,5 +33,9 @@ class Password(NamedTuple):
char = char[0] # drop the colon
return Password(lo, hi, char, password)
+
with open('data/passwords.txt') as f:
print(f'total valid: {sum(Password.parse(line).is_valid() for line in f)}')
+
+with open('data/passwords.txt') as f:
+ print(f'new valid: {sum(Password.parse(line).is_valid2() for line in f)}')