summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--20/py/d02.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/20/py/d02.py b/20/py/d02.py
new file mode 100644
index 0000000..572404e
--- /dev/null
+++ b/20/py/d02.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+def pt1(_in):
+ valid = 0
+ for passwd in _in:
+ want, have = passwd.split(":")
+ want_num, want_char = want.split()
+ want_min, want_max = [int(s) for s in want_num.split("-")]
+ if want_min <= have.count(want_char) <= want_max:
+ valid += 1
+ return valid
+
+
+def pt2(_in):
+ valid = 0
+ for passwd in _in:
+ want, have = passwd.split(":")
+ want_num, want_char = want.split()
+ want_first, want_second = [int(s) for s in want_num.split("-")]
+ matches = 0
+ if have[want_first] == want_char:
+ matches += 1
+ if have[want_second] == want_char:
+ matches += 1
+ if matches == 1:
+ valid += 1
+ return valid
+
+
+if __name__ == "__main__":
+ input = open("../input/02", "r").readlines()
+ print(pt1(input))
+ print(pt2(input))