Browse Source

Use more-itertools to make the day 1 solution smaller

Frans Bergman 3 years ago
parent
commit
1f1c295b75
2 changed files with 7 additions and 13 deletions
  1. 6 13
      1.py
  2. 1 0
      requirements.txt

+ 6 - 13
1.py

@@ -1,17 +1,10 @@
 import util
+from more_itertools.recipes import sliding_window as sw
 
-def sliding_window(input, size=1):
-    count = 0
-    current = input[0:size]
-
-    for i in input[size:]:
-        next = current[1:] + [i]
-        if sum(next) > sum(current):
-            count += 1
-        current = next
+input = util.get_input("1.input", int)
 
-    return count
+def day_1(input, size):
+    return len([1 for [a, b] in sw(sw(input, size), 2) if sum(a) < sum(b)])
 
-input = util.get_input("1.input", int)
-print(sliding_window(input, 1))
-print(sliding_window(input, 3))
+print(day_1(input, 1))
+print(day_1(input, 3))

+ 1 - 0
requirements.txt

@@ -0,0 +1 @@
+more-itertools>=8.12.0