-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay9.py
63 lines (41 loc) · 1.51 KB
/
Day9.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
### INPUT ###
with open('Data\Day9data.txt') as file:
XMAS = [int(n) for n in file.read().split('\n')[:-1]]
N = len(XMAS)
### FUNCTIONS ###
def xmascheck(pre, xmas):
'''
Parameters
----------
pre : length of preamble
xmas : XMAS encryption file
Returns
-------
List of numbers which are not valid in the xmas file
'''
invalid = [] # to be returned after filling with invalid numbers
for i in range(pre, N):
# lets have a variable for checking if xmas[i] is valid or not
valid = False
# can we find a way around this with stuff like pass/break?
# because i know this is bad
for j in range(pre):
if xmas[i] - xmas[i - (j+1)] in xmas[i-pre:i]:
valid = True
if valid == False:
invalid.append(xmas[i])
return(invalid)
### DAY 1 ###
weakness = xmascheck(25, XMAS)[0]
print(weakness)
### DAY 2 ###
def foo():
# returns smallest/greatest values in the sum
# made it a function because its the easiest way to get out of loops when you're done with them
for i in range(N): # XMAS[i] is the first value in the sum
for j in range(i, N): # XMAS[j-1] is the last value in the sum
if sum(XMAS[i:j]) > weakness:
break
if sum(XMAS[i:j]) == weakness:
return([min(XMAS[i:j]), max(XMAS[i:j])])
print(sum(foo()))