44from itertools import islice , permutations
55
66from typing import Any , List , Iterator , Generator
7+
78# from ..tools import sliding_window
89
910
@@ -20,19 +21,35 @@ def window(seq: Iterator[Any], n: int = 2) -> Generator[Any, None, None]:
2021 yield result
2122
2223
24+ def check_continous (numbers : List [int ], target : int ) -> int :
25+ # check increasing sliding windows, sum needs to be target
26+ for i in range (2 , len (numbers )):
27+ for slidewin in window (numbers , i ):
28+ if sum (slidewin ) == target :
29+ print ("slide:" , slidewin )
30+ return int (min (slidewin ) + max (slidewin ))
31+
32+ return 0
33+
34+
2335def check_sums (numbers : List [int ], preamble_len : int ) -> int :
24- for * sublist , tail in window (numbers , preamble_len + 1 ):
36+ for * sublist , tail in window (numbers , preamble_len + 1 ):
2537 results = [sum (perm ) == tail for perm in permutations (sublist , 2 )]
2638 if not any (results ):
27- return tail
39+ return int ( tail )
2840 return - 1
2941
42+
3043if __name__ == "__main__" :
3144 preamble_len = int (sys .argv [2 ])
3245 with open (sys .argv [1 ], "r" ) as infile :
3346 numbers = []
3447 for nextline in infile :
3548 num = int (nextline .strip ())
3649 numbers .append (num )
37- print ("Part 1:" , check_sums (numbers , preamble_len ), " is not the sum of the preceding %s numbers" % preamble_len )
38- print ("Part 2:" )
50+ target = check_sums (numbers , preamble_len )
51+ print (
52+ "Part 1:" , target , " is not the sum of the preceding %s numbers" % preamble_len
53+ )
54+ part2_sum = check_continous (numbers [: numbers .index (target )], target )
55+ print ("Part 2:" , part2_sum )
0 commit comments