advent-of-code/2019/day04_password.py

35 lines
753 B
Python
Raw Normal View History

2020-03-04 22:28:50 +01:00
def matches(num):
two_adjacents = False
2020-03-04 22:42:04 +01:00
num = str(num)
i = 0
while i < len(num):
current = num[i]
previous = ""
before_previous = ""
next_ = ""
if i > 0:
previous = num[i - 1]
if i > 1:
before_previous = num[i - 2]
if i < len(num) - 1:
next_ = num[i + 1]
if current == previous and current != before_previous and current != next_:
2020-03-04 22:28:50 +01:00
two_adjacents = True
2020-03-04 22:42:04 +01:00
if current < previous:
2020-03-04 22:28:50 +01:00
return 0
2020-03-04 22:42:04 +01:00
i += 1
2020-03-04 22:28:50 +01:00
return 1 if two_adjacents else 0
def main():
count = 0
for password in range(271973, 785961 + 1):
count += matches(password)
print(count)
if __name__ == "__main__":
main()