-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.py
69 lines (57 loc) · 1.89 KB
/
part2.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
##############
# Read input #
##############
# after many tests; I found it easier to go through the datas this way
def readInput():
with open('input') as f:
return list(map(lambda i: i.strip(), f.readlines()))
def parseData(input):
lists = []
for l in input:
if not l:
continue
lists.append(eval(l))
return lists
# This is not clean at all but I have no idea how to do it cleaner
# Basically it just compares the left and right value
# if left > right return 0
# else return 1
def compare(l, r):
i = 0
while True:
if len(l) <= i < len(r):
return 1
elif len(r) <= i < len(l):
return 0
elif i >= len(l) and i >= len(r):
return None
if isinstance(l[i], int) and isinstance(r[i], int):
if l[i] < r[i]:
return 1
elif r[i] < l[i]:
return 0
if isinstance(l[i], list) and isinstance(r[i], list):
result = compare(l[i], r[i])
if result is not None:
return result
if isinstance(l[i], list) and isinstance(r[i], int):
result = compare(l[i], [r[i]])
if result is not None:
return result
if isinstance(l[i], int) and isinstance(r[i], list):
result = compare([l[i]], r[i])
if result is not None:
return result
i += 1
# Add new packets and order them
# return the index of packet A * index of packet B
def countOrder(lists):
lists.append([[2]])
lists.append([[6]])
for i in range(len(lists)):
for j in range(i + 1, len(lists)):
if compare(lists[i], lists[j]) == 0:
lists[i], lists[j] = lists[j], lists[i]
return (lists.index([[2]]) + 1) * (lists.index([[6]]) + 1)
if __name__ == '__main__':
print("Part 2 : ", countOrder(parseData(readInput())))