diff options
| author | Gustav Sörnäs <gusso230@student.liu.se> | 2019-12-10 22:33:50 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gusso230@student.liu.se> | 2019-12-10 22:33:50 +0100 |
| commit | d61ef9e520eea9551f495de6e6e88f2eb991a0c4 (patch) | |
| tree | 94c12ae14606779b6bed715aaa583c085a589775 | |
| parent | e90da7fb85326094a01df6f4a3391462698f00bc (diff) | |
| download | aoc-d61ef9e520eea9551f495de6e6e88f2eb991a0c4.tar.gz | |
Faster day 4
| -rw-r--r-- | solutions/py/d04.py | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/solutions/py/d04.py b/solutions/py/d04.py index e3fb41a..778b05e 100644 --- a/solutions/py/d04.py +++ b/solutions/py/d04.py @@ -1,13 +1,17 @@ from collections import Counter def isIncreasing(num): - s = str(num) + s = list(str(num)) n = int(s[0]) + n_i = 0 for sp in s[1:]: + n_i += 1 if int(sp) < n: - return False + for i in range(n_i, 6): + s[i] = str(n) + return False, int("".join(s)) n = int(sp) - return True + return (True,) def pt1(input): def containsDouble(num): @@ -19,10 +23,14 @@ def pt1(input): return c[0] + c[1] < 10 amount = 0 - for n in range(357253, 892942 + 1): - if isIncreasing(n): + n = 357253 + while n < 892942 + 1: + if (inc := isIncreasing(n))[0] == True: if containsDouble(n): amount += 1 + n += 1 + else: + n = inc[1] return amount def pt2(input): @@ -36,10 +44,14 @@ def pt2(input): return c[2] >= 1 amount = 0 - for n in range(357253, 892942 + 1): - if isIncreasing(n): + n = 357253 + while n < 892942 + 1: + if (inc := isIncreasing(n))[0] == True: if containsDouble(n): amount += 1 + n += 1 + else: + n = inc[1] return amount if __name__ == "__main__": |
