diff options
| author | Gustav Sörnäs <gusso230@student.liu.se> | 2019-12-08 22:05:31 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gusso230@student.liu.se> | 2019-12-08 22:05:31 +0100 |
| commit | d8f929604f18cacc40eec80f3bbd4386757a4902 (patch) | |
| tree | f4bbce9565f266c8ffc1d3d45e02437e6175172f /solutions/py/d04.py | |
| parent | d860795218edd56ccaae8adbfee3ac916bc8b3fd (diff) | |
| download | aoc-d8f929604f18cacc40eec80f3bbd4386757a4902.tar.gz | |
Refactor day 4
Diffstat (limited to 'solutions/py/d04.py')
| -rw-r--r-- | solutions/py/d04.py | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/solutions/py/d04.py b/solutions/py/d04.py index 5628925..6b2af1d 100644 --- a/solutions/py/d04.py +++ b/solutions/py/d04.py @@ -12,35 +12,37 @@ def isIncreasing(num): def pt1(input): def containsDouble(num): s = str(num) - amounts = [] - for n in (0,1,2,3,4,5,6,7,8,9): # herregud - amounts.append(s.count(str(n))) + amounts = [0 for _ in range(10)] + for c in s: + amounts[int(c)] += 1 c = Counter(amounts) return c[0] + c[1] < 10 amount = 0 for n in range(357253, 892942 + 1): - if containsDouble(n): - if isIncreasing(n): + if isIncreasing(n): + if containsDouble(n): amount += 1 return amount def pt2(input): def containsDouble(num): s = str(num) - amounts = [] - for n in (0,1,2,3,4,5,6,7,8,9): # herregud - amounts.append(s.count(str(n))) + amounts = [0 for _ in range(10)] + for c in s: + amounts[int(c)] += 1 c = Counter(amounts) if c[0] + c[1] < 10: return c[2] >= 1 + amount = 0 for n in range(357253, 892942 + 1): - if containsDouble(n): - if isIncreasing(n): + if isIncreasing(n): + if containsDouble(n): amount += 1 return amount if __name__ == "__main__": import cProfile cProfile.run("pt1([])") + cProfile.run("pt2([])") |
