Browse Source

Day 2b

main
Noëlle 3 years ago
parent
commit
1b52885fa4
2 changed files with 34 additions and 5 deletions
  1. 11
    5
      day2a.py
  2. 23
    0
      day2b.py

+ 11
- 5
day2a.py View File

@@ -1,11 +1,17 @@
import re

def checkpw(a, b, l, pw):
print(a, b, l, pw)
c = pw.count(l)
if a <= c <= b:
return True
return False
try:
c = pw[a-1]
d = pw[b-1]
if c == l or d == l:
if c == l and d == l:
return False
return True
return False
except IndexError as e:
print(a, b, l, pw)
print(e)

def main():
patt = re.compile("^(\d+)-(\d+) (.): (.+)$")

+ 23
- 0
day2b.py View File

@@ -0,0 +1,23 @@
import re

def checkpw(a, b, l, pw):
print(a, b, l, pw)
c = pw.count(l)
if a <= c <= b:
return True
return False

def main():
patt = re.compile("^(\d+)-(\d+) (.): (.+)$")
with open("input2.txt") as file:
lines = file.readlines()
ln = []
for line in lines:
m = re.match(patt, line)
ln.append([int(m.group(1)), int(m.group(2)), m.group(3), m.group(4)])
valid = [pw for pw in ln if checkpw(pw[0], pw[1], pw[2], pw[3])]
print(f"There are {len(valid)} valid passwords out of {len(ln)}.")


if __name__ == "__main__":
main()

Loading…
Cancel
Save