From 070296abe3bad660e9ba04fa87c58258c46b1226 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Wed, 4 Mar 2020 22:42:04 +0100 Subject: [PATCH] Solve day 4 part 2 --- 2019/day04-password.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/2019/day04-password.py b/2019/day04-password.py index 2bf1e02..0afc8cb 100644 --- a/2019/day04-password.py +++ b/2019/day04-password.py @@ -1,12 +1,23 @@ def matches(num): - previous = "" two_adjacents = False - for digit in str(num): - if digit == previous: + 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_: two_adjacents = True - if digit < previous: + if current < previous: return 0 - previous = digit + i += 1 return 1 if two_adjacents else 0