-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.py
94 lines (84 loc) · 1.9 KB
/
Day13.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from functools import cmp_to_key
def parse():
with open("Day13input.txt", "r") as file:
pairs = []
temp = []
i = 0
for line in file:
if i < 2:
temp.append(eval(line))
i+=1
else:
i = 0
pairs.append(temp)
temp = []
return pairs
def parse2():
with open("Day13input.txt", "r") as file:
packets = []
i = 0
for line in file:
if i == 2:
i = 0
continue
packets.append(eval(line))
i+=1
packets.append([[2]])
packets.append([[6]])
return packets
def check(l, r):
if type(l) == type(r) == list:
if len(l) == len(r) == 0:
return -1
elif len(l) == 0:
return True
elif len(r) == 0:
return False
for i in range(len(l)):
if i == len(r):
return False
temp = check(l[i], r[i])
if temp != -1:
return temp
if len(l) < len(r):
return True
return -1
elif type(l) == list:
return check(l, [r])
elif type(r) == list:
return check([l], r)
else:
if l == r:
return -1
return l < r
def part1(p):
index = 1
sum = 0
for pair in p:
bo = True
left = pair[0]
right = pair[1]
t = check(left, right)
if t:
sum += index
index += 1
return sum
def compare(l, r):
t = check(l, r)
if t:
return -1
else:
return 1
def part2(p):
p = sorted(p, key=cmp_to_key(compare))
t = 1
index = 1
for el in p:
if el in [[[2]], [[6]]]:
t *= index
index += 1
return t
p = parse()
print("part 1", part1(p))
pl = parse2()
print("part 2", part2(pl))